codeset 0.4.4__py3-none-any.whl
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.
- codeset/__init__.py +92 -0
- codeset/_base_client.py +2001 -0
- codeset/_client.py +572 -0
- codeset/_compat.py +219 -0
- codeset/_constants.py +14 -0
- codeset/_exceptions.py +108 -0
- codeset/_files.py +123 -0
- codeset/_models.py +857 -0
- codeset/_qs.py +150 -0
- codeset/_resource.py +43 -0
- codeset/_response.py +830 -0
- codeset/_streaming.py +333 -0
- codeset/_types.py +261 -0
- codeset/_utils/__init__.py +66 -0
- codeset/_utils/_compat.py +45 -0
- codeset/_utils/_datetime_parse.py +136 -0
- codeset/_utils/_logs.py +25 -0
- codeset/_utils/_proxy.py +65 -0
- codeset/_utils/_reflection.py +42 -0
- codeset/_utils/_resources_proxy.py +24 -0
- codeset/_utils/_streams.py +12 -0
- codeset/_utils/_sync.py +58 -0
- codeset/_utils/_transform.py +457 -0
- codeset/_utils/_typing.py +156 -0
- codeset/_utils/_utils.py +474 -0
- codeset/_version.py +4 -0
- codeset/lib/.keep +4 -0
- codeset/py.typed +0 -0
- codeset/resources/__init__.py +61 -0
- codeset/resources/datasets.py +135 -0
- codeset/resources/health.py +135 -0
- codeset/resources/samples.py +303 -0
- codeset/resources/sessions/__init__.py +33 -0
- codeset/resources/sessions/sessions.py +891 -0
- codeset/resources/sessions/verify.py +335 -0
- codeset/types/__init__.py +22 -0
- codeset/types/container_info.py +32 -0
- codeset/types/dataset_list_response.py +28 -0
- codeset/types/error_info.py +15 -0
- codeset/types/health_check_response.py +21 -0
- codeset/types/interaction.py +37 -0
- codeset/types/interaction_status.py +7 -0
- codeset/types/sample_download_params.py +14 -0
- codeset/types/sample_list_params.py +22 -0
- codeset/types/sample_list_response.py +95 -0
- codeset/types/session.py +48 -0
- codeset/types/session_close_response.py +15 -0
- codeset/types/session_create_params.py +18 -0
- codeset/types/session_create_response.py +21 -0
- codeset/types/session_execute_command_params.py +15 -0
- codeset/types/session_execute_command_response.py +15 -0
- codeset/types/session_list_response.py +20 -0
- codeset/types/session_status.py +7 -0
- codeset/types/session_str_replace_params.py +18 -0
- codeset/types/session_str_replace_response.py +15 -0
- codeset/types/sessions/__init__.py +7 -0
- codeset/types/sessions/job_status.py +7 -0
- codeset/types/sessions/verify_start_response.py +21 -0
- codeset/types/sessions/verify_status_response.py +79 -0
- codeset-0.4.4.dist-info/METADATA +399 -0
- codeset-0.4.4.dist-info/RECORD +63 -0
- codeset-0.4.4.dist-info/WHEEL +4 -0
- codeset-0.4.4.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
from .._models import BaseModel
|
|
4
|
+
|
|
5
|
+
__all__ = ["SessionCloseResponse"]
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class SessionCloseResponse(BaseModel):
|
|
9
|
+
"""Response model for session deletion."""
|
|
10
|
+
|
|
11
|
+
duration_seconds: float
|
|
12
|
+
"""Duration of the session in seconds."""
|
|
13
|
+
|
|
14
|
+
message: str
|
|
15
|
+
"""Success message confirming session deletion"""
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing_extensions import Required, TypedDict
|
|
6
|
+
|
|
7
|
+
__all__ = ["SessionCreateParams"]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SessionCreateParams(TypedDict, total=False):
|
|
11
|
+
dataset: Required[str]
|
|
12
|
+
"""Dataset name for the sample."""
|
|
13
|
+
|
|
14
|
+
sample_id: Required[str]
|
|
15
|
+
"""Identifier of the sample to use for this session."""
|
|
16
|
+
|
|
17
|
+
ttl_minutes: int
|
|
18
|
+
"""Time to live for the session in minutes (default: 30)."""
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
|
|
5
|
+
from .._models import BaseModel
|
|
6
|
+
from .session_status import SessionStatus
|
|
7
|
+
|
|
8
|
+
__all__ = ["SessionCreateResponse"]
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class SessionCreateResponse(BaseModel):
|
|
12
|
+
"""Response for creating a session."""
|
|
13
|
+
|
|
14
|
+
expires_at: datetime
|
|
15
|
+
"""Timestamp when the session will expire (UTC)."""
|
|
16
|
+
|
|
17
|
+
session_id: str
|
|
18
|
+
"""Unique identifier for the created session."""
|
|
19
|
+
|
|
20
|
+
status: SessionStatus
|
|
21
|
+
"""Initial status of the session."""
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing_extensions import Required, TypedDict
|
|
6
|
+
|
|
7
|
+
__all__ = ["SessionExecuteCommandParams"]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SessionExecuteCommandParams(TypedDict, total=False):
|
|
11
|
+
command: Required[str]
|
|
12
|
+
"""The bash command to execute."""
|
|
13
|
+
|
|
14
|
+
command_timeout: int
|
|
15
|
+
"""Timeout for command execution in seconds (default: 300)."""
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
from .._models import BaseModel
|
|
4
|
+
|
|
5
|
+
__all__ = ["SessionExecuteCommandResponse"]
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class SessionExecuteCommandResponse(BaseModel):
|
|
9
|
+
"""Response for starting command execution (async)."""
|
|
10
|
+
|
|
11
|
+
interaction_id: str
|
|
12
|
+
"""Unique identifier for the interaction."""
|
|
13
|
+
|
|
14
|
+
status: str
|
|
15
|
+
"""Status of the interaction: 'pending', 'processing', 'completed', or 'failed'."""
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
from typing import List
|
|
4
|
+
|
|
5
|
+
from .session import Session
|
|
6
|
+
from .._models import BaseModel
|
|
7
|
+
|
|
8
|
+
__all__ = ["SessionListResponse"]
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class SessionListResponse(BaseModel):
|
|
12
|
+
"""Response for listing sessions."""
|
|
13
|
+
|
|
14
|
+
has_more: bool
|
|
15
|
+
"""Indicates if more pages of results are available."""
|
|
16
|
+
|
|
17
|
+
sessions: List[Session]
|
|
18
|
+
|
|
19
|
+
total_count: int
|
|
20
|
+
"""Total number of sessions returned."""
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing_extensions import Required, TypedDict
|
|
6
|
+
|
|
7
|
+
__all__ = ["SessionStrReplaceParams"]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SessionStrReplaceParams(TypedDict, total=False):
|
|
11
|
+
file_path: Required[str]
|
|
12
|
+
"""Path to the file where replacement should be performed."""
|
|
13
|
+
|
|
14
|
+
str_to_insert: Required[str]
|
|
15
|
+
"""String to insert as replacement."""
|
|
16
|
+
|
|
17
|
+
str_to_replace: Required[str]
|
|
18
|
+
"""String to be replaced."""
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
from .._models import BaseModel
|
|
4
|
+
|
|
5
|
+
__all__ = ["SessionStrReplaceResponse"]
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class SessionStrReplaceResponse(BaseModel):
|
|
9
|
+
"""Response for string replacement operation."""
|
|
10
|
+
|
|
11
|
+
message: str
|
|
12
|
+
"""Details about the string replacement operation."""
|
|
13
|
+
|
|
14
|
+
success: bool
|
|
15
|
+
"""Whether the string replacement was successful."""
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .job_status import JobStatus as JobStatus
|
|
6
|
+
from .verify_start_response import VerifyStartResponse as VerifyStartResponse
|
|
7
|
+
from .verify_status_response import VerifyStatusResponse as VerifyStatusResponse
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
from typing_extensions import Literal, TypeAlias
|
|
4
|
+
|
|
5
|
+
__all__ = ["JobStatus"]
|
|
6
|
+
|
|
7
|
+
JobStatus: TypeAlias = Literal["pending", "starting", "running", "completed", "error", "cancelled"]
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
|
|
5
|
+
from ..._models import BaseModel
|
|
6
|
+
from .job_status import JobStatus
|
|
7
|
+
|
|
8
|
+
__all__ = ["VerifyStartResponse"]
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class VerifyStartResponse(BaseModel):
|
|
12
|
+
"""Response for starting verification (async)."""
|
|
13
|
+
|
|
14
|
+
job_id: str
|
|
15
|
+
"""Unique identifier for the verification job."""
|
|
16
|
+
|
|
17
|
+
started_at: datetime
|
|
18
|
+
"""Timestamp when verification started (UTC)."""
|
|
19
|
+
|
|
20
|
+
status: JobStatus
|
|
21
|
+
"""Initial status of the verification job."""
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
from typing import List, Optional
|
|
4
|
+
from datetime import datetime
|
|
5
|
+
from typing_extensions import Literal
|
|
6
|
+
|
|
7
|
+
from ..._models import BaseModel
|
|
8
|
+
from .job_status import JobStatus
|
|
9
|
+
from ..error_info import ErrorInfo
|
|
10
|
+
from ..container_info import ContainerInfo
|
|
11
|
+
|
|
12
|
+
__all__ = ["VerifyStatusResponse", "Result"]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Result(BaseModel):
|
|
16
|
+
"""Result from a test suite verifier."""
|
|
17
|
+
|
|
18
|
+
execution_duration_seconds: float
|
|
19
|
+
"""Total execution time for the verification step."""
|
|
20
|
+
|
|
21
|
+
failed: int
|
|
22
|
+
"""Number of tests that failed."""
|
|
23
|
+
|
|
24
|
+
is_success: bool
|
|
25
|
+
"""Overall success status of the verification."""
|
|
26
|
+
|
|
27
|
+
passed: int
|
|
28
|
+
"""Number of tests that passed."""
|
|
29
|
+
|
|
30
|
+
skipped: int
|
|
31
|
+
"""Number of tests that were skipped."""
|
|
32
|
+
|
|
33
|
+
total: int
|
|
34
|
+
"""Total number of tests executed."""
|
|
35
|
+
|
|
36
|
+
failures: Optional[List[object]] = None
|
|
37
|
+
"""A list of failed tests with their details."""
|
|
38
|
+
|
|
39
|
+
stderr: Optional[str] = None
|
|
40
|
+
"""Standard error from the verifier."""
|
|
41
|
+
|
|
42
|
+
stdout: Optional[str] = None
|
|
43
|
+
"""Standard output from the verifier."""
|
|
44
|
+
|
|
45
|
+
tool: Optional[Literal["test_suite"]] = None
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class VerifyStatusResponse(BaseModel):
|
|
49
|
+
"""Represents a single verification job, the core resource of the API."""
|
|
50
|
+
|
|
51
|
+
created_at: datetime
|
|
52
|
+
"""Timestamp when the job was created (UTC)."""
|
|
53
|
+
|
|
54
|
+
job_id: str
|
|
55
|
+
"""Unique identifier for the job."""
|
|
56
|
+
|
|
57
|
+
sample_id: str
|
|
58
|
+
"""Identifier of the sample being used for verification."""
|
|
59
|
+
|
|
60
|
+
session_id: str
|
|
61
|
+
"""Session identifier for this job."""
|
|
62
|
+
|
|
63
|
+
status: JobStatus
|
|
64
|
+
"""Current status of the job."""
|
|
65
|
+
|
|
66
|
+
completed_at: Optional[datetime] = None
|
|
67
|
+
"""Timestamp when the job completed (UTC)."""
|
|
68
|
+
|
|
69
|
+
container_info: Optional[ContainerInfo] = None
|
|
70
|
+
"""Information about a container."""
|
|
71
|
+
|
|
72
|
+
error: Optional[ErrorInfo] = None
|
|
73
|
+
"""Details about an error that occurred during job processing."""
|
|
74
|
+
|
|
75
|
+
result: Optional[Result] = None
|
|
76
|
+
"""Result from a test suite verifier."""
|
|
77
|
+
|
|
78
|
+
started_at: Optional[datetime] = None
|
|
79
|
+
"""Timestamp when the job processing started (UTC)."""
|
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: codeset
|
|
3
|
+
Version: 0.4.4
|
|
4
|
+
Summary: The official Python library for the codeset API
|
|
5
|
+
Project-URL: Homepage, https://github.com/codeset-ai/codeset-sdk
|
|
6
|
+
Project-URL: Repository, https://github.com/codeset-ai/codeset-sdk
|
|
7
|
+
Author: Codeset
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
11
|
+
Classifier: Operating System :: MacOS
|
|
12
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Operating System :: POSIX
|
|
15
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.9
|
|
25
|
+
Requires-Dist: anyio<5,>=3.5.0
|
|
26
|
+
Requires-Dist: distro<2,>=1.7.0
|
|
27
|
+
Requires-Dist: httpx<1,>=0.23.0
|
|
28
|
+
Requires-Dist: pydantic<3,>=1.9.0
|
|
29
|
+
Requires-Dist: sniffio
|
|
30
|
+
Requires-Dist: typing-extensions<5,>=4.10
|
|
31
|
+
Provides-Extra: aiohttp
|
|
32
|
+
Requires-Dist: aiohttp; extra == 'aiohttp'
|
|
33
|
+
Requires-Dist: httpx-aiohttp>=0.1.9; extra == 'aiohttp'
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
|
|
36
|
+
# Codeset Python API library
|
|
37
|
+
|
|
38
|
+
<!-- prettier-ignore -->
|
|
39
|
+
[)](https://pypi.org/project/codeset/)
|
|
40
|
+
|
|
41
|
+
The Codeset Python library provides convenient access to the Codeset REST API from any Python 3.9+
|
|
42
|
+
application. The library includes type definitions for all request params and response fields,
|
|
43
|
+
and offers both synchronous and asynchronous clients powered by [httpx](https://github.com/encode/httpx).
|
|
44
|
+
|
|
45
|
+
It is generated with [Stainless](https://www.stainless.com/).
|
|
46
|
+
|
|
47
|
+
## Documentation
|
|
48
|
+
|
|
49
|
+
The REST API documentation can be found on [docs.codeset.ai](https://docs.codeset.ai). The full API of this library can be found in [api.md](https://github.com/codeset-ai/codeset-sdk/tree/main/api.md).
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
# install from PyPI
|
|
55
|
+
pip install codeset
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Usage
|
|
59
|
+
|
|
60
|
+
The full API of this library can be found in [api.md](https://github.com/codeset-ai/codeset-sdk/tree/main/api.md).
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
import os
|
|
64
|
+
from codeset import Codeset
|
|
65
|
+
|
|
66
|
+
client = Codeset(
|
|
67
|
+
api_key=os.environ.get("CODESET_API_KEY"), # This is the default and can be omitted
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
response = client.health.check()
|
|
71
|
+
print(response.service)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
While you can provide an `api_key` keyword argument,
|
|
75
|
+
we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
|
|
76
|
+
to add `CODESET_API_KEY="My API Key"` to your `.env` file
|
|
77
|
+
so that your API Key is not stored in source control.
|
|
78
|
+
|
|
79
|
+
## Async usage
|
|
80
|
+
|
|
81
|
+
Simply import `AsyncCodeset` instead of `Codeset` and use `await` with each API call:
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
import os
|
|
85
|
+
import asyncio
|
|
86
|
+
from codeset import AsyncCodeset
|
|
87
|
+
|
|
88
|
+
client = AsyncCodeset(
|
|
89
|
+
api_key=os.environ.get("CODESET_API_KEY"), # This is the default and can be omitted
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
async def main() -> None:
|
|
94
|
+
response = await client.health.check()
|
|
95
|
+
print(response.service)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
asyncio.run(main())
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Functionality between the synchronous and asynchronous clients is otherwise identical.
|
|
102
|
+
|
|
103
|
+
### With aiohttp
|
|
104
|
+
|
|
105
|
+
By default, the async client uses `httpx` for HTTP requests. However, for improved concurrency performance you may also use `aiohttp` as the HTTP backend.
|
|
106
|
+
|
|
107
|
+
You can enable this by installing `aiohttp`:
|
|
108
|
+
|
|
109
|
+
```sh
|
|
110
|
+
# install from PyPI
|
|
111
|
+
pip install codeset[aiohttp]
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Then you can enable it by instantiating the client with `http_client=DefaultAioHttpClient()`:
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
import os
|
|
118
|
+
import asyncio
|
|
119
|
+
from codeset import DefaultAioHttpClient
|
|
120
|
+
from codeset import AsyncCodeset
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
async def main() -> None:
|
|
124
|
+
async with AsyncCodeset(
|
|
125
|
+
api_key=os.environ.get("CODESET_API_KEY"), # This is the default and can be omitted
|
|
126
|
+
http_client=DefaultAioHttpClient(),
|
|
127
|
+
) as client:
|
|
128
|
+
response = await client.health.check()
|
|
129
|
+
print(response.service)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
asyncio.run(main())
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Using types
|
|
136
|
+
|
|
137
|
+
Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typing.html#typing.TypedDict). Responses are [Pydantic models](https://docs.pydantic.dev) which also provide helper methods for things like:
|
|
138
|
+
|
|
139
|
+
- Serializing back into JSON, `model.to_json()`
|
|
140
|
+
- Converting to a dictionary, `model.to_dict()`
|
|
141
|
+
|
|
142
|
+
Typed requests and responses provide autocomplete and documentation within your editor. If you would like to see type errors in VS Code to help catch bugs earlier, set `python.analysis.typeCheckingMode` to `basic`.
|
|
143
|
+
|
|
144
|
+
## Handling errors
|
|
145
|
+
|
|
146
|
+
When the library is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass of `codeset.APIConnectionError` is raised.
|
|
147
|
+
|
|
148
|
+
When the API returns a non-success status code (that is, 4xx or 5xx
|
|
149
|
+
response), a subclass of `codeset.APIStatusError` is raised, containing `status_code` and `response` properties.
|
|
150
|
+
|
|
151
|
+
All errors inherit from `codeset.APIError`.
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
import codeset
|
|
155
|
+
from codeset import Codeset
|
|
156
|
+
|
|
157
|
+
client = Codeset()
|
|
158
|
+
|
|
159
|
+
try:
|
|
160
|
+
client.health.check()
|
|
161
|
+
except codeset.APIConnectionError as e:
|
|
162
|
+
print("The server could not be reached")
|
|
163
|
+
print(e.__cause__) # an underlying Exception, likely raised within httpx.
|
|
164
|
+
except codeset.RateLimitError as e:
|
|
165
|
+
print("A 429 status code was received; we should back off a bit.")
|
|
166
|
+
except codeset.APIStatusError as e:
|
|
167
|
+
print("Another non-200-range status code was received")
|
|
168
|
+
print(e.status_code)
|
|
169
|
+
print(e.response)
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Error codes are as follows:
|
|
173
|
+
|
|
174
|
+
| Status Code | Error Type |
|
|
175
|
+
| ----------- | -------------------------- |
|
|
176
|
+
| 400 | `BadRequestError` |
|
|
177
|
+
| 401 | `AuthenticationError` |
|
|
178
|
+
| 403 | `PermissionDeniedError` |
|
|
179
|
+
| 404 | `NotFoundError` |
|
|
180
|
+
| 422 | `UnprocessableEntityError` |
|
|
181
|
+
| 429 | `RateLimitError` |
|
|
182
|
+
| >=500 | `InternalServerError` |
|
|
183
|
+
| N/A | `APIConnectionError` |
|
|
184
|
+
|
|
185
|
+
### Retries
|
|
186
|
+
|
|
187
|
+
Certain errors are automatically retried 0 times by default, with a short exponential backoff.
|
|
188
|
+
Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict,
|
|
189
|
+
429 Rate Limit, and >=500 Internal errors are all retried by default.
|
|
190
|
+
|
|
191
|
+
You can use the `max_retries` option to configure or disable retry settings:
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
from codeset import Codeset
|
|
195
|
+
|
|
196
|
+
# Configure the default for all requests:
|
|
197
|
+
client = Codeset(
|
|
198
|
+
# default is 2
|
|
199
|
+
max_retries=0,
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
# Or, configure per-request:
|
|
203
|
+
client.with_options(max_retries=5).health.check()
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Timeouts
|
|
207
|
+
|
|
208
|
+
By default requests time out after 5 minutes. You can configure this with a `timeout` option,
|
|
209
|
+
which accepts a float or an [`httpx.Timeout`](https://www.python-httpx.org/advanced/timeouts/#fine-tuning-the-configuration) object:
|
|
210
|
+
|
|
211
|
+
```python
|
|
212
|
+
from codeset import Codeset
|
|
213
|
+
|
|
214
|
+
# Configure the default for all requests:
|
|
215
|
+
client = Codeset(
|
|
216
|
+
# 20 seconds (default is 5 minutes)
|
|
217
|
+
timeout=20.0,
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
# More granular control:
|
|
221
|
+
client = Codeset(
|
|
222
|
+
timeout=httpx.Timeout(60.0, read=5.0, write=10.0, connect=2.0),
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
# Override per-request:
|
|
226
|
+
client.with_options(timeout=5.0).health.check()
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
On timeout, an `APITimeoutError` is thrown.
|
|
230
|
+
|
|
231
|
+
Note that requests that time out are [retried twice by default](https://github.com/codeset-ai/codeset-sdk/tree/main/#retries).
|
|
232
|
+
|
|
233
|
+
## Advanced
|
|
234
|
+
|
|
235
|
+
### Logging
|
|
236
|
+
|
|
237
|
+
We use the standard library [`logging`](https://docs.python.org/3/library/logging.html) module.
|
|
238
|
+
|
|
239
|
+
You can enable logging by setting the environment variable `CODESET_LOG` to `info`.
|
|
240
|
+
|
|
241
|
+
```shell
|
|
242
|
+
$ export CODESET_LOG=info
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
Or to `debug` for more verbose logging.
|
|
246
|
+
|
|
247
|
+
### How to tell whether `None` means `null` or missing
|
|
248
|
+
|
|
249
|
+
In an API response, a field may be explicitly `null`, or missing entirely; in either case, its value is `None` in this library. You can differentiate the two cases with `.model_fields_set`:
|
|
250
|
+
|
|
251
|
+
```py
|
|
252
|
+
if response.my_field is None:
|
|
253
|
+
if 'my_field' not in response.model_fields_set:
|
|
254
|
+
print('Got json like {}, without a "my_field" key present at all.')
|
|
255
|
+
else:
|
|
256
|
+
print('Got json like {"my_field": null}.')
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Accessing raw response data (e.g. headers)
|
|
260
|
+
|
|
261
|
+
The "raw" Response object can be accessed by prefixing `.with_raw_response.` to any HTTP method call, e.g.,
|
|
262
|
+
|
|
263
|
+
```py
|
|
264
|
+
from codeset import Codeset
|
|
265
|
+
|
|
266
|
+
client = Codeset()
|
|
267
|
+
response = client.health.with_raw_response.check()
|
|
268
|
+
print(response.headers.get('X-My-Header'))
|
|
269
|
+
|
|
270
|
+
health = response.parse() # get the object that `health.check()` would have returned
|
|
271
|
+
print(health.service)
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
These methods return an [`APIResponse`](https://github.com/codeset-ai/codeset-sdk/tree/main/src/codeset/_response.py) object.
|
|
275
|
+
|
|
276
|
+
The async client returns an [`AsyncAPIResponse`](https://github.com/codeset-ai/codeset-sdk/tree/main/src/codeset/_response.py) with the same structure, the only difference being `await`able methods for reading the response content.
|
|
277
|
+
|
|
278
|
+
#### `.with_streaming_response`
|
|
279
|
+
|
|
280
|
+
The above interface eagerly reads the full response body when you make the request, which may not always be what you want.
|
|
281
|
+
|
|
282
|
+
To stream the response body, use `.with_streaming_response` instead, which requires a context manager and only reads the response body once you call `.read()`, `.text()`, `.json()`, `.iter_bytes()`, `.iter_text()`, `.iter_lines()` or `.parse()`. In the async client, these are async methods.
|
|
283
|
+
|
|
284
|
+
```python
|
|
285
|
+
with client.health.with_streaming_response.check() as response:
|
|
286
|
+
print(response.headers.get("X-My-Header"))
|
|
287
|
+
|
|
288
|
+
for line in response.iter_lines():
|
|
289
|
+
print(line)
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
The context manager is required so that the response will reliably be closed.
|
|
293
|
+
|
|
294
|
+
### Making custom/undocumented requests
|
|
295
|
+
|
|
296
|
+
This library is typed for convenient access to the documented API.
|
|
297
|
+
|
|
298
|
+
If you need to access undocumented endpoints, params, or response properties, the library can still be used.
|
|
299
|
+
|
|
300
|
+
#### Undocumented endpoints
|
|
301
|
+
|
|
302
|
+
To make requests to undocumented endpoints, you can make requests using `client.get`, `client.post`, and other
|
|
303
|
+
http verbs. Options on the client will be respected (such as retries) when making this request.
|
|
304
|
+
|
|
305
|
+
```py
|
|
306
|
+
import httpx
|
|
307
|
+
|
|
308
|
+
response = client.post(
|
|
309
|
+
"/foo",
|
|
310
|
+
cast_to=httpx.Response,
|
|
311
|
+
body={"my_param": True},
|
|
312
|
+
)
|
|
313
|
+
|
|
314
|
+
print(response.headers.get("x-foo"))
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
#### Undocumented request params
|
|
318
|
+
|
|
319
|
+
If you want to explicitly send an extra param, you can do so with the `extra_query`, `extra_body`, and `extra_headers` request
|
|
320
|
+
options.
|
|
321
|
+
|
|
322
|
+
#### Undocumented response properties
|
|
323
|
+
|
|
324
|
+
To access undocumented response properties, you can access the extra fields like `response.unknown_prop`. You
|
|
325
|
+
can also get all the extra fields on the Pydantic model as a dict with
|
|
326
|
+
[`response.model_extra`](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_extra).
|
|
327
|
+
|
|
328
|
+
### Configuring the HTTP client
|
|
329
|
+
|
|
330
|
+
You can directly override the [httpx client](https://www.python-httpx.org/api/#client) to customize it for your use case, including:
|
|
331
|
+
|
|
332
|
+
- Support for [proxies](https://www.python-httpx.org/advanced/proxies/)
|
|
333
|
+
- Custom [transports](https://www.python-httpx.org/advanced/transports/)
|
|
334
|
+
- Additional [advanced](https://www.python-httpx.org/advanced/clients/) functionality
|
|
335
|
+
|
|
336
|
+
```python
|
|
337
|
+
import httpx
|
|
338
|
+
from codeset import Codeset, DefaultHttpxClient
|
|
339
|
+
|
|
340
|
+
client = Codeset(
|
|
341
|
+
# Or use the `CODESET_BASE_URL` env var
|
|
342
|
+
base_url="http://my.test.server.example.com:8083",
|
|
343
|
+
http_client=DefaultHttpxClient(
|
|
344
|
+
proxy="http://my.test.proxy.example.com",
|
|
345
|
+
transport=httpx.HTTPTransport(local_address="0.0.0.0"),
|
|
346
|
+
),
|
|
347
|
+
)
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
You can also customize the client on a per-request basis by using `with_options()`:
|
|
351
|
+
|
|
352
|
+
```python
|
|
353
|
+
client.with_options(http_client=DefaultHttpxClient(...))
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
### Managing HTTP resources
|
|
357
|
+
|
|
358
|
+
By default the library closes underlying HTTP connections whenever the client is [garbage collected](https://docs.python.org/3/reference/datamodel.html#object.__del__). You can manually close the client using the `.close()` method if desired, or with a context manager that closes when exiting.
|
|
359
|
+
|
|
360
|
+
```py
|
|
361
|
+
from codeset import Codeset
|
|
362
|
+
|
|
363
|
+
with Codeset() as client:
|
|
364
|
+
# make requests here
|
|
365
|
+
...
|
|
366
|
+
|
|
367
|
+
# HTTP client is now closed
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
## Versioning
|
|
371
|
+
|
|
372
|
+
This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions:
|
|
373
|
+
|
|
374
|
+
1. Changes that only affect static types, without breaking runtime behavior.
|
|
375
|
+
2. Changes to library internals which are technically public but not intended or documented for external use. _(Please open a GitHub issue to let us know if you are relying on such internals.)_
|
|
376
|
+
3. Changes that we do not expect to impact the vast majority of users in practice.
|
|
377
|
+
|
|
378
|
+
We take backwards-compatibility seriously and work hard to ensure you can rely on a smooth upgrade experience.
|
|
379
|
+
|
|
380
|
+
We are keen for your feedback; please open an [issue](https://www.github.com/codeset-ai/codeset-sdk/issues) with questions, bugs, or suggestions.
|
|
381
|
+
|
|
382
|
+
### Determining the installed version
|
|
383
|
+
|
|
384
|
+
If you've upgraded to the latest version but aren't seeing any new features you were expecting then your python environment is likely still using an older version.
|
|
385
|
+
|
|
386
|
+
You can determine the version that is being used at runtime with:
|
|
387
|
+
|
|
388
|
+
```py
|
|
389
|
+
import codeset
|
|
390
|
+
print(codeset.__version__)
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
## Requirements
|
|
394
|
+
|
|
395
|
+
Python 3.9 or higher.
|
|
396
|
+
|
|
397
|
+
## Contributing
|
|
398
|
+
|
|
399
|
+
See [the contributing documentation](https://github.com/codeset-ai/codeset-sdk/tree/main/./CONTRIBUTING.md).
|