Skip to content

Commit 3d229b5

Browse files
authored
Merge pull request #17 from depop/default-headers-support
Add default headers support
2 parents f48d422 + aa3d323 commit 3d229b5

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

README.rst

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ Some useful config keys (all of which are prefixed with
3838

3939
- ``POPGET_CLIENT_DEFAULT_USER_AGENT`` when making requests, popget will use this
4040
string as the user agent.
41+
- ``POPGET_CLIENT_DEFAULT_HEADERS`` when making requests, popget will add these
42+
headers by default to the request, but they can still be overridden when set
43+
explicitly.
4144
- ``POPGET_CLIENT_TIMEOUT`` if ``None`` then no timeout, otherwise this timeout
4245
(in seconds) will be applied to all requests. Requests which timeout will
4346
return a 504 response, which will be raised as an ``HTTPError``.

popget/__about__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '2.0.0'
1+
__version__ = '2.1.0'

popget/client.py

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ def __init__(self, config):
126126
def session(self) -> requests.Session:
127127
if not self._session:
128128
session = self.session_cls()
129+
session.headers.update(settings.CLIENT_DEFAULT_HEADERS)
129130
session.headers['User-Agent'] = settings.CLIENT_DEFAULT_USER_AGENT
130131
self._session = session
131132
return self._session

popget/conf/settings.py

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
settings, 'POPGET_CLIENT_DEFAULT_USER_AGENT', 'popget/{}'.format(__version__)
1010
)
1111

12+
CLIENT_DEFAULT_HEADERS: dict[str, str] = getattr(
13+
settings, 'POPGET_CLIENT_DEFAULT_HEADERS', {}
14+
)
15+
1216
CLIENT_TIMEOUT: float = getattr(settings, 'POPGET_CLIENT_TIMEOUT', 3.0)
1317

1418
CLIENT_DISABLE_VERIFY_SSL: bool = getattr(settings, 'POPGET_CLIENT_DISABLE_VERIFY_SSL', False)

tests/test_client.py

+28
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,31 @@ def test_request_header_args_ok():
167167
}
168168
assert endpoint.required_args == {'user_id', 'token', 'edition'}
169169

170+
@responses.activate
171+
def test_default_request_header_ok():
172+
"""
173+
Default request-header are present.
174+
"""
175+
with (
176+
patch(
177+
'popget.client.settings.CLIENT_DEFAULT_HEADERS',
178+
{'Accept-Encoding': 'gzip', 'X-Depop-Pointless': 'default'}
179+
)
180+
):
181+
def callback(request):
182+
assert 'Accept-Encoding' in request.headers
183+
assert 'gzip' in request.headers['Accept-Encoding']
184+
assert 'X-Depop-Pointless' in request.headers
185+
assert 'explicitly pointless' in request.headers['X-Depop-Pointless']
186+
187+
return (200, {}, '{"thing": "it\'s a thing"}')
188+
189+
responses.add_callback(responses.GET, 'http://example.com/v1/thing/777',
190+
callback=callback,
191+
content_type='application/json')
192+
193+
data = DummyService.thing_detail(id=777)
194+
assert len(responses.calls) == 1
170195

171196
def test_request_header_args_clash():
172197
"""
@@ -230,6 +255,9 @@ class Config:
230255
thing_detail = APIEndpoint(
231256
'GET',
232257
'/v1/thing/{id}',
258+
request_headers={
259+
'X-Depop-Pointless': 'explicitly pointless',
260+
}
233261
)
234262
thing_update = APIEndpoint(
235263
'PATCH',

0 commit comments

Comments
 (0)