Skip to content

Commit

Permalink
allow overriding the session object to pass customized ones
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeni committed Apr 11, 2023
1 parent c93eb1e commit 9e2590b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion apypie/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<URI>`.
: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::
Expand All @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_apypie.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

import apypie
import requests
import json


Expand Down Expand Up @@ -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', '/')

0 comments on commit 9e2590b

Please sign in to comment.