Skip to content

Commit a315d17

Browse files
authored
Add constructor args http_client and async_http_client so that users can provide their own instances. (#227)
1 parent f0cb7e2 commit a315d17

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

src/sumo/wrapper/sumo_client.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def __init__(
3434
retry_strategy=RetryStrategy(),
3535
timeout=DEFAULT_TIMEOUT,
3636
case_uuid=None,
37+
http_client=None,
38+
async_http_client=None,
3739
):
3840
"""Initialize a new Sumo object
3941
@@ -54,8 +56,19 @@ def __init__(
5456
self._verbosity = verbosity
5557

5658
self._retry_strategy = retry_strategy
57-
self._client = httpx.Client()
58-
self._async_client = httpx.AsyncClient()
59+
if http_client is None:
60+
self._client = httpx.Client()
61+
self._borrowed_client = False
62+
else:
63+
self._client = http_client
64+
self._borrowed_client = True
65+
66+
if async_http_client is None:
67+
self._async_client = httpx.AsyncClient()
68+
self._borrowed_async_client = False
69+
else:
70+
self._async_client = async_http_client
71+
self._borrowed_async_client = True
5972

6073
self._timeout = timeout
6174

@@ -105,24 +118,26 @@ def __enter__(self):
105118
return self
106119

107120
def __exit__(self, exc_type, exc_value, traceback):
108-
self._client.close()
121+
if not self._borrowed_client:
122+
self._client.close()
109123
self._client = None
110124
return False
111125

112126
async def __aenter__(self):
113127
return self
114128

115129
async def __aexit__(self, exc_type, exc_value, traceback):
116-
await self._async_client.aclose()
130+
if not self._borrowed_async_client:
131+
await self._async_client.aclose()
117132
self._async_client = None
118133
return False
119134

120135
def __del__(self):
121-
if self._client is not None:
136+
if self._client is not None and not self._borrowed_client:
122137
self._client.close()
123-
self._client = None
124138
pass
125-
if self._async_client is not None:
139+
self._client = None
140+
if self._async_client is not None and not self._borrowed_async_client:
126141

127142
async def closeit(client):
128143
await client.aclose()
@@ -133,8 +148,8 @@ async def closeit(client):
133148
loop.create_task(closeit(self._async_client))
134149
except RuntimeError:
135150
pass
136-
self._async_client = None
137151
pass
152+
self._async_client = None
138153

139154
def authenticate(self):
140155
if self.auth is None:

0 commit comments

Comments
 (0)