Skip to content
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
6 changes: 6 additions & 0 deletions asknews_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def __init__(
self.client_secret = client_secret
self.scopes = {"offline", "openid", *(scopes or set())}
self.api_key = api_key
self.user_agent = user_agent
self.base_url = base_url
self.token_url = token_url
self.verify_ssl = verify_ssl
Expand Down Expand Up @@ -132,6 +133,11 @@ def build_api_request(

headers["accept"] = build_accept_header(accept or [("application/json", 1.0)])

# The request is sent with ``client.send``, which does not merge the client's
# default headers, so the User-Agent has to be set on the request itself.
if not any(key.lower() == "user-agent" for key in headers):
headers["user-agent"] = self.user_agent

return Request(
method=method,
url=build_url(
Expand Down
55 changes: 53 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import pytest
from httpx import Request
import respx
from httpx import Request, Response
from respx.router import MockRouter

from asknews_sdk.client import APIClient, AsyncAPIClient
from asknews_sdk.client import USER_AGENT, APIClient, AsyncAPIClient
from asknews_sdk.errors import APIError
from asknews_sdk.response import APIResponse, AsyncAPIResponse
from tests.conftest import BASE_URL
Expand Down Expand Up @@ -205,3 +206,53 @@ async def test_async_client_api_error(

assert exc_info.value.code == 404000
assert exc_info.value.detail == "Not Found"


def test_build_api_request_user_agent(sync_api_client: APIClient):
request = sync_api_client.build_api_request("GET", "/test")

assert request.headers.get("user-agent") == USER_AGENT
assert USER_AGENT.startswith("asknews-sdk-python/")

request = sync_api_client.build_api_request(
"GET", "/test", headers={"User-Agent": "custom-agent/1.0"}
)

assert request.headers.get("user-agent") == "custom-agent/1.0"


def test_custom_user_agent():
with APIClient(
client_id=None,
client_secret=None,
scopes=None,
api_key="ank_test",
base_url=BASE_URL,
token_url=None,
user_agent="my-app/2.0",
) as client:
request = client.build_api_request("GET", "/test")

assert request.headers.get("user-agent") == "my-app/2.0"


@respx.mock
def test_request_sends_user_agent(sync_api_client: APIClient, respx_mock: MockRouter):
route = respx_mock.get(f"{BASE_URL}/test").mock(return_value=Response(200, json={}))

sync_api_client.request("GET", "/test")

assert route.called
assert route.calls.last.request.headers.get("user-agent") == USER_AGENT


@respx.mock
async def test_async_request_sends_user_agent(
async_api_client: AsyncAPIClient, respx_mock: MockRouter
):
route = respx_mock.get(f"{BASE_URL}/test").mock(return_value=Response(200, json={}))

await async_api_client.request("GET", "/test")

assert route.called
assert route.calls.last.request.headers.get("user-agent") == USER_AGENT
Loading
Loading