Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
315 lines (292 loc) · 13.9 KB
/
Copy pathcli.py
File metadata and controls
315 lines (292 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from __future__ import annotations
import argparse
import sys
from typing import Any, Iterable
from .core.auth import resolve_login_payload, save_cookie_payload, session_from_cookies
from .core.client import WqbClient
from .commands.account import add_account_parser, handle_account
from .commands.alpha import add_alpha_parser, handle_alpha
from .commands.competition import add_competition_parser, handle_competition
from .commands.config import add_config_parser, handle_config
from .commands.consultant import add_consultant_parser, handle_consultant
from .commands.community import add_community_parser, handle_community
from .commands.help_center import add_help_center_parser, handle_help_center
from .commands.sqlitecom import add_sqlitecom_parser, handle_sqlitecom
from .commands.data import add_data_parser, handle_data
from .commands.docs import add_docs_parser, handle_docs
from .commands.event import add_event_parser, handle_event
from .commands.errors import add_errors_parser, handle_errors
from .commands.platform import add_platform_parser, handle_platform
from .commands.search import add_search_parser, handle_search
from .commands.shortcut import add_shortcut_parser, handle_shortcut
from .commands.simu import add_simu_parser, handle_simu
from .commands.scope import add_scope_parser, handle_scope
from .commands.suggest import add_suggest_parser, handle_suggest
from .commands.tutorial import add_tutorial_parser, handle_tutorial
from .commands.user import add_user_parser, handle_user
from .commands.upgrade import add_upgrade_parser, handle_upgrade
from .core.io import parse_key_values, read_json_file, write_json
from .core.plugins import PluginLoadError, discover_plugins, register_plugins
from .core.registry import EndpointRegistry
from .sdk import CliPlugin, PluginContext
from .sqlitesimu.plugin import plugin as sqlitesimu_plugin
from . import __version__
def build_parser(plugins: Iterable[CliPlugin] | None = None) -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(prog="wqb", description="Agent-first WorldQuant BRAIN API CLI")
parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
parser.add_argument("--registry", help="Path to api_inventory_complete.json")
parser.add_argument("--cookies", help="Path to cookies.json; default is wqb_cli/local/auth/cookies.json")
sub = parser.add_subparsers(dest="command", required=True)
api = sub.add_parser("api", help="Call API endpoints from the registry")
api_sub = api.add_subparsers(dest="api_command", required=True)
stats_parser = api_sub.add_parser("stats", help="Show registry statistics")
stats_parser.add_argument("--output", help="Write JSON result to file")
list_parser = api_sub.add_parser("list", help="List registered endpoints")
list_parser.add_argument("--prefix", help="Filter endpoint path prefix")
list_parser.add_argument("--output", help="Write JSON result to file")
show_parser = api_sub.add_parser("show", help="Show one endpoint definition")
show_parser.add_argument("path")
show_parser.add_argument("--output", help="Write JSON result to file")
params_parser = api_sub.add_parser("params", help="Show query/body parameter hints for one endpoint")
params_parser.add_argument("path")
params_parser.add_argument("--output", help="Write JSON result to file")
call_parser = api_sub.add_parser("call", help="Execute an endpoint call")
call_parser.add_argument("method")
call_parser.add_argument("path")
call_parser.add_argument("--var", action="append", help="Template variable KEY=VALUE")
call_parser.add_argument("--param", action="append", help="Query parameter KEY=VALUE")
call_parser.add_argument("--input", help="JSON file containing path_vars, params, json")
call_parser.add_argument("--json", help="Inline JSON request body")
call_parser.add_argument("--dry-run", action="store_true", help="Validate and preview the request without sending HTTP")
call_parser.add_argument("--env-auth", action="store_true", help="For POST /authentication, read EMAIL/PASSWORD from wqb_cli/local/.env")
call_parser.add_argument("--output", help="Write JSON result to file")
auth = sub.add_parser("auth", help="Authentication helpers")
auth_sub = auth.add_subparsers(dest="auth_command", required=True)
status_parser = auth_sub.add_parser("status", help="GET /authentication")
status_parser.add_argument("--output", help="Write JSON result to file")
head_parser = auth_sub.add_parser("head", help="HEAD /authentication")
head_parser.add_argument("--output", help="Write JSON result to file")
login_parser = auth_sub.add_parser("login", help="POST /authentication")
login_parser.add_argument("--input", help="JSON login payload; defaults to keyring/config/env")
login_parser.add_argument("--email", help="Email override")
login_parser.add_argument("--password", help="Password override; prefer keyring or .env for normal use")
login_parser.add_argument("--expiry", type=int, default=3600, help="Token expiry seconds")
login_parser.add_argument("--config", dest="config_path", help="Path to local config.json")
login_parser.add_argument("--output", help="Write JSON result to file")
logout_parser = auth_sub.add_parser("logout", help="DELETE /authentication")
logout_parser.add_argument("--output", help="Write JSON result to file")
for name in ["brainlabs", "persona", "support", "workday"]:
auth_parser = auth_sub.add_parser(name, help=f"GET /authentication/{name}")
auth_parser.add_argument("--output", help="Write JSON result to file")
add_account_parser(sub)
add_alpha_parser(sub)
add_competition_parser(sub)
add_config_parser(sub)
add_consultant_parser(sub)
add_community_parser(sub)
add_help_center_parser(sub)
add_sqlitecom_parser(sub)
add_data_parser(sub)
add_docs_parser(sub)
add_event_parser(sub)
add_errors_parser(sub)
add_platform_parser(sub)
add_search_parser(sub)
add_shortcut_parser(sub)
add_simu_parser(sub)
add_scope_parser(sub)
add_suggest_parser(sub)
add_tutorial_parser(sub)
add_user_parser(sub)
add_upgrade_parser(sub)
resolved_plugins = discover_plugins((sqlitesimu_plugin,)) if plugins is None else plugins
register_plugins(sub, resolved_plugins)
return parser
def load_registry(args: argparse.Namespace) -> EndpointRegistry:
return EndpointRegistry.load(args.registry)
def handle_api(args: argparse.Namespace) -> int:
registry = load_registry(args)
if args.api_command == "stats":
write_json(registry.stats(), args.output)
return 0
if args.api_command == "list":
write_json(
[
{
"path": endpoint.path,
"methods": list(endpoint.methods),
"description": endpoint.description,
}
for endpoint in registry.list(args.prefix)
],
args.output,
)
return 0
if args.api_command == "show":
write_json(registry.get(args.path).raw, args.output)
return 0
if args.api_command == "params":
endpoint = registry.get(args.path)
write_json(
{
"path": endpoint.path,
"methods": list(endpoint.methods),
"path_variables": endpoint.variables,
"params": endpoint.params,
"request_body": endpoint.request_body,
"source": list(endpoint.source),
},
args.output,
)
return 0
if args.api_command == "call":
endpoint = registry.get(args.path)
input_payload = read_json_file(args.input)
path_vars: dict[str, Any] = {}
path_vars.update(input_payload.get("path_vars") or {})
path_vars.update(parse_key_values(args.var))
params: dict[str, Any] = {}
params.update(input_payload.get("params") or {})
params.update(parse_key_values(args.param))
json_body = input_payload.get("json")
if args.json:
import json as json_module
json_body = json_module.loads(args.json)
if args.env_auth:
env_payload = input_payload.get("json") if isinstance(input_payload.get("json"), dict) else {}
json_body = resolve_login_payload(input_payload=env_payload, expiry=env_payload.get("expiry", 3600))
client = WqbClient(registry, session_from_cookies(args.cookies))
prepared = client.prepare(
endpoint,
args.method,
path_vars={key: str(value) for key, value in path_vars.items()},
params=params,
json_body=json_body,
)
if args.dry_run:
write_json({"ok": prepared.executable, "dry_run": True, "request": prepared.__dict__}, args.output)
return 0 if prepared.executable else 1
result = client.call(prepared)
if (
endpoint.path == "/authentication"
and args.method.upper() == "POST"
and result.get("ok")
):
save_cookie_payload(client.session, args.cookies)
write_json(result, args.output)
return 0
raise AssertionError(args.api_command)
def handle_auth(args: argparse.Namespace) -> int:
registry = load_registry(args)
client = WqbClient(registry, session_from_cookies(args.cookies))
if args.auth_command == "status":
endpoint = registry.get("/authentication")
prepared = client.prepare(endpoint, "GET")
write_json(client.call(prepared), args.output)
return 0
if args.auth_command == "head":
endpoint = registry.get("/authentication")
prepared = client.prepare(endpoint, "HEAD")
write_json(client.call(prepared), args.output)
return 0
if args.auth_command == "login":
endpoint = registry.get("/authentication")
payload = resolve_login_payload(
input_payload=read_json_file(args.input),
email=args.email,
password=args.password,
expiry=args.expiry,
config_path=args.config_path,
)
prepared = client.prepare(endpoint, "POST", json_body=payload)
result = client.call(prepared)
if result.get("ok"):
save_cookie_payload(client.session, args.cookies)
write_json(result, args.output)
return 0
if args.auth_command == "logout":
endpoint = registry.get("/authentication")
prepared = client.prepare(endpoint, "DELETE")
result = client.call(prepared)
write_json(result, args.output)
return 0
if args.auth_command in {"brainlabs", "persona", "support", "workday"}:
endpoint = registry.get(f"/authentication/{args.auth_command}")
prepared = client.prepare(endpoint, "GET")
write_json(client.call(prepared), args.output)
return 0
raise AssertionError(args.auth_command)
def main(argv: list[str] | None = None) -> None:
try:
parser = build_parser()
except PluginLoadError as exc:
write_json({"ok": False, "error_type": type(exc).__name__, "detail": str(exc)})
sys.exit(1)
args = parser.parse_args(argv)
try:
plugin = getattr(args, "_wqb_plugin", None)
if plugin is not None:
code = plugin.handle(
args,
PluginContext(registry_path=args.registry, cookies_path=args.cookies),
)
elif args.command == "api":
code = handle_api(args)
elif args.command == "account":
code = handle_account(args, load_registry(args))
elif args.command == "auth":
code = handle_auth(args)
elif args.command == "alpha":
code = handle_alpha(args, load_registry(args))
elif args.command == "competition":
code = handle_competition(args, load_registry(args))
elif args.command == "config":
code = handle_config(args, load_registry(args))
elif args.command == "consultant":
code = handle_consultant(args, load_registry(args))
elif args.command == "community":
code = handle_community(args)
elif args.command == "help-center":
code = handle_help_center(args)
elif args.command == "sqlitecom":
code = handle_sqlitecom(args)
elif args.command == "data":
code = handle_data(args, load_registry(args))
elif args.command == "docs":
code = handle_docs(args)
elif args.command == "event":
code = handle_event(args, load_registry(args))
elif args.command == "errors":
code = handle_errors(args, load_registry(args))
elif args.command == "platform":
code = handle_platform(args, load_registry(args))
elif args.command == "search":
code = handle_search(args, load_registry(args))
elif args.command in {"shortcut", "quick"}:
code = handle_shortcut(args, load_registry(args))
elif args.command == "simu":
code = handle_simu(args, load_registry(args))
elif args.command == "scope":
code = handle_scope(args)
elif args.command == "suggest":
code = handle_suggest(args, load_registry(args))
elif args.command == "tutorial":
code = handle_tutorial(args, load_registry(args))
elif args.command == "user":
code = handle_user(args, load_registry(args))
elif args.command == "upgrade":
code = handle_upgrade(args)
else:
parser.error(f"Unknown command: {args.command}")
code = 2
except Exception as exc:
from .core.community_client import CommunityError
write_json(
exc.as_dict() if isinstance(exc, CommunityError) else
{"ok": False, "error_type": type(exc).__name__, "detail": str(exc)},
getattr(args, "output", None),
)
code = 1
sys.exit(code)
if __name__ == "__main__":
main()