Skip to content

Commit 7962ea7

Browse files
feat(api): manual updates (#23)
1 parent 3df897a commit 7962ea7

File tree

4 files changed

+300
-2
lines changed

4 files changed

+300
-2
lines changed

.stats.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
configured_endpoints: 27
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/replicate-mpatankar%2Freplicate-client-b45f922f6a041550870a96f5acec02aa6d8830046fc98b95a275c6486f7586fc.yml
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/replicate%2Freplicate-client-b45f922f6a041550870a96f5acec02aa6d8830046fc98b95a275c6486f7586fc.yml
33
openapi_spec_hash: ef7fddfb49b4d9c440b0635d2c86f341
4-
config_hash: ff75d189c98bb571ce62b1d338862231
4+
config_hash: ce31624b6bbb996036f4bcc4b9525ed6

src/replicate/__init__.py

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3+
from __future__ import annotations
4+
5+
from typing_extensions import override
6+
37
from . import types
48
from ._types import NOT_GIVEN, Omit, NoneType, NotGiven, Transport, ProxiesTypes
59
from ._utils import file_from_path
@@ -92,3 +96,146 @@
9296
except (TypeError, AttributeError):
9397
# Some of our exported symbols are builtins which we can't set attributes for.
9498
pass
99+
100+
# ------ Module level client ------
101+
import typing as _t
102+
103+
import httpx as _httpx
104+
105+
from ._base_client import DEFAULT_TIMEOUT, DEFAULT_MAX_RETRIES
106+
107+
bearer_token: str | None = None
108+
109+
base_url: str | _httpx.URL | None = None
110+
111+
timeout: float | Timeout | None = DEFAULT_TIMEOUT
112+
113+
max_retries: int = DEFAULT_MAX_RETRIES
114+
115+
default_headers: _t.Mapping[str, str] | None = None
116+
117+
default_query: _t.Mapping[str, object] | None = None
118+
119+
http_client: _httpx.Client | None = None
120+
121+
122+
class _ModuleClient(ReplicateClient):
123+
# Note: we have to use type: ignores here as overriding class members
124+
# with properties is technically unsafe but it is fine for our use case
125+
126+
@property # type: ignore
127+
@override
128+
def bearer_token(self) -> str | None:
129+
return bearer_token
130+
131+
@bearer_token.setter # type: ignore
132+
def bearer_token(self, value: str | None) -> None: # type: ignore
133+
global bearer_token
134+
135+
bearer_token = value
136+
137+
@property
138+
@override
139+
def base_url(self) -> _httpx.URL:
140+
if base_url is not None:
141+
return _httpx.URL(base_url)
142+
143+
return super().base_url
144+
145+
@base_url.setter
146+
def base_url(self, url: _httpx.URL | str) -> None:
147+
super().base_url = url # type: ignore[misc]
148+
149+
@property # type: ignore
150+
@override
151+
def timeout(self) -> float | Timeout | None:
152+
return timeout
153+
154+
@timeout.setter # type: ignore
155+
def timeout(self, value: float | Timeout | None) -> None: # type: ignore
156+
global timeout
157+
158+
timeout = value
159+
160+
@property # type: ignore
161+
@override
162+
def max_retries(self) -> int:
163+
return max_retries
164+
165+
@max_retries.setter # type: ignore
166+
def max_retries(self, value: int) -> None: # type: ignore
167+
global max_retries
168+
169+
max_retries = value
170+
171+
@property # type: ignore
172+
@override
173+
def _custom_headers(self) -> _t.Mapping[str, str] | None:
174+
return default_headers
175+
176+
@_custom_headers.setter # type: ignore
177+
def _custom_headers(self, value: _t.Mapping[str, str] | None) -> None: # type: ignore
178+
global default_headers
179+
180+
default_headers = value
181+
182+
@property # type: ignore
183+
@override
184+
def _custom_query(self) -> _t.Mapping[str, object] | None:
185+
return default_query
186+
187+
@_custom_query.setter # type: ignore
188+
def _custom_query(self, value: _t.Mapping[str, object] | None) -> None: # type: ignore
189+
global default_query
190+
191+
default_query = value
192+
193+
@property # type: ignore
194+
@override
195+
def _client(self) -> _httpx.Client:
196+
return http_client or super()._client
197+
198+
@_client.setter # type: ignore
199+
def _client(self, value: _httpx.Client) -> None: # type: ignore
200+
global http_client
201+
202+
http_client = value
203+
204+
205+
_client: ReplicateClient | None = None
206+
207+
208+
def _load_client() -> ReplicateClient: # type: ignore[reportUnusedFunction]
209+
global _client
210+
211+
if _client is None:
212+
_client = _ModuleClient(
213+
bearer_token=bearer_token,
214+
base_url=base_url,
215+
timeout=timeout,
216+
max_retries=max_retries,
217+
default_headers=default_headers,
218+
default_query=default_query,
219+
http_client=http_client,
220+
)
221+
return _client
222+
223+
return _client
224+
225+
226+
def _reset_client() -> None: # type: ignore[reportUnusedFunction]
227+
global _client
228+
229+
_client = None
230+
231+
232+
from ._module_client import (
233+
models as models,
234+
accounts as accounts,
235+
hardware as hardware,
236+
webhooks as webhooks,
237+
trainings as trainings,
238+
collections as collections,
239+
deployments as deployments,
240+
predictions as predictions,
241+
)

src/replicate/_module_client.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
from typing_extensions import override
4+
5+
from . import resources, _load_client
6+
from ._utils import LazyProxy
7+
8+
9+
class ModelsResourceProxy(LazyProxy[resources.ModelsResource]):
10+
@override
11+
def __load__(self) -> resources.ModelsResource:
12+
return _load_client().models
13+
14+
15+
class HardwareResourceProxy(LazyProxy[resources.HardwareResource]):
16+
@override
17+
def __load__(self) -> resources.HardwareResource:
18+
return _load_client().hardware
19+
20+
21+
class AccountsResourceProxy(LazyProxy[resources.AccountsResource]):
22+
@override
23+
def __load__(self) -> resources.AccountsResource:
24+
return _load_client().accounts
25+
26+
27+
class WebhooksResourceProxy(LazyProxy[resources.WebhooksResource]):
28+
@override
29+
def __load__(self) -> resources.WebhooksResource:
30+
return _load_client().webhooks
31+
32+
33+
class TrainingsResourceProxy(LazyProxy[resources.TrainingsResource]):
34+
@override
35+
def __load__(self) -> resources.TrainingsResource:
36+
return _load_client().trainings
37+
38+
39+
class CollectionsResourceProxy(LazyProxy[resources.CollectionsResource]):
40+
@override
41+
def __load__(self) -> resources.CollectionsResource:
42+
return _load_client().collections
43+
44+
45+
class DeploymentsResourceProxy(LazyProxy[resources.DeploymentsResource]):
46+
@override
47+
def __load__(self) -> resources.DeploymentsResource:
48+
return _load_client().deployments
49+
50+
51+
class PredictionsResourceProxy(LazyProxy[resources.PredictionsResource]):
52+
@override
53+
def __load__(self) -> resources.PredictionsResource:
54+
return _load_client().predictions
55+
56+
57+
models: resources.ModelsResource = ModelsResourceProxy().__as_proxied__()
58+
hardware: resources.HardwareResource = HardwareResourceProxy().__as_proxied__()
59+
accounts: resources.AccountsResource = AccountsResourceProxy().__as_proxied__()
60+
webhooks: resources.WebhooksResource = WebhooksResourceProxy().__as_proxied__()
61+
trainings: resources.TrainingsResource = TrainingsResourceProxy().__as_proxied__()
62+
collections: resources.CollectionsResource = CollectionsResourceProxy().__as_proxied__()
63+
deployments: resources.DeploymentsResource = DeploymentsResourceProxy().__as_proxied__()
64+
predictions: resources.PredictionsResource = PredictionsResourceProxy().__as_proxied__()

tests/test_module_client.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
from __future__ import annotations
4+
5+
import httpx
6+
import pytest
7+
from httpx import URL
8+
9+
import replicate
10+
from replicate import DEFAULT_TIMEOUT, DEFAULT_MAX_RETRIES
11+
12+
13+
def reset_state() -> None:
14+
replicate._reset_client()
15+
replicate.bearer_token = None or "My Bearer Token"
16+
replicate.base_url = None
17+
replicate.timeout = DEFAULT_TIMEOUT
18+
replicate.max_retries = DEFAULT_MAX_RETRIES
19+
replicate.default_headers = None
20+
replicate.default_query = None
21+
replicate.http_client = None
22+
23+
24+
@pytest.fixture(autouse=True)
25+
def reset_state_fixture() -> None:
26+
reset_state()
27+
28+
29+
def test_base_url_option() -> None:
30+
assert replicate.base_url is None
31+
assert replicate.collections._client.base_url == URL("https://api.replicate.com/v1/")
32+
33+
replicate.base_url = "http://foo.com"
34+
35+
assert replicate.base_url == URL("http://foo.com")
36+
assert replicate.collections._client.base_url == URL("http://foo.com")
37+
38+
39+
def test_timeout_option() -> None:
40+
assert replicate.timeout == replicate.DEFAULT_TIMEOUT
41+
assert replicate.collections._client.timeout == replicate.DEFAULT_TIMEOUT
42+
43+
replicate.timeout = 3
44+
45+
assert replicate.timeout == 3
46+
assert replicate.collections._client.timeout == 3
47+
48+
49+
def test_max_retries_option() -> None:
50+
assert replicate.max_retries == replicate.DEFAULT_MAX_RETRIES
51+
assert replicate.collections._client.max_retries == replicate.DEFAULT_MAX_RETRIES
52+
53+
replicate.max_retries = 1
54+
55+
assert replicate.max_retries == 1
56+
assert replicate.collections._client.max_retries == 1
57+
58+
59+
def test_default_headers_option() -> None:
60+
assert replicate.default_headers == None
61+
62+
replicate.default_headers = {"Foo": "Bar"}
63+
64+
assert replicate.default_headers["Foo"] == "Bar"
65+
assert replicate.collections._client.default_headers["Foo"] == "Bar"
66+
67+
68+
def test_default_query_option() -> None:
69+
assert replicate.default_query is None
70+
assert replicate.collections._client._custom_query == {}
71+
72+
replicate.default_query = {"Foo": {"nested": 1}}
73+
74+
assert replicate.default_query["Foo"] == {"nested": 1}
75+
assert replicate.collections._client._custom_query["Foo"] == {"nested": 1}
76+
77+
78+
def test_http_client_option() -> None:
79+
assert openai.http_client is None
80+
81+
original_http_client = openai.completions._client._client
82+
assert original_http_client is not None
83+
84+
new_client = httpx.Client()
85+
openai.http_client = new_client
86+
87+
assert openai.completions._client._client is new_client

0 commit comments

Comments
 (0)