Skip to content

Commit

Permalink
Merge pull request #17 from depop/default-headers-support
Browse files Browse the repository at this point in the history
Add default headers support
  • Loading branch information
luismfonseca authored Jun 27, 2023
2 parents f48d422 + aa3d323 commit 3d229b5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ Some useful config keys (all of which are prefixed with

- ``POPGET_CLIENT_DEFAULT_USER_AGENT`` when making requests, popget will use this
string as the user agent.
- ``POPGET_CLIENT_DEFAULT_HEADERS`` when making requests, popget will add these
headers by default to the request, but they can still be overridden when set
explicitly.
- ``POPGET_CLIENT_TIMEOUT`` if ``None`` then no timeout, otherwise this timeout
(in seconds) will be applied to all requests. Requests which timeout will
return a 504 response, which will be raised as an ``HTTPError``.
Expand Down
2 changes: 1 addition & 1 deletion popget/__about__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.0.0'
__version__ = '2.1.0'
1 change: 1 addition & 0 deletions popget/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def __init__(self, config):
def session(self) -> requests.Session:
if not self._session:
session = self.session_cls()
session.headers.update(settings.CLIENT_DEFAULT_HEADERS)
session.headers['User-Agent'] = settings.CLIENT_DEFAULT_USER_AGENT
self._session = session
return self._session
Expand Down
4 changes: 4 additions & 0 deletions popget/conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
settings, 'POPGET_CLIENT_DEFAULT_USER_AGENT', 'popget/{}'.format(__version__)
)

CLIENT_DEFAULT_HEADERS: dict[str, str] = getattr(
settings, 'POPGET_CLIENT_DEFAULT_HEADERS', {}
)

CLIENT_TIMEOUT: float = getattr(settings, 'POPGET_CLIENT_TIMEOUT', 3.0)

CLIENT_DISABLE_VERIFY_SSL: bool = getattr(settings, 'POPGET_CLIENT_DISABLE_VERIFY_SSL', False)
28 changes: 28 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,31 @@ def test_request_header_args_ok():
}
assert endpoint.required_args == {'user_id', 'token', 'edition'}

@responses.activate
def test_default_request_header_ok():
"""
Default request-header are present.
"""
with (
patch(
'popget.client.settings.CLIENT_DEFAULT_HEADERS',
{'Accept-Encoding': 'gzip', 'X-Depop-Pointless': 'default'}
)
):
def callback(request):
assert 'Accept-Encoding' in request.headers
assert 'gzip' in request.headers['Accept-Encoding']
assert 'X-Depop-Pointless' in request.headers
assert 'explicitly pointless' in request.headers['X-Depop-Pointless']

return (200, {}, '{"thing": "it\'s a thing"}')

responses.add_callback(responses.GET, 'http://example.com/v1/thing/777',
callback=callback,
content_type='application/json')

data = DummyService.thing_detail(id=777)
assert len(responses.calls) == 1

def test_request_header_args_clash():
"""
Expand Down Expand Up @@ -230,6 +255,9 @@ class Config:
thing_detail = APIEndpoint(
'GET',
'/v1/thing/{id}',
request_headers={
'X-Depop-Pointless': 'explicitly pointless',
}
)
thing_update = APIEndpoint(
'PATCH',
Expand Down

0 comments on commit 3d229b5

Please sign in to comment.