Skip to content

Commit

Permalink
support cert auth when talking to the API
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeni committed Jan 10, 2025
1 parent 5278e0d commit c0e6378
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions apypie/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class Api(object):
:param uri: base URL of the server
:param username: username to access the API
:param password: password to access the API
:param client_cert: client cert to access the API
:param client_key: client key to access the API
:param api_version: version of the API. Defaults to `1`
:param language: prefered locale for the API description
:param apidoc_cache_base_dir: base directory for building apidoc_cache_dir. Defaults to `~/.cache/apipie_bindings`.
Expand Down Expand Up @@ -80,6 +82,9 @@ def __init__(self, **kwargs):
if kwargs.get('username') and kwargs.get('password'):
self._session.auth = (kwargs['username'], kwargs['password'])

if kwargs.get('client_cert') and kwargs.get('client_key'):
self._session.cert = (kwargs['client_cert'], kwargs['client_key'])

self._apidoc = None

@property
Expand Down
13 changes: 13 additions & 0 deletions tests/test_apypie.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ def test_init_auth(apidoc_cache_dir, username, password, expected):
assert api._session.auth == expected


@pytest.mark.parametrize('client_cert,client_key,expected', [
(None, None, None),
('client.crt', None, None),
(None, 'client.key', None),
('client.crt', 'client.key', ('client.crt', 'client.key')),
])
def test_init_cert(apidoc_cache_dir, client_cert, client_key, expected):
api = apypie.Api(uri='https://api.example.com', apidoc_cache_dir=apidoc_cache_dir.strpath,
client_cert=client_cert, client_key=client_key)

assert api._session.cert == expected


def test_init_language(apidoc_cache_dir):
api = apypie.Api(uri='https://api.example.com', apidoc_cache_dir=apidoc_cache_dir.strpath,
language='tlh')
Expand Down

0 comments on commit c0e6378

Please sign in to comment.