diff --git a/README.rst b/README.rst index 06cb2a3..1723e2d 100644 --- a/README.rst +++ b/README.rst @@ -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``. diff --git a/popget/__about__.py b/popget/__about__.py index afced14..a33997d 100644 --- a/popget/__about__.py +++ b/popget/__about__.py @@ -1 +1 @@ -__version__ = '2.0.0' +__version__ = '2.1.0' diff --git a/popget/client.py b/popget/client.py index e1b4302..24f2af6 100644 --- a/popget/client.py +++ b/popget/client.py @@ -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 diff --git a/popget/conf/settings.py b/popget/conf/settings.py index 37d91d2..d080696 100644 --- a/popget/conf/settings.py +++ b/popget/conf/settings.py @@ -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) diff --git a/tests/test_client.py b/tests/test_client.py index e0d067c..a161439 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -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(): """ @@ -230,6 +255,9 @@ class Config: thing_detail = APIEndpoint( 'GET', '/v1/thing/{id}', + request_headers={ + 'X-Depop-Pointless': 'explicitly pointless', + } ) thing_update = APIEndpoint( 'PATCH',