Skip to content

Apply custom headers passed to client constructor #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions replicate/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,8 @@ def _build_httpx_client(
timeout: Optional[httpx.Timeout] = None,
**kwargs,
) -> Union[httpx.Client, httpx.AsyncClient]:
headers = {
"User-Agent": f"replicate-python/{__version__}",
}
headers = kwargs.pop("headers", {})
headers["User-Agent"] = f"replicate-python/{__version__}"

if (
api_token := api_token or os.environ.get("REPLICATE_API_TOKEN")
Expand Down
24 changes: 24 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,27 @@ async def test_server_error_handling():
client._request("GET", "/")
assert "status: 500" in str(exc_info.value)
assert "detail: Server error occurred" in str(exc_info.value)


def test_custom_headers_are_applied():
import replicate
from replicate.exceptions import ReplicateError

custom_headers = {"Custom-Header": "CustomValue"}

def mock_send(request: httpx.Request, **kwargs) -> httpx.Response:
assert "Custom-Header" in request.headers
assert request.headers["Custom-Header"] == "CustomValue"

return httpx.Response(401, json={})

client = replicate.Client(
api_token="dummy_token",
headers=custom_headers,
transport=httpx.MockTransport(mock_send),
)

try:
client.accounts.current()
except ReplicateError:
pass