contree-client 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.
- contree_client-0.1.0/LICENSE +177 -0
- contree_client-0.1.0/PKG-INFO +389 -0
- contree_client-0.1.0/README.md +361 -0
- contree_client-0.1.0/contree_client/__init__.py +174 -0
- contree_client-0.1.0/contree_client/aiohttp.py +153 -0
- contree_client-0.1.0/contree_client/asyncio.py +48 -0
- contree_client-0.1.0/contree_client/base.py +2653 -0
- contree_client-0.1.0/contree_client/exceptions.py +97 -0
- contree_client-0.1.0/contree_client/http.py +334 -0
- contree_client-0.1.0/contree_client/httpx.py +230 -0
- contree_client-0.1.0/contree_client/models.py +2004 -0
- contree_client-0.1.0/contree_client/operations.py +478 -0
- contree_client-0.1.0/contree_client/profiles.py +131 -0
- contree_client-0.1.0/contree_client/py.typed +0 -0
- contree_client-0.1.0/contree_client/requests.py +140 -0
- contree_client-0.1.0/contree_client/runtime.py +619 -0
- contree_client-0.1.0/contree_client/spec_info.py +2785 -0
- contree_client-0.1.0/contree_client/sync.py +55 -0
- contree_client-0.1.0/contree_client/testing.py +275 -0
- contree_client-0.1.0/contree_client/types.py +37 -0
- contree_client-0.1.0/contree_client/urllib3.py +139 -0
- contree_client-0.1.0/pyproject.toml +88 -0
- contree_client-0.1.0/tests/conftest.py +168 -0
- contree_client-0.1.0/tests/stub_server.py +768 -0
- contree_client-0.1.0/tests/test_autodetect.py +86 -0
- contree_client-0.1.0/tests/test_clients.py +797 -0
- contree_client-0.1.0/tests/test_file_helpers.py +125 -0
- contree_client-0.1.0/tests/test_integration.py +452 -0
- contree_client-0.1.0/tests/test_interface.py +175 -0
- contree_client-0.1.0/tests/test_keepalive.py +483 -0
- contree_client-0.1.0/tests/test_logging.py +181 -0
- contree_client-0.1.0/tests/test_models.py +438 -0
- contree_client-0.1.0/tests/test_operation_helpers.py +81 -0
- contree_client-0.1.0/tests/test_operations.py +124 -0
- contree_client-0.1.0/tests/test_pagination.py +77 -0
- contree_client-0.1.0/tests/test_profiles.py +301 -0
- contree_client-0.1.0/tests/test_retry.py +264 -0
- contree_client-0.1.0/tests/test_runtime.py +336 -0
- contree_client-0.1.0/tests/test_sse.py +97 -0
- contree_client-0.1.0/tests/test_stream_lifecycle.py +184 -0
- contree_client-0.1.0/tests/test_testing.py +232 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: contree-client
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python client for the Contree API, generated from the OpenAPI specification
|
|
5
|
+
Author: Dmitry Orlov
|
|
6
|
+
Author-email: Dmitry Orlov <mosquito@nebius.com>
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: aiohttp>=3.9 ; extra == 'aiohttp'
|
|
10
|
+
Requires-Dist: contree-client[urllib3,requests,aiohttp,httpx] ; extra == 'dev'
|
|
11
|
+
Requires-Dist: pytest>=8 ; extra == 'dev'
|
|
12
|
+
Requires-Dist: pytest-cov>=7.1.0 ; extra == 'dev'
|
|
13
|
+
Requires-Dist: pytest-timeout>=2.3 ; extra == 'dev'
|
|
14
|
+
Requires-Dist: markdown-pytest[async]>=0.3 ; extra == 'dev'
|
|
15
|
+
Requires-Dist: types-requests ; extra == 'dev'
|
|
16
|
+
Requires-Dist: httpx>=0.27 ; extra == 'httpx'
|
|
17
|
+
Requires-Dist: requests>=2.28 ; extra == 'requests'
|
|
18
|
+
Requires-Dist: urllib3>=2.0 ; extra == 'urllib3'
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Project-URL: Homepage, https://contree.dev/
|
|
21
|
+
Project-URL: Documentation, https://docs.tokenfactory.nebius.com/sandboxes/overview
|
|
22
|
+
Provides-Extra: aiohttp
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Provides-Extra: httpx
|
|
25
|
+
Provides-Extra: requests
|
|
26
|
+
Provides-Extra: urllib3
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# contree-client
|
|
30
|
+
|
|
31
|
+
Python client for the Contree API — sandboxed code execution
|
|
32
|
+
combining virtual machine isolation with container operational
|
|
33
|
+
efficiency.
|
|
34
|
+
|
|
35
|
+
Synchronous clients are named `ContreeClient`, asynchronous ones are
|
|
36
|
+
named `ContreeAsyncClient`; every backend shares the same interface:
|
|
37
|
+
|
|
38
|
+
| Backend | Extra | Import |
|
|
39
|
+
|-------------|----------------------------|-----------------------------------------------------------------|
|
|
40
|
+
| http.client | — (stdlib) | `from contree_client.http import ContreeClient` |
|
|
41
|
+
| urllib3 | `contree-client[urllib3]` | `from contree_client.urllib3 import ContreeClient` |
|
|
42
|
+
| requests | `contree-client[requests]` | `from contree_client.requests import ContreeClient` |
|
|
43
|
+
| httpx | `contree-client[httpx]` | `from contree_client.httpx import ContreeClient` / `ContreeAsyncClient` |
|
|
44
|
+
| aiohttp | `contree-client[aiohttp]` | `from contree_client.aiohttp import ContreeAsyncClient` |
|
|
45
|
+
|
|
46
|
+
Or let the package pick the first installed backend:
|
|
47
|
+
|
|
48
|
+
<!-- name: test_readme_autodetect -->
|
|
49
|
+
```python
|
|
50
|
+
from contree_client.sync import ContreeClient
|
|
51
|
+
from contree_client.asyncio import ContreeAsyncClient
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Quick start
|
|
55
|
+
|
|
56
|
+
<!--
|
|
57
|
+
name: test_readme_quick_start;
|
|
58
|
+
fixtures: tmp_path, monkeypatch
|
|
59
|
+
```python
|
|
60
|
+
from contree_client import models, testing
|
|
61
|
+
|
|
62
|
+
OP_UUID = "87654321-9abc-baba-deda-0123456789ab"
|
|
63
|
+
EVENTS = [
|
|
64
|
+
models.OperationEvent.from_dict(event)
|
|
65
|
+
for event in (
|
|
66
|
+
{
|
|
67
|
+
"id": 1,
|
|
68
|
+
"ts": "2026-06-08T20:00:00Z",
|
|
69
|
+
"spid": 1,
|
|
70
|
+
"type": "stdout",
|
|
71
|
+
"data": {"value": "Linux\n", "encoding": "ascii"},
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
"id": 2,
|
|
75
|
+
"ts": "2026-06-08T20:00:01Z",
|
|
76
|
+
"spid": 0,
|
|
77
|
+
"type": "completion",
|
|
78
|
+
"data": {"status": "SUCCESS", "error": None, "duration_ms": 12},
|
|
79
|
+
},
|
|
80
|
+
)
|
|
81
|
+
]
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class QuickStartDouble(testing.ContreeClient):
|
|
85
|
+
def __init__(self, *args, **kwargs):
|
|
86
|
+
super().__init__(*args, **kwargs)
|
|
87
|
+
self.mock(
|
|
88
|
+
"spawn_instance",
|
|
89
|
+
models.InstanceSpawnResponse.from_dict({"uuid": OP_UUID}),
|
|
90
|
+
)
|
|
91
|
+
self.mock("iter_operation_events", EVENTS)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
(tmp_path / "auth.ini").write_text(
|
|
95
|
+
"[DEFAULT]\nprofile = default\n"
|
|
96
|
+
"[profile:default]\ntoken = SECRET\nurl = https://contree.example.com\n"
|
|
97
|
+
)
|
|
98
|
+
monkeypatch.setenv("CONTREE_HOME", str(tmp_path))
|
|
99
|
+
monkeypatch.delenv("CONTREE_PROFILE", raising=False)
|
|
100
|
+
monkeypatch.setattr("contree_client.sync.ContreeClient", QuickStartDouble)
|
|
101
|
+
```
|
|
102
|
+
-->
|
|
103
|
+
```python
|
|
104
|
+
from contree_client.sync import ContreeClient
|
|
105
|
+
|
|
106
|
+
# reuse a saved Contree profile,
|
|
107
|
+
# or pass credentials directly: ContreeClient("IAM_TOKEN", project=...)
|
|
108
|
+
with ContreeClient.from_profile() as client:
|
|
109
|
+
response = client.spawn_instance("uname -a", "tag:ubuntu:latest", shell=True)
|
|
110
|
+
for event in client.iter_operation_events(response.uuid, follow=True):
|
|
111
|
+
print(event.type, event.data)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Use cases
|
|
115
|
+
|
|
116
|
+
Run a command and read its output:
|
|
117
|
+
|
|
118
|
+
<!--
|
|
119
|
+
name: test_readme_use_cases;
|
|
120
|
+
fixtures: tmp_path, monkeypatch
|
|
121
|
+
```python
|
|
122
|
+
import gzip
|
|
123
|
+
import io
|
|
124
|
+
import tarfile
|
|
125
|
+
|
|
126
|
+
from contree_client import models, testing
|
|
127
|
+
|
|
128
|
+
OP_UUID = "87654321-9abc-baba-deda-0123456789ab"
|
|
129
|
+
IMG_UUID = "12345678-9abc-baba-deda-0123456789ab"
|
|
130
|
+
EVENTS = [
|
|
131
|
+
models.OperationEvent.from_dict(event)
|
|
132
|
+
for event in (
|
|
133
|
+
{
|
|
134
|
+
"id": 1,
|
|
135
|
+
"ts": "2026-06-08T20:00:00Z",
|
|
136
|
+
"spid": 1,
|
|
137
|
+
"type": "stdout",
|
|
138
|
+
"data": {"value": "hello\n", "encoding": "ascii"},
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
"id": 2,
|
|
142
|
+
"ts": "2026-06-08T20:00:01Z",
|
|
143
|
+
"spid": 0,
|
|
144
|
+
"type": "completion",
|
|
145
|
+
"data": {"status": "SUCCESS", "error": None, "duration_ms": 12},
|
|
146
|
+
},
|
|
147
|
+
)
|
|
148
|
+
]
|
|
149
|
+
OPERATION = models.OperationResponse.from_dict(
|
|
150
|
+
{
|
|
151
|
+
"uuid": OP_UUID,
|
|
152
|
+
"kind": "instance",
|
|
153
|
+
"status": "SUCCESS",
|
|
154
|
+
"metadata": {
|
|
155
|
+
"command": "echo hello",
|
|
156
|
+
"image": "tag:ubuntu:latest",
|
|
157
|
+
"result": {"stdout": {"value": "hello\n", "encoding": "ascii"}},
|
|
158
|
+
},
|
|
159
|
+
}
|
|
160
|
+
)
|
|
161
|
+
buffer = io.BytesIO()
|
|
162
|
+
with tarfile.open(fileobj=buffer, mode="w") as tar:
|
|
163
|
+
payload = b"127.0.0.1 localhost\n"
|
|
164
|
+
info = tarfile.TarInfo("etc/hosts")
|
|
165
|
+
info.size = len(payload)
|
|
166
|
+
tar.addfile(info, io.BytesIO(payload))
|
|
167
|
+
TAR_BYTES = buffer.getvalue()
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
class UseCaseDouble(testing.ContreeClient):
|
|
171
|
+
def __init__(self, *args, **kwargs):
|
|
172
|
+
super().__init__(*args, **kwargs)
|
|
173
|
+
self.mock(
|
|
174
|
+
"spawn_instance",
|
|
175
|
+
models.InstanceSpawnResponse.from_dict({"uuid": OP_UUID}),
|
|
176
|
+
)
|
|
177
|
+
self.mock("get_operation_status", OPERATION)
|
|
178
|
+
self.mock("iter_operation_events", EVENTS)
|
|
179
|
+
self.mock(
|
|
180
|
+
"upload_file",
|
|
181
|
+
models.FileResponse.from_dict(
|
|
182
|
+
{
|
|
183
|
+
"uuid": "a9165a5d-5c86-4bd8-8ee4-ae46c19cf45d",
|
|
184
|
+
"sha256": "0" * 64,
|
|
185
|
+
"size": 8,
|
|
186
|
+
}
|
|
187
|
+
),
|
|
188
|
+
)
|
|
189
|
+
self.mock("import_image", OP_UUID)
|
|
190
|
+
self.mock("inspect_find_image_by_tag", IMG_UUID)
|
|
191
|
+
self.mock(
|
|
192
|
+
"inspect_image_list",
|
|
193
|
+
models.DirectoryList.from_dict({"path": "/etc", "files": []}),
|
|
194
|
+
)
|
|
195
|
+
self.mock("inspect_image_archive", [TAR_BYTES])
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
monkeypatch.chdir(tmp_path)
|
|
199
|
+
(tmp_path / "data.txt").write_text("one\ntwo\n")
|
|
200
|
+
(tmp_path / "auth.ini").write_text(
|
|
201
|
+
"[DEFAULT]\nprofile = default\n"
|
|
202
|
+
"[profile:default]\ntoken = SECRET\nurl = https://contree.example.com\n"
|
|
203
|
+
)
|
|
204
|
+
monkeypatch.setenv("CONTREE_HOME", str(tmp_path))
|
|
205
|
+
monkeypatch.delenv("CONTREE_PROFILE", raising=False)
|
|
206
|
+
ContreeClient = UseCaseDouble
|
|
207
|
+
client = UseCaseDouble("IAM_TOKEN")
|
|
208
|
+
```
|
|
209
|
+
-->
|
|
210
|
+
```python
|
|
211
|
+
from contree_client import OperationStatus
|
|
212
|
+
|
|
213
|
+
response = client.spawn_instance(
|
|
214
|
+
"echo hello",
|
|
215
|
+
"tag:ubuntu:latest",
|
|
216
|
+
shell=True,
|
|
217
|
+
disposable=True, # do not persist a result image
|
|
218
|
+
timeout=60,
|
|
219
|
+
)
|
|
220
|
+
operation = client.wait_operation(response.uuid)
|
|
221
|
+
assert operation.status is OperationStatus.SUCCESS
|
|
222
|
+
print(operation.metadata.result.stdout.as_text())
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
Follow the live event stream with transparent reconnection
|
|
226
|
+
(`Last-Event-Id` resume; ends on the `completion` event):
|
|
227
|
+
|
|
228
|
+
<!-- name: test_readme_use_cases -->
|
|
229
|
+
```python
|
|
230
|
+
import sys
|
|
231
|
+
|
|
232
|
+
from contree_client import decode_chunk
|
|
233
|
+
|
|
234
|
+
for event in client.follow_operation_events(response.uuid):
|
|
235
|
+
if event.type in ("stdout", "stderr"):
|
|
236
|
+
sys.stdout.buffer.write(decode_chunk(event.data))
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Retry transient failures (network errors, 410/425/5xx) automatically:
|
|
240
|
+
|
|
241
|
+
<!-- name: test_readme_use_cases -->
|
|
242
|
+
```python
|
|
243
|
+
from contree_client import RetryPolicy
|
|
244
|
+
|
|
245
|
+
client = ContreeClient.from_profile(retry=RetryPolicy())
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
Stage files into the sandbox:
|
|
249
|
+
|
|
250
|
+
<!-- name: test_readme_use_cases -->
|
|
251
|
+
```python
|
|
252
|
+
from contree_client import FileSpec
|
|
253
|
+
|
|
254
|
+
uploaded = client.upload_file(open("data.txt", "rb"))
|
|
255
|
+
client.spawn_instance(
|
|
256
|
+
"wc -l /work/data.txt",
|
|
257
|
+
"tag:ubuntu:latest",
|
|
258
|
+
shell=True,
|
|
259
|
+
files={"/work/data.txt": FileSpec(uuid=uploaded.uuid, mode="0644")},
|
|
260
|
+
)
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
Import an image and inspect it without spawning anything:
|
|
264
|
+
|
|
265
|
+
<!-- name: test_readme_use_cases -->
|
|
266
|
+
```python
|
|
267
|
+
from contree_client import ImageImportRegistry
|
|
268
|
+
|
|
269
|
+
operation_id = client.import_image(
|
|
270
|
+
ImageImportRegistry(url="docker://docker.io/library/busybox:latest"),
|
|
271
|
+
tag="busybox:latest",
|
|
272
|
+
)
|
|
273
|
+
image_uuid = client.inspect_find_image_by_tag("busybox:latest")
|
|
274
|
+
print(client.inspect_image_list(image_uuid, "/etc").files)
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
Download a directory as a tar stream:
|
|
278
|
+
|
|
279
|
+
<!-- name: test_readme_use_cases -->
|
|
280
|
+
```python
|
|
281
|
+
with open("etc.tar", "wb") as archive:
|
|
282
|
+
for chunk in client.inspect_image_archive(image_uuid, "/etc"):
|
|
283
|
+
archive.write(chunk)
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
Asynchronous flavour mirrors the sync API exactly:
|
|
287
|
+
|
|
288
|
+
<!--
|
|
289
|
+
name: async test_readme_async;
|
|
290
|
+
fixtures: tmp_path, monkeypatch
|
|
291
|
+
```python
|
|
292
|
+
from contree_client import models, testing
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
class AsyncDouble(testing.ContreeAsyncClient):
|
|
296
|
+
def __init__(self, *args, **kwargs):
|
|
297
|
+
super().__init__(*args, **kwargs)
|
|
298
|
+
self.mock(
|
|
299
|
+
"whoami",
|
|
300
|
+
models.WhoAmIResponse.from_dict(
|
|
301
|
+
{
|
|
302
|
+
"token_uuid": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
|
|
303
|
+
"token_expiration": None,
|
|
304
|
+
"permissions": {"spawn": True},
|
|
305
|
+
"operations_stat": {},
|
|
306
|
+
}
|
|
307
|
+
),
|
|
308
|
+
)
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
(tmp_path / "auth.ini").write_text(
|
|
312
|
+
"[DEFAULT]\nprofile = default\n"
|
|
313
|
+
"[profile:default]\ntoken = SECRET\nurl = https://contree.example.com\n"
|
|
314
|
+
)
|
|
315
|
+
monkeypatch.setenv("CONTREE_HOME", str(tmp_path))
|
|
316
|
+
monkeypatch.delenv("CONTREE_PROFILE", raising=False)
|
|
317
|
+
monkeypatch.setattr("contree_client.asyncio.ContreeAsyncClient", AsyncDouble)
|
|
318
|
+
```
|
|
319
|
+
-->
|
|
320
|
+
```python
|
|
321
|
+
from contree_client.asyncio import ContreeAsyncClient
|
|
322
|
+
|
|
323
|
+
async with ContreeAsyncClient.from_profile() as client:
|
|
324
|
+
me = await client.whoami()
|
|
325
|
+
print(me.permissions)
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
Test your code without a server — the in-memory double from
|
|
329
|
+
`contree_client.testing` implements the same interface as every real
|
|
330
|
+
backend; unmocked methods raise, calls are recorded:
|
|
331
|
+
|
|
332
|
+
<!--
|
|
333
|
+
name: test_readme_testing
|
|
334
|
+
```python
|
|
335
|
+
from contree_client import models
|
|
336
|
+
|
|
337
|
+
spawn_response = models.InstanceSpawnResponse.from_dict(
|
|
338
|
+
{"uuid": "87654321-9abc-baba-deda-0123456789ab"}
|
|
339
|
+
)
|
|
340
|
+
stdout_event = models.OperationEvent.from_dict(
|
|
341
|
+
{
|
|
342
|
+
"id": 1,
|
|
343
|
+
"ts": "2026-06-08T20:00:00Z",
|
|
344
|
+
"spid": 1,
|
|
345
|
+
"type": "stdout",
|
|
346
|
+
"data": {"value": "hi\n", "encoding": "ascii"},
|
|
347
|
+
}
|
|
348
|
+
)
|
|
349
|
+
exit_event = stdout_event
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def run_my_code(client):
|
|
353
|
+
response = client.spawn_instance("uname -a", "tag:ubuntu:latest", shell=True)
|
|
354
|
+
for event in client.iter_operation_events(response.uuid):
|
|
355
|
+
pass
|
|
356
|
+
```
|
|
357
|
+
-->
|
|
358
|
+
```python
|
|
359
|
+
from contree_client.testing import ContreeClient
|
|
360
|
+
|
|
361
|
+
client = ContreeClient()
|
|
362
|
+
client.mock("spawn_instance", spawn_response)
|
|
363
|
+
client.mock("iter_operation_events", [stdout_event, exit_event])
|
|
364
|
+
|
|
365
|
+
run_my_code(client)
|
|
366
|
+
|
|
367
|
+
assert client.calls_for("spawn_instance")
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
Debug logging (redacted, off by default):
|
|
371
|
+
|
|
372
|
+
<!-- name: test_readme_logging -->
|
|
373
|
+
```python
|
|
374
|
+
import logging
|
|
375
|
+
|
|
376
|
+
from contree_client.types import set_log_level
|
|
377
|
+
|
|
378
|
+
set_log_level(logging.DEBUG)
|
|
379
|
+
```
|
|
380
|
+
<!--
|
|
381
|
+
name: test_readme_logging
|
|
382
|
+
```python
|
|
383
|
+
set_log_level(logging.ERROR) # keep the other examples quiet
|
|
384
|
+
```
|
|
385
|
+
-->
|
|
386
|
+
|
|
387
|
+
## Copyright
|
|
388
|
+
|
|
389
|
+
Nebius B.V. 2026, Licensed under the Apache License, Version 2.0 (see "LICENSE" file).
|