33import logging
44import re
55import time
6+ from typing import Dict , Optional , Tuple
67
78import httpx
89import jwt
2526class SumoClient :
2627 """Authenticate and perform requests to the Sumo API."""
2728
29+ _client : httpx .Client
30+ _async_client : httpx .AsyncClient
31+
2832 def __init__ (
2933 self ,
3034 env : str ,
31- token : str = None ,
35+ token : Optional [ str ] = None ,
3236 interactive : bool = False ,
3337 devicecode : bool = False ,
3438 verbosity : str = "CRITICAL" ,
@@ -119,26 +123,23 @@ def __init__(
119123 def __enter__ (self ):
120124 return self
121125
122- def __exit__ (self , exc_type , exc_value , traceback ):
126+ def __exit__ (self , * _ ):
123127 if not self ._borrowed_client :
124128 self ._client .close ()
125- self ._client = None
126129 return False
127130
128131 async def __aenter__ (self ):
129132 return self
130133
131- async def __aexit__ (self , exc_type , exc_value , traceback ):
134+ async def __aexit__ (self , * _ ):
132135 if not self ._borrowed_async_client :
133136 await self ._async_client .aclose ()
134- self ._async_client = None
135137 return False
136138
137139 def __del__ (self ):
138140 if self ._client is not None and not self ._borrowed_client :
139141 self ._client .close ()
140142 pass
141- self ._client = None
142143 if self ._async_client is not None and not self ._borrowed_async_client :
143144
144145 async def closeit (client ):
@@ -151,7 +152,6 @@ async def closeit(client):
151152 except RuntimeError :
152153 pass
153154 pass
154- self ._async_client = None
155155
156156 def authenticate (self ):
157157 if self .auth is None :
@@ -186,7 +186,7 @@ def blob_client(self) -> BlobClient:
186186 )
187187
188188 @raise_for_status
189- def get (self , path : str , params : dict = None ) -> dict :
189+ def get (self , path : str , params : Optional [ Dict ] = None ) -> httpx . Response :
190190 """Performs a GET-request to the Sumo API.
191191
192192 Args:
@@ -247,9 +247,9 @@ def _get():
247247 def post (
248248 self ,
249249 path : str ,
250- blob : bytes = None ,
251- json : dict = None ,
252- params : dict = None ,
250+ blob : Optional [ bytes ] = None ,
251+ json : Optional [ dict ] = None ,
252+ params : Optional [ dict ] = None ,
253253 ) -> httpx .Response :
254254 """Performs a POST-request to the Sumo API.
255255
@@ -320,7 +320,10 @@ def _post():
320320
321321 @raise_for_status
322322 def put (
323- self , path : str , blob : bytes = None , json : dict = None
323+ self ,
324+ path : str ,
325+ blob : Optional [bytes ] = None ,
326+ json : Optional [dict ] = None ,
324327 ) -> httpx .Response :
325328 """Performs a PUT-request to the Sumo API.
326329
@@ -365,7 +368,9 @@ def _put():
365368 return retryer (_put )
366369
367370 @raise_for_status
368- def delete (self , path : str , params : dict = None ) -> dict :
371+ def delete (
372+ self , path : str , params : Optional [dict ] = None
373+ ) -> httpx .Response :
369374 """Performs a DELETE-request to the Sumo API.
370375
371376 Args:
@@ -402,12 +407,12 @@ def _delete():
402407
403408 return retryer (_delete )
404409
405- def _get_retry_details (self , response_in ):
410+ def _get_retry_details (self , response_in ) -> Tuple [ str , int ] :
406411 assert response_in .status_code == 202 , (
407412 "Incorrect status code; expcted 202"
408413 )
409414 headers = response_in .headers
410- location = headers .get ("location" )
415+ location : str = headers .get ("location" )
411416 assert location is not None , "Missing header: Location"
412417 assert location .startswith (self .base_url )
413418 retry_after = headers .get ("retry-after" )
@@ -440,7 +445,6 @@ def poll(
440445 )
441446 location , retry_after = self ._get_retry_details (response )
442447 pass
443- return None # should never get here.
444448
445449 def getLogger (self , name ):
446450 """Gets a logger object that sends log objects into the message_log
@@ -495,7 +499,9 @@ def client_for_case(self, case_uuid):
495499 return self
496500
497501 @raise_for_status_async
498- async def get_async (self , path : str , params : dict = None ):
502+ async def get_async (
503+ self , path : str , params : Optional [dict ] = None
504+ ) -> httpx .Response :
499505 """Performs an async GET-request to the Sumo API.
500506
501507 Args:
@@ -556,9 +562,9 @@ async def _get():
556562 async def post_async (
557563 self ,
558564 path : str ,
559- blob : bytes = None ,
560- json : dict = None ,
561- params : dict = None ,
565+ blob : Optional [ bytes ] = None ,
566+ json : Optional [ dict ] = None ,
567+ params : Optional [ dict ] = None ,
562568 ) -> httpx .Response :
563569 """Performs an async POST-request to the Sumo API.
564570
@@ -630,7 +636,10 @@ async def _post():
630636
631637 @raise_for_status_async
632638 async def put_async (
633- self , path : str , blob : bytes = None , json : dict = None
639+ self ,
640+ path : str ,
641+ blob : Optional [bytes ] = None ,
642+ json : Optional [dict ] = None ,
634643 ) -> httpx .Response :
635644 """Performs an async PUT-request to the Sumo API.
636645
@@ -675,7 +684,9 @@ async def _put():
675684 return await retryer (_put )
676685
677686 @raise_for_status_async
678- async def delete_async (self , path : str , params : dict = None ) -> dict :
687+ async def delete_async (
688+ self , path : str , params : Optional [dict ] = None
689+ ) -> httpx .Response :
679690 """Performs an async DELETE-request to the Sumo API.
680691
681692 Args:
@@ -736,4 +747,3 @@ async def poll_async(
736747 )
737748 location , retry_after = self ._get_retry_details (response )
738749 pass
739- return None # should never get here.
0 commit comments