1313# limitations under the License.
1414
1515from abc import ABC
16- from requests import Session
16+ from requests import Session , Response
1717from requests .adapters import HTTPAdapter
1818from requests .auth import AuthBase
19+ from typing import Callable , Dict , Optional , Tuple
1920from urllib3 .util .retry import Retry
2021
2122from planet_auth .auth_client import AuthClientException
2223from planet_auth .constants import X_PLANET_APP_HEADER , X_PLANET_APP
2324from planet_auth .util import parse_content_type
2425
26+ EnricherPayloadType = Dict
27+ # EnricherAudType = str
28+ EnricherReturnType = Tuple [Dict , Optional [AuthBase ]]
29+ EnricherFuncType = Callable [[EnricherPayloadType , str ], EnricherReturnType ]
30+
31+ _RequestAuthType = AuthBase
32+ _RequestParamsType = Dict # Requests allows a lot more, but constrain our use.
33+ _RequestResponseType = Response
34+
2535
2636class OidcApiClient (ABC ):
2737 """
@@ -34,6 +44,8 @@ class OidcApiClient(ABC):
3444 # Generally, this will be some combination of the client ID and secret
3545 # and may be a header or payload adjustment. But sometimes, we just
3646 # need to use an Authorization header.
47+ # TODO: dog-food - use our own RequestAuthenticator like we do for the
48+ # static API key auth client
3749 class TokenBearerAuth (AuthBase ):
3850 def __init__ (self , token ):
3951 self ._token = token
@@ -42,7 +54,7 @@ def __call__(self, r):
4254 r .headers ["Authorization" ] = "Bearer " + self ._token
4355 return r
4456
45- def __init__ (self , endpoint_uri ):
57+ def __init__ (self , endpoint_uri : str ):
4658 self ._endpoint_uri = endpoint_uri
4759
4860 retry_strategy = Retry (total = 3 , backoff_factor = 1 , status_forcelist = [429 ], allowed_methods = ["POST" , "GET" ])
@@ -51,7 +63,7 @@ def __init__(self, endpoint_uri):
5163 self ._session .mount ("https://" , adapter )
5264 # self._session.mount("http://", adapter)
5365
54- def __check_http_error (self , response ) :
66+ def __check_http_error (self , response : _RequestResponseType ) -> None :
5567 if not response .ok :
5668 raise OidcApiClientException (
5769 message = "HTTP error from OIDC endpoint at {}: {}: {}" .format (
@@ -60,7 +72,7 @@ def __check_http_error(self, response):
6072 raw_response = response ,
6173 )
6274
63- def __check_oidc_payload_json_error (self , response ) :
75+ def __check_oidc_payload_json_error (self , response : _RequestResponseType ) -> None :
6476 if response .content :
6577 ct = parse_content_type (response .headers .get ("content-type" ))
6678 if not ct ["content-type" ] == "application/json" :
@@ -89,7 +101,7 @@ def __check_oidc_payload_json_error(self, response):
89101 )
90102
91103 @staticmethod
92- def __checked_json_response (response ) :
104+ def __checked_json_response (response : _RequestResponseType ) -> Dict :
93105 json_response = None
94106 if response .content :
95107 ct = parse_content_type (response .headers .get ("content-type" ))
@@ -106,13 +118,15 @@ def __checked_json_response(response):
106118 )
107119 return json_response
108120
109- def __check_response (self , response ) :
121+ def __check_response (self , response : _RequestResponseType ) -> None :
110122 # Check for the json error first so we throw a more specific parsed
111123 # error if we understand it, regardless of HTTP status code.
112124 self .__check_oidc_payload_json_error (response )
113125 self .__check_http_error (response )
114126
115- def _checked_get (self , params , request_auth ):
127+ def _checked_get (
128+ self , params : Optional [_RequestParamsType ], request_auth : Optional [_RequestAuthType ]
129+ ) -> _RequestResponseType :
116130 response = self ._session .get (
117131 self ._endpoint_uri ,
118132 params = params ,
@@ -122,7 +136,9 @@ def _checked_get(self, params, request_auth):
122136 self .__check_response (response )
123137 return response
124138
125- def _checked_post (self , params , request_auth ):
139+ def _checked_post (
140+ self , params : Optional [_RequestParamsType ], request_auth : Optional [_RequestAuthType ]
141+ ) -> _RequestResponseType :
126142 response = self ._session .post (
127143 self ._endpoint_uri ,
128144 # Note: is the data/params crossing confusing? This was born out
@@ -139,10 +155,14 @@ def _checked_post(self, params, request_auth):
139155 self .__check_response (response )
140156 return response
141157
142- def _checked_post_json_response (self , params , request_auth ):
158+ def _checked_post_json_response (
159+ self , params : Optional [_RequestParamsType ], request_auth : Optional [_RequestAuthType ]
160+ ) -> Dict :
143161 return self .__checked_json_response (self ._checked_post (params , request_auth ))
144162
145- def _checked_get_json_response (self , params , request_auth ):
163+ def _checked_get_json_response (
164+ self , params : Optional [_RequestParamsType ], request_auth : Optional [_RequestAuthType ]
165+ ) -> Dict :
146166 return self .__checked_json_response (self ._checked_get (params , request_auth ))
147167
148168
0 commit comments