margen 0.1.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.
- margen-0.1.0/LICENSE +21 -0
- margen-0.1.0/PKG-INFO +562 -0
- margen-0.1.0/README.md +547 -0
- margen-0.1.0/pyproject.toml +54 -0
- margen-0.1.0/setup.cfg +4 -0
- margen-0.1.0/src/margen/__init__.py +17 -0
- margen-0.1.0/src/margen/_hooks/__init__.py +4 -0
- margen-0.1.0/src/margen/_hooks/sdkhooks.py +74 -0
- margen-0.1.0/src/margen/_hooks/types.py +124 -0
- margen-0.1.0/src/margen/_version.py +15 -0
- margen-0.1.0/src/margen/basesdk.py +392 -0
- margen-0.1.0/src/margen/errors/__init__.py +39 -0
- margen-0.1.0/src/margen/errors/error.py +38 -0
- margen-0.1.0/src/margen/errors/margendefaulterror.py +40 -0
- margen-0.1.0/src/margen/errors/margenerror.py +30 -0
- margen-0.1.0/src/margen/errors/no_response_error.py +17 -0
- margen-0.1.0/src/margen/errors/responsevalidationerror.py +27 -0
- margen-0.1.0/src/margen/httpclient.py +125 -0
- margen-0.1.0/src/margen/models/__init__.py +122 -0
- margen-0.1.0/src/margen/models/attackdataitem.py +135 -0
- margen-0.1.0/src/margen/models/benchmark.py +71 -0
- margen-0.1.0/src/margen/models/benchmarklist.py +59 -0
- margen-0.1.0/src/margen/models/catalog.py +71 -0
- margen-0.1.0/src/margen/models/catalogdimension.py +63 -0
- margen-0.1.0/src/margen/models/dimensionvalue.py +34 -0
- margen-0.1.0/src/margen/models/download.py +71 -0
- margen-0.1.0/src/margen/models/downloaditemop.py +48 -0
- margen-0.1.0/src/margen/models/error.py +56 -0
- margen-0.1.0/src/margen/models/getcatalogop.py +37 -0
- margen-0.1.0/src/margen/models/itemlist.py +135 -0
- margen-0.1.0/src/margen/models/listitemsop.py +174 -0
- margen-0.1.0/src/margen/models/security.py +42 -0
- margen-0.1.0/src/margen/models/usage.py +63 -0
- margen-0.1.0/src/margen/py.typed +1 -0
- margen-0.1.0/src/margen/sdk.py +1220 -0
- margen-0.1.0/src/margen/sdkconfiguration.py +51 -0
- margen-0.1.0/src/margen/types/__init__.py +24 -0
- margen-0.1.0/src/margen/types/base64fileinput.py +43 -0
- margen-0.1.0/src/margen/types/basemodel.py +77 -0
- margen-0.1.0/src/margen/utils/__init__.py +178 -0
- margen-0.1.0/src/margen/utils/annotations.py +79 -0
- margen-0.1.0/src/margen/utils/datetimes.py +23 -0
- margen-0.1.0/src/margen/utils/dynamic_imports.py +54 -0
- margen-0.1.0/src/margen/utils/enums.py +134 -0
- margen-0.1.0/src/margen/utils/eventstreaming.py +326 -0
- margen-0.1.0/src/margen/utils/forms.py +239 -0
- margen-0.1.0/src/margen/utils/headers.py +136 -0
- margen-0.1.0/src/margen/utils/logger.py +27 -0
- margen-0.1.0/src/margen/utils/metadata.py +119 -0
- margen-0.1.0/src/margen/utils/queryparams.py +217 -0
- margen-0.1.0/src/margen/utils/requestbodies.py +67 -0
- margen-0.1.0/src/margen/utils/retries.py +356 -0
- margen-0.1.0/src/margen/utils/security.py +215 -0
- margen-0.1.0/src/margen/utils/serializers.py +306 -0
- margen-0.1.0/src/margen/utils/unmarshal_json_response.py +38 -0
- margen-0.1.0/src/margen/utils/url.py +155 -0
- margen-0.1.0/src/margen/utils/values.py +137 -0
- margen-0.1.0/src/margen.egg-info/PKG-INFO +562 -0
- margen-0.1.0/src/margen.egg-info/SOURCES.txt +60 -0
- margen-0.1.0/src/margen.egg-info/dependency_links.txt +1 -0
- margen-0.1.0/src/margen.egg-info/requires.txt +4 -0
- margen-0.1.0/src/margen.egg-info/top_level.txt +1 -0
margen-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 danbabalola
|
|
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.
|
margen-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: margen
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python Client SDK Generated by Speakeasy.
|
|
5
|
+
Author: Speakeasy
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: httpcore>=1.0.9
|
|
11
|
+
Requires-Dist: httpx>=0.28.1
|
|
12
|
+
Requires-Dist: jsonpath-python>=1.0.6
|
|
13
|
+
Requires-Dist: pydantic<2.13,>=2.11.2
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# margen
|
|
17
|
+
|
|
18
|
+
Developer-friendly & type-safe Python SDK specifically catered to leverage *margen* API.
|
|
19
|
+
|
|
20
|
+
[](https://www.speakeasy.com/?utm_source=margen&utm_campaign=python)
|
|
21
|
+
[](https://opensource.org/licenses/MIT)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
<br /><br />
|
|
25
|
+
> [!IMPORTANT]
|
|
26
|
+
> This SDK is not yet ready for production use. To complete setup please follow the steps outlined in your [workspace](https://app.speakeasy.com/org/margen/margen-api). Delete this section before > publishing to a package manager.
|
|
27
|
+
|
|
28
|
+
<!-- Start Summary [summary] -->
|
|
29
|
+
## Summary
|
|
30
|
+
|
|
31
|
+
Margen Attack-Data API: Credit-metered API that delivers labeled deepfake attack-data (real vs AI-generated face images and their platform-perturbed variants). Data is organized into benchmarks (versioned datasets, e.g. synthetic-face-v1), each with its own queryable dimensions. Reverse-engineered from the live b2b-website routes and the proven scripts/data-api/margen_client.py; this spec is the source of truth for the generated SDKs. Auth: Bearer token (or x-api-key). Pull one image per credit; test keys pull a free fixed sample. The canonical path prefix is /api/v1/data; the unversioned /api/data prefix remains a permanent alias.
|
|
32
|
+
<!-- End Summary [summary] -->
|
|
33
|
+
|
|
34
|
+
<!-- Start Table of Contents [toc] -->
|
|
35
|
+
## Table of Contents
|
|
36
|
+
<!-- $toc-max-depth=2 -->
|
|
37
|
+
* [margen](#margen)
|
|
38
|
+
* [SDK Installation](#sdk-installation)
|
|
39
|
+
* [IDE Support](#ide-support)
|
|
40
|
+
* [SDK Example Usage](#sdk-example-usage)
|
|
41
|
+
* [Authentication](#authentication)
|
|
42
|
+
* [Available Resources and Operations](#available-resources-and-operations)
|
|
43
|
+
* [Pagination](#pagination)
|
|
44
|
+
* [Retries](#retries)
|
|
45
|
+
* [Error Handling](#error-handling)
|
|
46
|
+
* [Server Selection](#server-selection)
|
|
47
|
+
* [Custom HTTP Client](#custom-http-client)
|
|
48
|
+
* [Resource Management](#resource-management)
|
|
49
|
+
* [Debugging](#debugging)
|
|
50
|
+
* [Development](#development)
|
|
51
|
+
* [Maturity](#maturity)
|
|
52
|
+
* [Contributions](#contributions)
|
|
53
|
+
|
|
54
|
+
<!-- End Table of Contents [toc] -->
|
|
55
|
+
|
|
56
|
+
<!-- Start SDK Installation [installation] -->
|
|
57
|
+
## SDK Installation
|
|
58
|
+
|
|
59
|
+
> [!TIP]
|
|
60
|
+
> To finish publishing your SDK to PyPI you must [run your first generation action](https://www.speakeasy.com/docs/github-setup#step-by-step-guide).
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
> [!NOTE]
|
|
64
|
+
> **Python version upgrade policy**
|
|
65
|
+
>
|
|
66
|
+
> Once a Python version reaches its [official end of life date](https://devguide.python.org/versions/), a 3-month grace period is provided for users to upgrade. Following this grace period, the minimum python version supported in the SDK will be updated.
|
|
67
|
+
|
|
68
|
+
The SDK can be installed with *uv*, *pip*, or *poetry* package managers.
|
|
69
|
+
|
|
70
|
+
### uv
|
|
71
|
+
|
|
72
|
+
*uv* is a fast Python package installer and resolver, designed as a drop-in replacement for pip and pip-tools. It's recommended for its speed and modern Python tooling capabilities.
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
uv add git+<UNSET>.git
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### PIP
|
|
79
|
+
|
|
80
|
+
*PIP* is the default package installer for Python, enabling easy installation and management of packages from PyPI via the command line.
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
pip install git+<UNSET>.git
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Poetry
|
|
87
|
+
|
|
88
|
+
*Poetry* is a modern tool that simplifies dependency management and package publishing by using a single `pyproject.toml` file to handle project metadata and dependencies.
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
poetry add git+<UNSET>.git
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Shell and script usage with `uv`
|
|
95
|
+
|
|
96
|
+
You can use this SDK in a Python shell with [uv](https://docs.astral.sh/uv/) and the `uvx` command that comes with it like so:
|
|
97
|
+
|
|
98
|
+
```shell
|
|
99
|
+
uvx --from margen python
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
It's also possible to write a standalone Python script without needing to set up a whole project like so:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
#!/usr/bin/env -S uv run --script
|
|
106
|
+
# /// script
|
|
107
|
+
# requires-python = ">=3.10"
|
|
108
|
+
# dependencies = [
|
|
109
|
+
# "margen",
|
|
110
|
+
# ]
|
|
111
|
+
# ///
|
|
112
|
+
|
|
113
|
+
from margen import Margen
|
|
114
|
+
|
|
115
|
+
sdk = Margen(
|
|
116
|
+
# SDK arguments
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
# Rest of script here...
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Once that is saved to a file, you can run it with `uv run script.py` where
|
|
123
|
+
`script.py` can be replaced with the actual file name.
|
|
124
|
+
<!-- End SDK Installation [installation] -->
|
|
125
|
+
|
|
126
|
+
<!-- Start IDE Support [idesupport] -->
|
|
127
|
+
## IDE Support
|
|
128
|
+
|
|
129
|
+
### PyCharm
|
|
130
|
+
|
|
131
|
+
Generally, the SDK will work well with most IDEs out of the box. However, when using PyCharm, you can enjoy much better integration with Pydantic by installing an additional plugin.
|
|
132
|
+
|
|
133
|
+
- [PyCharm Pydantic Plugin](https://docs.pydantic.dev/latest/integrations/pycharm/)
|
|
134
|
+
<!-- End IDE Support [idesupport] -->
|
|
135
|
+
|
|
136
|
+
<!-- Start SDK Example Usage [usage] -->
|
|
137
|
+
## SDK Example Usage
|
|
138
|
+
|
|
139
|
+
### Example
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
# Synchronous Example
|
|
143
|
+
from margen import Margen
|
|
144
|
+
import os
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
with Margen(
|
|
148
|
+
bearer_auth=os.getenv("MARGEN_BEARER_AUTH", ""),
|
|
149
|
+
) as m_client:
|
|
150
|
+
|
|
151
|
+
res = m_client.list_benchmarks()
|
|
152
|
+
|
|
153
|
+
# Handle response
|
|
154
|
+
print(res)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
</br>
|
|
158
|
+
|
|
159
|
+
The same SDK client can also be used to make asynchronous requests by importing asyncio.
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
# Asynchronous Example
|
|
163
|
+
import asyncio
|
|
164
|
+
from margen import Margen
|
|
165
|
+
import os
|
|
166
|
+
|
|
167
|
+
async def main():
|
|
168
|
+
|
|
169
|
+
async with Margen(
|
|
170
|
+
bearer_auth=os.getenv("MARGEN_BEARER_AUTH", ""),
|
|
171
|
+
) as m_client:
|
|
172
|
+
|
|
173
|
+
res = await m_client.list_benchmarks_async()
|
|
174
|
+
|
|
175
|
+
# Handle response
|
|
176
|
+
print(res)
|
|
177
|
+
|
|
178
|
+
asyncio.run(main())
|
|
179
|
+
```
|
|
180
|
+
<!-- End SDK Example Usage [usage] -->
|
|
181
|
+
|
|
182
|
+
<!-- Start Authentication [security] -->
|
|
183
|
+
## Authentication
|
|
184
|
+
|
|
185
|
+
### Per-Client Security Schemes
|
|
186
|
+
|
|
187
|
+
This SDK supports the following security scheme globally:
|
|
188
|
+
|
|
189
|
+
| Name | Type | Scheme | Environment Variable |
|
|
190
|
+
| ------------- | ---- | ----------- | -------------------- |
|
|
191
|
+
| `bearer_auth` | http | HTTP Bearer | `MARGEN_BEARER_AUTH` |
|
|
192
|
+
|
|
193
|
+
To authenticate with the API the `bearer_auth` parameter must be set when initializing the SDK client instance. For example:
|
|
194
|
+
```python
|
|
195
|
+
from margen import Margen
|
|
196
|
+
import os
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
with Margen(
|
|
200
|
+
bearer_auth=os.getenv("MARGEN_BEARER_AUTH", ""),
|
|
201
|
+
) as m_client:
|
|
202
|
+
|
|
203
|
+
res = m_client.list_benchmarks()
|
|
204
|
+
|
|
205
|
+
# Handle response
|
|
206
|
+
print(res)
|
|
207
|
+
|
|
208
|
+
```
|
|
209
|
+
<!-- End Authentication [security] -->
|
|
210
|
+
|
|
211
|
+
<!-- Start Available Resources and Operations [operations] -->
|
|
212
|
+
## Available Resources and Operations
|
|
213
|
+
|
|
214
|
+
<details open>
|
|
215
|
+
<summary>Available methods</summary>
|
|
216
|
+
|
|
217
|
+
### [Margen SDK](docs/sdks/margen/README.md)
|
|
218
|
+
|
|
219
|
+
* [list_benchmarks](docs/sdks/margen/README.md#list_benchmarks) - List benchmarks the key can query
|
|
220
|
+
* [get_catalog](docs/sdks/margen/README.md#get_catalog) - Dimensions and allowed values for a benchmark
|
|
221
|
+
* [list_items](docs/sdks/margen/README.md#list_items) - Select items for a benchmark
|
|
222
|
+
* [download_item](docs/sdks/margen/README.md#download_item) - Get a signed URL for one item
|
|
223
|
+
* [get_usage](docs/sdks/margen/README.md#get_usage) - Current tier and credit balance
|
|
224
|
+
|
|
225
|
+
</details>
|
|
226
|
+
<!-- End Available Resources and Operations [operations] -->
|
|
227
|
+
|
|
228
|
+
<!-- Start Pagination [pagination] -->
|
|
229
|
+
## Pagination
|
|
230
|
+
|
|
231
|
+
Some of the endpoints in this SDK support pagination. To use pagination, you make your SDK calls as usual, but the
|
|
232
|
+
returned response object will have a `Next` method that can be called to pull down the next group of results. If the
|
|
233
|
+
return value of `Next` is `None`, then there are no more pages to be fetched.
|
|
234
|
+
|
|
235
|
+
Here's an example of one such pagination call:
|
|
236
|
+
```python
|
|
237
|
+
from margen import Margen
|
|
238
|
+
import os
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
with Margen(
|
|
242
|
+
bearer_auth=os.getenv("MARGEN_BEARER_AUTH", ""),
|
|
243
|
+
) as m_client:
|
|
244
|
+
|
|
245
|
+
res = m_client.list_items(limit=100, offset=0)
|
|
246
|
+
|
|
247
|
+
while res is not None:
|
|
248
|
+
# Handle items
|
|
249
|
+
|
|
250
|
+
res = res.next()
|
|
251
|
+
|
|
252
|
+
```
|
|
253
|
+
<!-- End Pagination [pagination] -->
|
|
254
|
+
|
|
255
|
+
<!-- Start Retries [retries] -->
|
|
256
|
+
## Retries
|
|
257
|
+
|
|
258
|
+
Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.
|
|
259
|
+
|
|
260
|
+
To change the default retry strategy for a single API call, simply provide a `RetryConfig` object to the call:
|
|
261
|
+
```python
|
|
262
|
+
from margen import Margen
|
|
263
|
+
from margen.utils import BackoffStrategy, RetryConfig
|
|
264
|
+
import os
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
with Margen(
|
|
268
|
+
bearer_auth=os.getenv("MARGEN_BEARER_AUTH", ""),
|
|
269
|
+
) as m_client:
|
|
270
|
+
|
|
271
|
+
res = m_client.list_benchmarks(,
|
|
272
|
+
RetryConfig("backoff", BackoffStrategy(1, 50, 1.1, 100), False))
|
|
273
|
+
|
|
274
|
+
# Handle response
|
|
275
|
+
print(res)
|
|
276
|
+
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
If you'd like to override the default retry strategy for all operations that support retries, you can use the `retry_config` optional parameter when initializing the SDK:
|
|
280
|
+
```python
|
|
281
|
+
from margen import Margen
|
|
282
|
+
from margen.utils import BackoffStrategy, RetryConfig
|
|
283
|
+
import os
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
with Margen(
|
|
287
|
+
retry_config=RetryConfig("backoff", BackoffStrategy(1, 50, 1.1, 100), False),
|
|
288
|
+
bearer_auth=os.getenv("MARGEN_BEARER_AUTH", ""),
|
|
289
|
+
) as m_client:
|
|
290
|
+
|
|
291
|
+
res = m_client.list_benchmarks()
|
|
292
|
+
|
|
293
|
+
# Handle response
|
|
294
|
+
print(res)
|
|
295
|
+
|
|
296
|
+
```
|
|
297
|
+
<!-- End Retries [retries] -->
|
|
298
|
+
|
|
299
|
+
<!-- Start Error Handling [errors] -->
|
|
300
|
+
## Error Handling
|
|
301
|
+
|
|
302
|
+
[`MargenError`](./src/margen/errors/margenerror.py) is the base class for all HTTP error responses. It has the following properties:
|
|
303
|
+
|
|
304
|
+
| Property | Type | Description |
|
|
305
|
+
| ------------------ | ---------------- | --------------------------------------------------------------------------------------- |
|
|
306
|
+
| `err.message` | `str` | Error message |
|
|
307
|
+
| `err.status_code` | `int` | HTTP response status code eg `404` |
|
|
308
|
+
| `err.headers` | `httpx.Headers` | HTTP response headers |
|
|
309
|
+
| `err.body` | `str` | HTTP body. Can be empty string if no body is returned. |
|
|
310
|
+
| `err.raw_response` | `httpx.Response` | Raw HTTP response |
|
|
311
|
+
| `err.data` | | Optional. Some errors may contain structured data. [See Error Classes](#error-classes). |
|
|
312
|
+
|
|
313
|
+
### Example
|
|
314
|
+
```python
|
|
315
|
+
from margen import Margen, errors
|
|
316
|
+
import os
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
with Margen(
|
|
320
|
+
bearer_auth=os.getenv("MARGEN_BEARER_AUTH", ""),
|
|
321
|
+
) as m_client:
|
|
322
|
+
res = None
|
|
323
|
+
try:
|
|
324
|
+
|
|
325
|
+
res = m_client.list_benchmarks()
|
|
326
|
+
|
|
327
|
+
# Handle response
|
|
328
|
+
print(res)
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
except errors.MargenError as e:
|
|
332
|
+
# The base class for HTTP error responses
|
|
333
|
+
print(e.message)
|
|
334
|
+
print(e.status_code)
|
|
335
|
+
print(e.body)
|
|
336
|
+
print(e.headers)
|
|
337
|
+
print(e.raw_response)
|
|
338
|
+
|
|
339
|
+
# Depending on the method different errors may be thrown
|
|
340
|
+
if isinstance(e, errors.Error):
|
|
341
|
+
print(e.data.error) # str
|
|
342
|
+
print(e.data.code) # models.Code
|
|
343
|
+
print(e.data.param) # Optional[str]
|
|
344
|
+
print(e.data.allowed) # Optional[List[str]]
|
|
345
|
+
print(e.data.available) # Optional[List[models.Available]]
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
### Error Classes
|
|
349
|
+
**Primary errors:**
|
|
350
|
+
* [`MargenError`](./src/margen/errors/margenerror.py): The base class for HTTP error responses.
|
|
351
|
+
* [`Error`](./src/margen/errors/error.py): Missing, invalid, or revoked API key.
|
|
352
|
+
|
|
353
|
+
<details><summary>Less common errors (5)</summary>
|
|
354
|
+
|
|
355
|
+
<br />
|
|
356
|
+
|
|
357
|
+
**Network errors:**
|
|
358
|
+
* [`httpx.RequestError`](https://www.python-httpx.org/exceptions/#httpx.RequestError): Base class for request errors.
|
|
359
|
+
* [`httpx.ConnectError`](https://www.python-httpx.org/exceptions/#httpx.ConnectError): HTTP client was unable to make a request to a server.
|
|
360
|
+
* [`httpx.TimeoutException`](https://www.python-httpx.org/exceptions/#httpx.TimeoutException): HTTP request timed out.
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
**Inherit from [`MargenError`](./src/margen/errors/margenerror.py)**:
|
|
364
|
+
* [`ResponseValidationError`](./src/margen/errors/responsevalidationerror.py): Type mismatch between the response data and the expected Pydantic model. Provides access to the Pydantic validation error via the `cause` attribute.
|
|
365
|
+
|
|
366
|
+
</details>
|
|
367
|
+
<!-- End Error Handling [errors] -->
|
|
368
|
+
|
|
369
|
+
<!-- Start Server Selection [server] -->
|
|
370
|
+
## Server Selection
|
|
371
|
+
|
|
372
|
+
### Select Server by Index
|
|
373
|
+
|
|
374
|
+
You can override the default server globally by passing a server index to the `server_idx: int` optional parameter when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the indexes associated with the available servers:
|
|
375
|
+
|
|
376
|
+
| # | Server | Description |
|
|
377
|
+
| --- | ---------------------------- | ----------- |
|
|
378
|
+
| 0 | `https://margensoftware.com` | Production |
|
|
379
|
+
| 1 | `http://localhost:3000` | Local dev |
|
|
380
|
+
|
|
381
|
+
#### Example
|
|
382
|
+
|
|
383
|
+
```python
|
|
384
|
+
from margen import Margen
|
|
385
|
+
import os
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
with Margen(
|
|
389
|
+
server_idx=0,
|
|
390
|
+
bearer_auth=os.getenv("MARGEN_BEARER_AUTH", ""),
|
|
391
|
+
) as m_client:
|
|
392
|
+
|
|
393
|
+
res = m_client.list_benchmarks()
|
|
394
|
+
|
|
395
|
+
# Handle response
|
|
396
|
+
print(res)
|
|
397
|
+
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
### Override Server URL Per-Client
|
|
401
|
+
|
|
402
|
+
The default server can also be overridden globally by passing a URL to the `server_url: str` optional parameter when initializing the SDK client instance. For example:
|
|
403
|
+
```python
|
|
404
|
+
from margen import Margen
|
|
405
|
+
import os
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
with Margen(
|
|
409
|
+
server_url="http://localhost:3000",
|
|
410
|
+
bearer_auth=os.getenv("MARGEN_BEARER_AUTH", ""),
|
|
411
|
+
) as m_client:
|
|
412
|
+
|
|
413
|
+
res = m_client.list_benchmarks()
|
|
414
|
+
|
|
415
|
+
# Handle response
|
|
416
|
+
print(res)
|
|
417
|
+
|
|
418
|
+
```
|
|
419
|
+
<!-- End Server Selection [server] -->
|
|
420
|
+
|
|
421
|
+
<!-- Start Custom HTTP Client [http-client] -->
|
|
422
|
+
## Custom HTTP Client
|
|
423
|
+
|
|
424
|
+
The Python SDK makes API calls using the [httpx](https://www.python-httpx.org/) HTTP library. In order to provide a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration, you can initialize the SDK client with your own HTTP client instance.
|
|
425
|
+
Depending on whether you are using the sync or async version of the SDK, you can pass an instance of `HttpClient` or `AsyncHttpClient` respectively, which are Protocol's ensuring that the client has the necessary methods to make API calls.
|
|
426
|
+
This allows you to wrap the client with your own custom logic, such as adding custom headers, logging, or error handling, or you can just pass an instance of `httpx.Client` or `httpx.AsyncClient` directly.
|
|
427
|
+
|
|
428
|
+
For example, you could specify a header for every request that this sdk makes as follows:
|
|
429
|
+
```python
|
|
430
|
+
from margen import Margen
|
|
431
|
+
import httpx
|
|
432
|
+
|
|
433
|
+
http_client = httpx.Client(headers={"x-custom-header": "someValue"})
|
|
434
|
+
s = Margen(client=http_client)
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
or you could wrap the client with your own custom logic:
|
|
438
|
+
```python
|
|
439
|
+
from margen import Margen
|
|
440
|
+
from margen.httpclient import AsyncHttpClient
|
|
441
|
+
import httpx
|
|
442
|
+
|
|
443
|
+
class CustomClient(AsyncHttpClient):
|
|
444
|
+
client: AsyncHttpClient
|
|
445
|
+
|
|
446
|
+
def __init__(self, client: AsyncHttpClient):
|
|
447
|
+
self.client = client
|
|
448
|
+
|
|
449
|
+
async def send(
|
|
450
|
+
self,
|
|
451
|
+
request: httpx.Request,
|
|
452
|
+
*,
|
|
453
|
+
stream: bool = False,
|
|
454
|
+
auth: Union[
|
|
455
|
+
httpx._types.AuthTypes, httpx._client.UseClientDefault, None
|
|
456
|
+
] = httpx.USE_CLIENT_DEFAULT,
|
|
457
|
+
follow_redirects: Union[
|
|
458
|
+
bool, httpx._client.UseClientDefault
|
|
459
|
+
] = httpx.USE_CLIENT_DEFAULT,
|
|
460
|
+
) -> httpx.Response:
|
|
461
|
+
request.headers["Client-Level-Header"] = "added by client"
|
|
462
|
+
|
|
463
|
+
return await self.client.send(
|
|
464
|
+
request, stream=stream, auth=auth, follow_redirects=follow_redirects
|
|
465
|
+
)
|
|
466
|
+
|
|
467
|
+
def build_request(
|
|
468
|
+
self,
|
|
469
|
+
method: str,
|
|
470
|
+
url: httpx._types.URLTypes,
|
|
471
|
+
*,
|
|
472
|
+
content: Optional[httpx._types.RequestContent] = None,
|
|
473
|
+
data: Optional[httpx._types.RequestData] = None,
|
|
474
|
+
files: Optional[httpx._types.RequestFiles] = None,
|
|
475
|
+
json: Optional[Any] = None,
|
|
476
|
+
params: Optional[httpx._types.QueryParamTypes] = None,
|
|
477
|
+
headers: Optional[httpx._types.HeaderTypes] = None,
|
|
478
|
+
cookies: Optional[httpx._types.CookieTypes] = None,
|
|
479
|
+
timeout: Union[
|
|
480
|
+
httpx._types.TimeoutTypes, httpx._client.UseClientDefault
|
|
481
|
+
] = httpx.USE_CLIENT_DEFAULT,
|
|
482
|
+
extensions: Optional[httpx._types.RequestExtensions] = None,
|
|
483
|
+
) -> httpx.Request:
|
|
484
|
+
return self.client.build_request(
|
|
485
|
+
method,
|
|
486
|
+
url,
|
|
487
|
+
content=content,
|
|
488
|
+
data=data,
|
|
489
|
+
files=files,
|
|
490
|
+
json=json,
|
|
491
|
+
params=params,
|
|
492
|
+
headers=headers,
|
|
493
|
+
cookies=cookies,
|
|
494
|
+
timeout=timeout,
|
|
495
|
+
extensions=extensions,
|
|
496
|
+
)
|
|
497
|
+
|
|
498
|
+
s = Margen(async_client=CustomClient(httpx.AsyncClient()))
|
|
499
|
+
```
|
|
500
|
+
<!-- End Custom HTTP Client [http-client] -->
|
|
501
|
+
|
|
502
|
+
<!-- Start Resource Management [resource-management] -->
|
|
503
|
+
## Resource Management
|
|
504
|
+
|
|
505
|
+
The `Margen` class implements the context manager protocol and registers a finalizer function to close the underlying sync and async HTTPX clients it uses under the hood. This will close HTTP connections, release memory and free up other resources held by the SDK. In short-lived Python programs and notebooks that make a few SDK method calls, resource management may not be a concern. However, in longer-lived programs, it is beneficial to create a single SDK instance via a [context manager][context-manager] and reuse it across the application.
|
|
506
|
+
|
|
507
|
+
[context-manager]: https://docs.python.org/3/reference/datamodel.html#context-managers
|
|
508
|
+
|
|
509
|
+
```python
|
|
510
|
+
from margen import Margen
|
|
511
|
+
import os
|
|
512
|
+
def main():
|
|
513
|
+
|
|
514
|
+
with Margen(
|
|
515
|
+
bearer_auth=os.getenv("MARGEN_BEARER_AUTH", ""),
|
|
516
|
+
) as m_client:
|
|
517
|
+
# Rest of application here...
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
# Or when using async:
|
|
521
|
+
async def amain():
|
|
522
|
+
|
|
523
|
+
async with Margen(
|
|
524
|
+
bearer_auth=os.getenv("MARGEN_BEARER_AUTH", ""),
|
|
525
|
+
) as m_client:
|
|
526
|
+
# Rest of application here...
|
|
527
|
+
```
|
|
528
|
+
<!-- End Resource Management [resource-management] -->
|
|
529
|
+
|
|
530
|
+
<!-- Start Debugging [debug] -->
|
|
531
|
+
## Debugging
|
|
532
|
+
|
|
533
|
+
You can setup your SDK to emit debug logs for SDK requests and responses.
|
|
534
|
+
|
|
535
|
+
You can pass your own logger class directly into your SDK.
|
|
536
|
+
```python
|
|
537
|
+
from margen import Margen
|
|
538
|
+
import logging
|
|
539
|
+
|
|
540
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
541
|
+
s = Margen(debug_logger=logging.getLogger("margen"))
|
|
542
|
+
```
|
|
543
|
+
|
|
544
|
+
You can also enable a default debug logger by setting an environment variable `MARGEN_DEBUG` to true.
|
|
545
|
+
<!-- End Debugging [debug] -->
|
|
546
|
+
|
|
547
|
+
<!-- Placeholder for Future Speakeasy SDK Sections -->
|
|
548
|
+
|
|
549
|
+
# Development
|
|
550
|
+
|
|
551
|
+
## Maturity
|
|
552
|
+
|
|
553
|
+
This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage
|
|
554
|
+
to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally
|
|
555
|
+
looking for the latest version.
|
|
556
|
+
|
|
557
|
+
## Contributions
|
|
558
|
+
|
|
559
|
+
While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation.
|
|
560
|
+
We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.
|
|
561
|
+
|
|
562
|
+
### SDK Created by [Speakeasy](https://www.speakeasy.com/?utm_source=margen&utm_campaign=python)
|