diff --git a/apypie/api.py b/apypie/api.py index 9722099..d5c506f 100644 --- a/apypie/api.py +++ b/apypie/api.py @@ -48,6 +48,7 @@ class Api(object): :param apidoc_cache_dir: where to cache the JSON description of the API. Defaults to `apidoc_cache_base_dir/`. :param apidoc_cache_name: name of the cache file. If there is cache in the `apidoc_cache_dir`, it is used. Defaults to `default`. :param verify_ssl: should the SSL certificate be verified. Defaults to `True`. + :param session: a `requests.Session` compatible object. Defaults to `requests.Session()`. Usage:: @@ -72,7 +73,7 @@ def __init__(self, **kwargs): self.apidoc_cache_dir = kwargs.get('apidoc_cache_dir', apidoc_cache_dir_default) self.apidoc_cache_name = kwargs.get('apidoc_cache_name', self._find_cache_name()) - self._session = requests.Session() + self._session = kwargs.get('session') or requests.Session() self._session.verify = kwargs.get('verify_ssl', True) self._session.headers['Accept'] = 'application/json;version={}'.format(self.api_version) diff --git a/tests/test_apypie.py b/tests/test_apypie.py index cc25f88..4d2cdf8 100644 --- a/tests/test_apypie.py +++ b/tests/test_apypie.py @@ -2,6 +2,7 @@ import pytest import apypie +import requests import json @@ -314,3 +315,15 @@ def test_validate_cache_path_traversal(fixture_dir, requests_mock, tmp_xdg_cache assert tmp_xdg_cache_home.join('apypie', 'https___api.example.com', 'v1', 'default.json').check(exists=0) assert api.apidoc assert tmp_xdg_cache_home.join('apypie', 'https___api.example.com', 'v1', 'testcache.json').check(file=1) + + +def test_custom_session(fixture_dir, requests_mock, tmpdir): + headers = {'X-Apypie-Test': 'Custom'} + + my_session = requests.Session() + my_session.headers = headers + + my_api = apypie.Api(uri='https://api.example.com', session=my_session) + + requests_mock.get('https://api.example.com/', request_headers=headers, text='{}') + my_api.http_call('get', '/')