directly integrate login assertion in requests #27
@ -7,7 +7,7 @@ import requests
|
|||||||
from dopt_basics.io import combine_route
|
from dopt_basics.io import combine_route
|
||||||
from pydantic import BaseModel, PositiveInt, SkipValidation
|
from pydantic import BaseModel, PositiveInt, SkipValidation
|
||||||
|
|
||||||
from delta_barth.constants import API_CON_TIMEOUT
|
from delta_barth.constants import API_CON_TIMEOUT, MAX_LOGIN_RETRIES
|
||||||
from delta_barth.errors import STATUS_HANDLER
|
from delta_barth.errors import STATUS_HANDLER
|
||||||
from delta_barth.types import DelBarApiError, ExportResponse, ResponseType, Status
|
from delta_barth.types import DelBarApiError, ExportResponse, ResponseType, Status
|
||||||
|
|
||||||
@ -71,12 +71,18 @@ def get_sales_prognosis_data(
|
|||||||
)
|
)
|
||||||
empty_response = SalesPrognosisResponse(daten=tuple())
|
empty_response = SalesPrognosisResponse(daten=tuple())
|
||||||
try:
|
try:
|
||||||
resp = requests.get(
|
for attempt in range(1, (MAX_LOGIN_RETRIES + 1)):
|
||||||
URL,
|
resp = requests.get(
|
||||||
params=sales_prog_req.model_dump(mode="json", exclude_none=True),
|
URL,
|
||||||
headers=session.headers, # type: ignore[argumentType]
|
params=sales_prog_req.model_dump(mode="json", exclude_none=True),
|
||||||
timeout=API_CON_TIMEOUT,
|
headers=session.headers, # type: ignore[argumentType]
|
||||||
)
|
timeout=API_CON_TIMEOUT,
|
||||||
|
)
|
||||||
|
if resp.status_code == 401:
|
||||||
|
_, status = session.relogin()
|
||||||
|
if status != STATUS_HANDLER.SUCCESS and attempt == MAX_LOGIN_RETRIES:
|
||||||
|
return empty_response, status
|
||||||
|
|
||||||
except requests.exceptions.Timeout:
|
except requests.exceptions.Timeout:
|
||||||
return empty_response, STATUS_HANDLER.pipe_states.CONNECTION_TIMEOUT
|
return empty_response, STATUS_HANDLER.pipe_states.CONNECTION_TIMEOUT
|
||||||
except requests.exceptions.RequestException:
|
except requests.exceptions.RequestException:
|
||||||
|
|||||||
@ -51,6 +51,8 @@ class KnownDelBarApiErrorCodes(enum.Enum):
|
|||||||
|
|
||||||
# ** API
|
# ** API
|
||||||
API_CON_TIMEOUT: Final[float] = 10.0 # secs to response
|
API_CON_TIMEOUT: Final[float] = 10.0 # secs to response
|
||||||
|
MAX_LOGIN_RETRIES: Final[int] = 1
|
||||||
|
|
||||||
# ** API response parsing
|
# ** API response parsing
|
||||||
# ** column mapping [API-Response --> Target-Features]
|
# ** column mapping [API-Response --> Target-Features]
|
||||||
COL_MAP_SALES_PROGNOSIS: Final[DualDict[str, str]] = DualDict(
|
COL_MAP_SALES_PROGNOSIS: Final[DualDict[str, str]] = DualDict(
|
||||||
|
|||||||
@ -292,6 +292,15 @@ class Session:
|
|||||||
|
|
||||||
return None, status
|
return None, status
|
||||||
|
|
||||||
|
def relogin(
|
||||||
|
self,
|
||||||
|
) -> tuple[LoginResponse, Status]:
|
||||||
|
if self.session_token is None:
|
||||||
|
return self.login()
|
||||||
|
|
||||||
|
self._remove_session_token()
|
||||||
|
return self.login()
|
||||||
|
|
||||||
def assert_login(
|
def assert_login(
|
||||||
self,
|
self,
|
||||||
) -> tuple[LoginResponse, Status]:
|
) -> tuple[LoginResponse, Status]:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user