File tree 4 files changed +43
-10
lines changed
4 files changed +43
-10
lines changed Original file line number Diff line number Diff line change @@ -359,6 +359,33 @@ training = replicate.trainings.create(
359
359
)
360
360
```
361
361
362
+ ## Customize client behavior
363
+
364
+ The ` replicate ` package exports a default shared client.
365
+ This client is initialized with an API token
366
+ set by the ` REPLICATE_API_TOKEN ` environment variable.
367
+
368
+ You can create your own client instance to
369
+ pass a different API token value,
370
+ add custom headers to requests,
371
+ or control the behavior of the underlying [ HTTPX client] ( https://www.python-httpx.org/api/#client ) :
372
+
373
+ ``` python
374
+ import os
375
+ from replicate.client import Client
376
+
377
+ replicate = Client(
378
+ api_token = os.environ[" SOME_OTHER_REPLICATE_API_TOKEN" ]
379
+ headers = {
380
+ " User-Agent" : " my-app/1.0
381
+ }
382
+ )
383
+ ```
384
+
385
+ > [ !WARNING]
386
+ > Never hardcode authentication credentials like API tokens into your code.
387
+ > Instead, pass them as environment variables when running your program.
388
+
362
389
## Development
363
390
364
391
See [ CONTRIBUTING.md] ( CONTRIBUTING.md )
Original file line number Diff line number Diff line change @@ -80,4 +80,5 @@ ignore = [
80
80
" S101" , # Use of assert
81
81
" S106" , # Possible use of hard-coded password function arguments
82
82
" ANN201" , # Missing return type annotation for public function
83
+ " ANN202" , # Missing return type annotation for private function
83
84
]
Original file line number Diff line number Diff line change @@ -330,11 +330,11 @@ def _build_httpx_client(
330
330
** kwargs ,
331
331
) -> Union [httpx .Client , httpx .AsyncClient ]:
332
332
headers = kwargs .pop ("headers" , {})
333
- headers [ "User-Agent" ] = f"replicate-python/ { __version__ } "
334
-
335
- if (
333
+ if "User-Agent" not in headers :
334
+ headers [ "User-Agent" ] = f"replicate-python/ { __version__ } "
335
+ if "Authorization" not in headers and (
336
336
api_token := api_token or os .environ .get ("REPLICATE_API_TOKEN" )
337
- ) and api_token != "" :
337
+ ):
338
338
headers ["Authorization" ] = f"Bearer { api_token } "
339
339
340
340
base_url = (
Original file line number Diff line number Diff line change @@ -91,21 +91,26 @@ def test_custom_headers_are_applied():
91
91
import replicate
92
92
from replicate .exceptions import ReplicateError
93
93
94
- custom_headers = {"Custom-Header" : "CustomValue" }
95
-
96
- def mock_send (request : httpx .Request , ** kwargs ) -> httpx .Response :
97
- assert "Custom-Header" in request .headers
98
- assert request .headers ["Custom-Header" ] == "CustomValue"
94
+ custom_headers = {"User-Agent" : "my-custom-user-agent/1.0" }
99
95
96
+ def mock_send (request ):
97
+ assert "User-Agent" in request .headers , "Custom header not found in request"
98
+ assert (
99
+ request .headers ["User-Agent" ] == "my-custom-user-agent/1.0"
100
+ ), "Custom header value is incorrect"
100
101
return httpx .Response (401 , json = {})
101
102
103
+ mock_send_wrapper = mock .Mock (side_effect = mock_send )
104
+
102
105
client = replicate .Client (
103
106
api_token = "dummy_token" ,
104
107
headers = custom_headers ,
105
- transport = httpx .MockTransport (mock_send ),
108
+ transport = httpx .MockTransport (mock_send_wrapper ),
106
109
)
107
110
108
111
try :
109
112
client .accounts .current ()
110
113
except ReplicateError :
111
114
pass
115
+
116
+ mock_send_wrapper .assert_called_once ()
You can’t perform that action at this time.
0 commit comments