Skip to content

Commit c79eedc

Browse files
authored
feat: Add the ability to pass a requests Session to Client (#275)
fixes #254
1 parent c930530 commit c79eedc

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

cert_manager/client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ def __init__(self, **kwargs):
4040
auth_url: The full URL to the Sectigo OAuth2 token endpoint; the default is "https://auth.sso.sectigo.com/auth/realms/apiclients/protocol/openid-connect/token"
4141
client_id: The Client ID to use for OAuth2 authentication
4242
client_secret: The Client Secret to use for OAuth2 authentication
43+
session: A requests.Session object to use instead of creating a new one; the default is None,
44+
which will create a new session
4345
"""
4446
# Initialize class variables
4547
self._base_url = None
@@ -51,7 +53,8 @@ def __init__(self, **kwargs):
5153
self._user_key_file = None
5254
self._username = None
5355

54-
self._session = requests.Session()
56+
self._session = kwargs.get("session", requests.Session())
57+
5558
# Set the default HTTP headers
5659
self._headers = {
5760
"Accept": "application/json",

tests/test_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from unittest import mock
1212

1313
import pytest
14+
import requests
1415
import responses
1516
from requests.exceptions import HTTPError
1617
from testtools import TestCase
@@ -138,6 +139,24 @@ def test_versioning(self):
138139
self.assertEqual(client.headers["User-Agent"], user_agent)
139140
self.assertEqual(client._session.headers["User-Agent"], user_agent)
140141

142+
def test_session_passed(self):
143+
"""Test that a passed session is used."""
144+
session = requests.Session()
145+
client = Client(
146+
base_url=self.cfixt.base_url,
147+
login_uri=self.cfixt.login_uri,
148+
username=self.cfixt.username,
149+
password=self.cfixt.password,
150+
session=session,
151+
)
152+
self.assertIs(client._session, session)
153+
154+
def test_session_created(self):
155+
"""Test that a session is created if not passed."""
156+
# The setUp method already creates a client without a session passed
157+
self.assertIsInstance(self.client._session, requests.Session)
158+
self.assertIsNotNone(self.client._session)
159+
141160
def test_need_crt(self):
142161
"""Raise an exception without a cert file if cert_auth=True."""
143162
self.assertRaises(KeyError, Client, base_url=self.cfixt.base_url, login_uri=self.cfixt.login_uri,

0 commit comments

Comments
 (0)