11from contextlib import asynccontextmanager
22from http import HTTPStatus
3- from typing import Optional
3+ from typing import Optional , Any , AsyncGenerator
44
55from aiohttp import ClientSession , ClientResponseError
66
7- from pdap_access_manager import RequestType , DataSourcesNamespaces , SourceCollectorNamespaces , ResponseInfo , RequestInfo
8- from pdap_access_manager .constants import DEFAULT_DATA_SOURCES_URL , DEFAULT_SOURCE_COLLECTOR_URL
7+ from pdap_access_manager .src .constants import DEFAULT_DATA_SOURCES_URL , DEFAULT_SOURCE_COLLECTOR_URL
8+ from pdap_access_manager .src .enums import RequestType , DataSourcesNamespaces , SourceCollectorNamespaces
9+ from pdap_access_manager .src .models .request import RequestInfo
10+ from pdap_access_manager .src .models .response import ResponseInfo
911
1012request_methods = {
1113 RequestType .POST : ClientSession .post ,
@@ -45,7 +47,7 @@ def __init__(
4547 self .source_collector_url = source_collector_url
4648
4749 @asynccontextmanager
48- async def with_session (self ) -> "AccessManager" :
50+ async def with_session (self ) -> AsyncGenerator [ "AccessManager" , Any ] :
4951 """Allows just the session lifecycle to be managed."""
5052 created_session = False
5153 if self .session is None :
@@ -102,10 +104,7 @@ async def access_token(self) -> str:
102104 :return:
103105 """
104106 if self ._access_token is None :
105- await self .login (
106- email = self .email ,
107- password = self .password
108- )
107+ await self .login ()
109108 return self ._access_token
110109
111110 @property
@@ -115,10 +114,7 @@ async def refresh_token(self) -> str:
115114 :return:
116115 """
117116 if self ._refresh_token is None :
118- await self .login (
119- email = self .email ,
120- password = self .password
121- )
117+ await self .login ()
122118 return self ._refresh_token
123119
124120 async def load_api_key (self ):
@@ -159,7 +155,7 @@ async def refresh_access_token(self):
159155 self ._refresh_token = data ['refresh_token' ]
160156 except ClientResponseError as e :
161157 if e .status == HTTPStatus .UNAUTHORIZED : # Token expired, retry logging in
162- await self .login (self . email , self . password )
158+ await self .login ()
163159
164160 async def make_request (self , ri : RequestInfo , allow_retry : bool = True ) -> ResponseInfo :
165161 """
@@ -187,11 +183,9 @@ async def make_request(self, ri: RequestInfo, allow_retry: bool = True) -> Respo
187183 raise e
188184
189185
190- async def login (self , email : str , password : str ) :
186+ async def login (self ) -> None :
191187 """
192188 Login to PDAP and retrieve access and refresh tokens
193- :param email:
194- :param password:
195189 :return:
196190 """
197191 url = self .build_url (
@@ -202,8 +196,8 @@ async def login(self, email: str, password: str):
202196 type_ = RequestType .POST ,
203197 url = url ,
204198 json_ = {
205- "email" : email ,
206- "password" : password
199+ "email" : self . email ,
200+ "password" : self . password
207201 }
208202 )
209203 response_info = await self .make_request (request_info )
0 commit comments