mlarena-sdk 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mlarena_sdk-0.2.0/LICENSE +21 -0
- mlarena_sdk-0.2.0/PKG-INFO +168 -0
- mlarena_sdk-0.2.0/README.md +136 -0
- mlarena_sdk-0.2.0/mlarena/__init__.py +63 -0
- mlarena_sdk-0.2.0/mlarena/client.py +1051 -0
- mlarena_sdk-0.2.0/mlarena/exceptions.py +18 -0
- mlarena_sdk-0.2.0/mlarena/py.typed +0 -0
- mlarena_sdk-0.2.0/mlarena_sdk.egg-info/PKG-INFO +168 -0
- mlarena_sdk-0.2.0/mlarena_sdk.egg-info/SOURCES.txt +12 -0
- mlarena_sdk-0.2.0/mlarena_sdk.egg-info/dependency_links.txt +1 -0
- mlarena_sdk-0.2.0/mlarena_sdk.egg-info/requires.txt +4 -0
- mlarena_sdk-0.2.0/mlarena_sdk.egg-info/top_level.txt +1 -0
- mlarena_sdk-0.2.0/pyproject.toml +45 -0
- mlarena_sdk-0.2.0/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ML Arena
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mlarena-sdk
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Python SDK for ML Arena - submit agents, manage competitions, courses, and view leaderboards
|
|
5
|
+
Author-email: ML Arena <contact@ml-arena.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://ml-arena.com
|
|
8
|
+
Project-URL: Documentation, https://ml-arena.com/docs
|
|
9
|
+
Project-URL: Repository, https://github.com/ml-arena/mlarena-sdk
|
|
10
|
+
Project-URL: Bug Tracker, https://github.com/ml-arena/mlarena-sdk/issues
|
|
11
|
+
Keywords: machine-learning,reinforcement-learning,competitions,sdk,ml-arena
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: Education
|
|
15
|
+
Classifier: Intended Audience :: Science/Research
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
24
|
+
Classifier: Typing :: Typed
|
|
25
|
+
Requires-Python: >=3.8
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Requires-Dist: requests>=2.32.0
|
|
29
|
+
Provides-Extra: pandas
|
|
30
|
+
Requires-Dist: pandas>=1.0.0; extra == "pandas"
|
|
31
|
+
Dynamic: license-file
|
|
32
|
+
|
|
33
|
+
# mlarena
|
|
34
|
+
|
|
35
|
+
Python SDK for [ML Arena](https://ml-arena.com) — submit agents, manage competitions, manage courses, and read leaderboards from any notebook or IDE.
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install mlarena
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Quick Start
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
import mlarena
|
|
47
|
+
|
|
48
|
+
# Connect with your API key (Profile page → "API Keys"). The token is the full
|
|
49
|
+
# string starting with `mlk_…`, not a `key_id:key_pass` pair.
|
|
50
|
+
client = mlarena.connect(api_key="mlk_user_a1b2c3d4_<32-hex-secret>")
|
|
51
|
+
|
|
52
|
+
# List competitions (public, no auth)
|
|
53
|
+
client.competitions()
|
|
54
|
+
|
|
55
|
+
# Submit an agent class — creates an attachment, uploads, and deploys.
|
|
56
|
+
class MyAgent:
|
|
57
|
+
def predict(self, observation):
|
|
58
|
+
return 0
|
|
59
|
+
|
|
60
|
+
result = client.submit(competition_id=42, agent=MyAgent)
|
|
61
|
+
|
|
62
|
+
# Or submit files from disk
|
|
63
|
+
client.submit(competition_id=42, files=["agent.py", "model.pkl"])
|
|
64
|
+
|
|
65
|
+
# Check status of the last submission
|
|
66
|
+
client.status()
|
|
67
|
+
|
|
68
|
+
# View leaderboard (returns DataFrame if pandas is installed)
|
|
69
|
+
client.leaderboard(42)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Auth & scopes
|
|
73
|
+
|
|
74
|
+
The token's *scope* segment dictates which routes you can call:
|
|
75
|
+
|
|
76
|
+
- `mlk_user_…` — submit agents, check status, manage your own attachments.
|
|
77
|
+
- `mlk_creator_…` — create / update competitions you own.
|
|
78
|
+
- `mlk_teacher_…` — create academic courses.
|
|
79
|
+
|
|
80
|
+
A `user`-scope token cannot call a `creator`-required route (and vice versa). Mint scope-specific tokens from your Profile page.
|
|
81
|
+
|
|
82
|
+
## API reference
|
|
83
|
+
|
|
84
|
+
### `mlarena.connect(api_key, base_url="https://ml-arena.com")`
|
|
85
|
+
|
|
86
|
+
Create a client. `api_key` must be the full `mlk_<scope>_<lookup>_<secret>` token.
|
|
87
|
+
|
|
88
|
+
### Agents (user scope)
|
|
89
|
+
|
|
90
|
+
- `client.submit(competition_id, agent=None, files=None, agent_name=None, runtime_id=None, runtime=None)` — one-shot create + (pick runner) + upload + deploy.
|
|
91
|
+
- `client.create_attached_agent(competition_id, agent_name, copy_from_agent_id=None)`
|
|
92
|
+
- `client.upload_agent_file(competition_id, attache_agent_id, file_path)` — multipart upload from disk.
|
|
93
|
+
- `client.update_agent_file_content(competition_id, attache_agent_id, filename, content)` — upload from a string (template render → upload).
|
|
94
|
+
- `client.list_agent_files(competition_id, attache_agent_id)` — list files with their content / binary marker.
|
|
95
|
+
- `client.get_agent_file_content(competition_id, attache_agent_id, filename)` — fetch one file's text.
|
|
96
|
+
- `client.delete_agent_file(competition_id, attache_agent_id, filename)`
|
|
97
|
+
- `client.deploy_agent(competition_id, attache_agent_id)`
|
|
98
|
+
- `client.delete_agent(competition_id, attache_agent_id)`
|
|
99
|
+
- `client.agent_status(competition_id, attache_agent_id)` — rich status (queue, runs, errors).
|
|
100
|
+
- `client.agent_deploy_status(competition_id, attache_agent_id)` — deploy quotas + last deploy.
|
|
101
|
+
- `client.agent_games(attache_agent_id)` — recent games with signed log URLs (60-day GCS retention).
|
|
102
|
+
- `client.tail_logs(competition_id, attache_agent_id, follow=False, poll_sec=5.0)` — generator of status / run lines.
|
|
103
|
+
- `client.status(agent_id=None, competition_id=None)` — defaults to the last submission.
|
|
104
|
+
|
|
105
|
+
### Runners (DockerImageAgentRuntime, user scope)
|
|
106
|
+
|
|
107
|
+
- `client.runtime_options(competition_id)` — list runtimes compatible with the competition.
|
|
108
|
+
- `client.agent_runtime(attache_agent_id)` — read the runtime currently pinned to an agent.
|
|
109
|
+
- `client.set_agent_runtime(attache_agent_id, runtime_id)` — pin a runtime by id.
|
|
110
|
+
- `client.resolve_runtime(competition_id, language=None, framework=None, framework_version=None)` — resolve a (lang, framework, version) spec to one runtime row.
|
|
111
|
+
|
|
112
|
+
## Full participant workflow
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
import mlarena, requests
|
|
116
|
+
|
|
117
|
+
c = mlarena.connect("mlk_user_…", base_url="http://localhost:5000")
|
|
118
|
+
|
|
119
|
+
cid = 42 # competition id
|
|
120
|
+
|
|
121
|
+
# 1. Pick a runner (language × framework)
|
|
122
|
+
runtimes = c.runtime_options(cid)
|
|
123
|
+
py_gym = c.resolve_runtime(cid, language="python", framework="gymnasium")
|
|
124
|
+
|
|
125
|
+
# 2. Create the agent + pin runner + upload files + deploy in one call
|
|
126
|
+
sub = c.submit(cid, files=["agent.py", "model.pkl"], runtime_id=py_gym["id"])
|
|
127
|
+
aid = sub["attache_agent_id"]
|
|
128
|
+
|
|
129
|
+
# 3. Inspect / edit a file in place after the initial upload
|
|
130
|
+
src = c.get_agent_file_content(cid, aid, "agent.py")
|
|
131
|
+
c.update_agent_file_content(cid, aid, "agent.py", src.replace("epsilon=0.1", "epsilon=0.05"))
|
|
132
|
+
c.deploy_agent(cid, aid) # redeploy after edit
|
|
133
|
+
|
|
134
|
+
# 4. Watch status / run progress until terminal
|
|
135
|
+
for line in c.tail_logs(cid, aid):
|
|
136
|
+
print(line)
|
|
137
|
+
|
|
138
|
+
# 5. Pull stdout from completed games via signed URLs (60d retention)
|
|
139
|
+
for game in c.agent_games(aid)["games"]:
|
|
140
|
+
if game["signed_url"]:
|
|
141
|
+
print(requests.get(game["signed_url"]).text)
|
|
142
|
+
|
|
143
|
+
# 6. Read the leaderboard
|
|
144
|
+
print(c.leaderboard(cid).head())
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Competitions
|
|
148
|
+
|
|
149
|
+
- `client.competitions()` — public list.
|
|
150
|
+
- `client.create_competition(name, kernel_version, description=None, copy_from_competition_id=None, tag_names=None)` — creator scope. The backend resolves the engine + default evaluation + default env runtime from `kernel_version`. Pass `tag_names=["rl", "research"]` to attach tags at creation time; unknown names raise `MLArenaError`.
|
|
151
|
+
- `client.list_tags()` — public read of the tag catalog.
|
|
152
|
+
- `client.set_competition_tags(competition_id, tag_names=None, tag_ids=None)` — creator scope. Replaces the tag set on a competition you own; pass `[]` to clear all tags.
|
|
153
|
+
|
|
154
|
+
### Academic courses
|
|
155
|
+
|
|
156
|
+
- `client.create_course(name, code, start_date, end_date, instructor_name=None, competition_id=None, enrollment_link=None)` — teacher scope.
|
|
157
|
+
- `client.enroll_in_course(enrollment_link, student_email=None, student_number=None, project_url=None)` — user scope.
|
|
158
|
+
|
|
159
|
+
### Leaderboard
|
|
160
|
+
|
|
161
|
+
- `client.leaderboard(competition_id=None)` — defaults to last competition; returns DataFrame if pandas is installed.
|
|
162
|
+
|
|
163
|
+
## Get your API key
|
|
164
|
+
|
|
165
|
+
1. Go to [ml-arena.com](https://ml-arena.com).
|
|
166
|
+
2. Open your Profile page.
|
|
167
|
+
3. Mint a key for the scope you need (`user`, `creator`, or `teacher`).
|
|
168
|
+
4. Copy the full token (shown once) — it starts with `mlk_`.
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# mlarena
|
|
2
|
+
|
|
3
|
+
Python SDK for [ML Arena](https://ml-arena.com) — submit agents, manage competitions, manage courses, and read leaderboards from any notebook or IDE.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install mlarena
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
import mlarena
|
|
15
|
+
|
|
16
|
+
# Connect with your API key (Profile page → "API Keys"). The token is the full
|
|
17
|
+
# string starting with `mlk_…`, not a `key_id:key_pass` pair.
|
|
18
|
+
client = mlarena.connect(api_key="mlk_user_a1b2c3d4_<32-hex-secret>")
|
|
19
|
+
|
|
20
|
+
# List competitions (public, no auth)
|
|
21
|
+
client.competitions()
|
|
22
|
+
|
|
23
|
+
# Submit an agent class — creates an attachment, uploads, and deploys.
|
|
24
|
+
class MyAgent:
|
|
25
|
+
def predict(self, observation):
|
|
26
|
+
return 0
|
|
27
|
+
|
|
28
|
+
result = client.submit(competition_id=42, agent=MyAgent)
|
|
29
|
+
|
|
30
|
+
# Or submit files from disk
|
|
31
|
+
client.submit(competition_id=42, files=["agent.py", "model.pkl"])
|
|
32
|
+
|
|
33
|
+
# Check status of the last submission
|
|
34
|
+
client.status()
|
|
35
|
+
|
|
36
|
+
# View leaderboard (returns DataFrame if pandas is installed)
|
|
37
|
+
client.leaderboard(42)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Auth & scopes
|
|
41
|
+
|
|
42
|
+
The token's *scope* segment dictates which routes you can call:
|
|
43
|
+
|
|
44
|
+
- `mlk_user_…` — submit agents, check status, manage your own attachments.
|
|
45
|
+
- `mlk_creator_…` — create / update competitions you own.
|
|
46
|
+
- `mlk_teacher_…` — create academic courses.
|
|
47
|
+
|
|
48
|
+
A `user`-scope token cannot call a `creator`-required route (and vice versa). Mint scope-specific tokens from your Profile page.
|
|
49
|
+
|
|
50
|
+
## API reference
|
|
51
|
+
|
|
52
|
+
### `mlarena.connect(api_key, base_url="https://ml-arena.com")`
|
|
53
|
+
|
|
54
|
+
Create a client. `api_key` must be the full `mlk_<scope>_<lookup>_<secret>` token.
|
|
55
|
+
|
|
56
|
+
### Agents (user scope)
|
|
57
|
+
|
|
58
|
+
- `client.submit(competition_id, agent=None, files=None, agent_name=None, runtime_id=None, runtime=None)` — one-shot create + (pick runner) + upload + deploy.
|
|
59
|
+
- `client.create_attached_agent(competition_id, agent_name, copy_from_agent_id=None)`
|
|
60
|
+
- `client.upload_agent_file(competition_id, attache_agent_id, file_path)` — multipart upload from disk.
|
|
61
|
+
- `client.update_agent_file_content(competition_id, attache_agent_id, filename, content)` — upload from a string (template render → upload).
|
|
62
|
+
- `client.list_agent_files(competition_id, attache_agent_id)` — list files with their content / binary marker.
|
|
63
|
+
- `client.get_agent_file_content(competition_id, attache_agent_id, filename)` — fetch one file's text.
|
|
64
|
+
- `client.delete_agent_file(competition_id, attache_agent_id, filename)`
|
|
65
|
+
- `client.deploy_agent(competition_id, attache_agent_id)`
|
|
66
|
+
- `client.delete_agent(competition_id, attache_agent_id)`
|
|
67
|
+
- `client.agent_status(competition_id, attache_agent_id)` — rich status (queue, runs, errors).
|
|
68
|
+
- `client.agent_deploy_status(competition_id, attache_agent_id)` — deploy quotas + last deploy.
|
|
69
|
+
- `client.agent_games(attache_agent_id)` — recent games with signed log URLs (60-day GCS retention).
|
|
70
|
+
- `client.tail_logs(competition_id, attache_agent_id, follow=False, poll_sec=5.0)` — generator of status / run lines.
|
|
71
|
+
- `client.status(agent_id=None, competition_id=None)` — defaults to the last submission.
|
|
72
|
+
|
|
73
|
+
### Runners (DockerImageAgentRuntime, user scope)
|
|
74
|
+
|
|
75
|
+
- `client.runtime_options(competition_id)` — list runtimes compatible with the competition.
|
|
76
|
+
- `client.agent_runtime(attache_agent_id)` — read the runtime currently pinned to an agent.
|
|
77
|
+
- `client.set_agent_runtime(attache_agent_id, runtime_id)` — pin a runtime by id.
|
|
78
|
+
- `client.resolve_runtime(competition_id, language=None, framework=None, framework_version=None)` — resolve a (lang, framework, version) spec to one runtime row.
|
|
79
|
+
|
|
80
|
+
## Full participant workflow
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
import mlarena, requests
|
|
84
|
+
|
|
85
|
+
c = mlarena.connect("mlk_user_…", base_url="http://localhost:5000")
|
|
86
|
+
|
|
87
|
+
cid = 42 # competition id
|
|
88
|
+
|
|
89
|
+
# 1. Pick a runner (language × framework)
|
|
90
|
+
runtimes = c.runtime_options(cid)
|
|
91
|
+
py_gym = c.resolve_runtime(cid, language="python", framework="gymnasium")
|
|
92
|
+
|
|
93
|
+
# 2. Create the agent + pin runner + upload files + deploy in one call
|
|
94
|
+
sub = c.submit(cid, files=["agent.py", "model.pkl"], runtime_id=py_gym["id"])
|
|
95
|
+
aid = sub["attache_agent_id"]
|
|
96
|
+
|
|
97
|
+
# 3. Inspect / edit a file in place after the initial upload
|
|
98
|
+
src = c.get_agent_file_content(cid, aid, "agent.py")
|
|
99
|
+
c.update_agent_file_content(cid, aid, "agent.py", src.replace("epsilon=0.1", "epsilon=0.05"))
|
|
100
|
+
c.deploy_agent(cid, aid) # redeploy after edit
|
|
101
|
+
|
|
102
|
+
# 4. Watch status / run progress until terminal
|
|
103
|
+
for line in c.tail_logs(cid, aid):
|
|
104
|
+
print(line)
|
|
105
|
+
|
|
106
|
+
# 5. Pull stdout from completed games via signed URLs (60d retention)
|
|
107
|
+
for game in c.agent_games(aid)["games"]:
|
|
108
|
+
if game["signed_url"]:
|
|
109
|
+
print(requests.get(game["signed_url"]).text)
|
|
110
|
+
|
|
111
|
+
# 6. Read the leaderboard
|
|
112
|
+
print(c.leaderboard(cid).head())
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Competitions
|
|
116
|
+
|
|
117
|
+
- `client.competitions()` — public list.
|
|
118
|
+
- `client.create_competition(name, kernel_version, description=None, copy_from_competition_id=None, tag_names=None)` — creator scope. The backend resolves the engine + default evaluation + default env runtime from `kernel_version`. Pass `tag_names=["rl", "research"]` to attach tags at creation time; unknown names raise `MLArenaError`.
|
|
119
|
+
- `client.list_tags()` — public read of the tag catalog.
|
|
120
|
+
- `client.set_competition_tags(competition_id, tag_names=None, tag_ids=None)` — creator scope. Replaces the tag set on a competition you own; pass `[]` to clear all tags.
|
|
121
|
+
|
|
122
|
+
### Academic courses
|
|
123
|
+
|
|
124
|
+
- `client.create_course(name, code, start_date, end_date, instructor_name=None, competition_id=None, enrollment_link=None)` — teacher scope.
|
|
125
|
+
- `client.enroll_in_course(enrollment_link, student_email=None, student_number=None, project_url=None)` — user scope.
|
|
126
|
+
|
|
127
|
+
### Leaderboard
|
|
128
|
+
|
|
129
|
+
- `client.leaderboard(competition_id=None)` — defaults to last competition; returns DataFrame if pandas is installed.
|
|
130
|
+
|
|
131
|
+
## Get your API key
|
|
132
|
+
|
|
133
|
+
1. Go to [ml-arena.com](https://ml-arena.com).
|
|
134
|
+
2. Open your Profile page.
|
|
135
|
+
3. Mint a key for the scope you need (`user`, `creator`, or `teacher`).
|
|
136
|
+
4. Copy the full token (shown once) — it starts with `mlk_`.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from urllib.parse import urlsplit
|
|
2
|
+
|
|
3
|
+
from mlarena.client import MLArenaClient
|
|
4
|
+
from mlarena.exceptions import (
|
|
5
|
+
AuthenticationError,
|
|
6
|
+
CompetitionNotFoundError,
|
|
7
|
+
MLArenaError,
|
|
8
|
+
SubmissionError,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
__version__ = "0.2.0"
|
|
12
|
+
|
|
13
|
+
_LOCAL_HOSTS = frozenset({"localhost", "127.0.0.1", "::1"})
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def connect(
|
|
17
|
+
api_key: str,
|
|
18
|
+
base_url: str = "https://ml-arena.com",
|
|
19
|
+
*,
|
|
20
|
+
allow_insecure: bool = False,
|
|
21
|
+
) -> MLArenaClient:
|
|
22
|
+
"""Connect to ML Arena.
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
api_key: Full bearer token in the canonical form
|
|
26
|
+
``mlk_<scope>_<lookup>_<secret>`` (copy it from your Profile page).
|
|
27
|
+
Older `key_id:key_pass` colon-separated form is no longer supported.
|
|
28
|
+
base_url: ML Arena server URL.
|
|
29
|
+
allow_insecure: Opt-in escape hatch to allow ``http://`` URLs against
|
|
30
|
+
non-loopback hosts. ``http://localhost`` and ``http://127.0.0.1``
|
|
31
|
+
are always allowed without this flag (local dev). Default
|
|
32
|
+
``False`` because the bearer token is sent on every request and
|
|
33
|
+
cleartext transport would expose it to anyone on the network path.
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
MLArenaClient instance.
|
|
37
|
+
|
|
38
|
+
Raises:
|
|
39
|
+
AuthenticationError: If the token doesn't look like a real `mlk_…`
|
|
40
|
+
token. The backend ultimately decides authenticity on the first
|
|
41
|
+
authenticated request.
|
|
42
|
+
"""
|
|
43
|
+
if not api_key or not api_key.startswith("mlk_"):
|
|
44
|
+
raise AuthenticationError(
|
|
45
|
+
"Invalid api_key format. Expected a token of the form "
|
|
46
|
+
"'mlk_<scope>_<lookup>_<secret>' (copy it from your Profile page)."
|
|
47
|
+
)
|
|
48
|
+
parts = api_key.split("_", 3)
|
|
49
|
+
if len(parts) != 4 or any(not part for part in parts):
|
|
50
|
+
raise AuthenticationError(
|
|
51
|
+
"Invalid api_key shape. Expected exactly 4 underscore-separated parts "
|
|
52
|
+
"(prefix, scope, lookup, secret)."
|
|
53
|
+
)
|
|
54
|
+
parsed = urlsplit(base_url)
|
|
55
|
+
if parsed.scheme != "https":
|
|
56
|
+
is_local = parsed.hostname in _LOCAL_HOSTS
|
|
57
|
+
if not is_local and not allow_insecure:
|
|
58
|
+
raise AuthenticationError(
|
|
59
|
+
f"Refusing to send bearer token over '{parsed.scheme}://'. "
|
|
60
|
+
"Use an https:// base_url, or pass allow_insecure=True if you "
|
|
61
|
+
"are pointing at a non-public dev server you control."
|
|
62
|
+
)
|
|
63
|
+
return MLArenaClient(token=api_key, base_url=base_url)
|