Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 4 additions & 135 deletions inpost/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self):
self._log: logging.Logger | None = None

def __repr__(self):
return f'Phone number: {self.phone_number}\nToken: {self.auth_token}'
return f'{self.__class__.__name__}(phone_number={self.phone_number})'

@classmethod
async def from_phone_number(cls, phone_number: str | int):
Expand Down Expand Up @@ -87,13 +87,6 @@ async def send_sms_code(self) -> bool:

raise UnidentifiedAPIError(reason=phone)

# if phone.status == 200:
# self._log.debug(f'sms code sent')
# return True
# else:
# self._log.error(f'could not sent sms code')
# raise PhoneNumberError(reason=phone)

async def confirm_sms_code(self, sms_code: str | int) -> bool:
"""Confirms sms code sent to `Inpost.phone_number` and fetches tokens

Expand Down Expand Up @@ -143,17 +136,6 @@ async def confirm_sms_code(self, sms_code: str | int) -> bool:

raise UnidentifiedAPIError(reason=confirmation)

# if confirmation.status == 200:
# resp = await confirmation.json()
# self.sms_code = sms_code
# self.refr_token = resp['refreshToken']
# self.auth_token = resp['authToken']
# self._log.debug(f'sms code confirmed')
# return True
# else:
# self._log.error(f'could not confirm sms code')
# raise SmsCodeConfirmationError(reason=confirmation)

async def refresh_token(self) -> bool:
"""Refreshes authorization token using refresh token

Expand Down Expand Up @@ -197,19 +179,6 @@ async def refresh_token(self) -> bool:

raise UnidentifiedAPIError(reason=confirmation)

# if confirmation.status == 200:
# resp = await confirmation.json()
# if resp['reauthenticationRequired']:
# self._log.error(f'could not refresh token, log in again')
# raise ReAuthenticationError(reason='You need to log in again!')
# self.auth_token = resp['authToken']
# self._log.debug(f'token refreshed')
# return True
#
# else:
# self._log.error(f'could not refresh token')
# raise RefreshTokenException(reason=confirmation)

async def logout(self) -> bool:
"""Logouts user from inpost api service

Expand Down Expand Up @@ -246,17 +215,6 @@ async def logout(self) -> bool:

raise UnidentifiedAPIError(reason=resp)

# if resp.status == 200:
# self.phone_number = None
# self.refr_token = None
# self.auth_token = None
# self.sms_code = None
# self._log.debug('logged out')
# return True
# else:
# self._log.error('could not log out')
# raise UnidentifiedAPIError(reason=resp)

async def disconnect(self) -> bool:
"""Simplified method to logout and close user's session

Expand Down Expand Up @@ -311,13 +269,6 @@ async def get_parcel(self, shipment_number: int | str, parse=False) -> dict | Pa
self._log.error(f'could not get parcel with shipment number {shipment_number}, unhandled status')

raise UnidentifiedAPIError(reason=resp)
# if resp.status == 200:
# self._log.debug(f'parcel with shipment number {shipment_number} received')
# return await resp.json() if not parse else Parcel(await resp.json(), logger=self._log)
#
# else:
# self._log.error(f'could not get parcel with shipment number {shipment_number}')
# raise UnidentifiedAPIError(reason=resp)

async def get_parcels(self,
parcel_type: ParcelType = ParcelType.TRACKED,
Expand Down Expand Up @@ -423,48 +374,6 @@ async def get_parcels(self,

raise UnidentifiedAPIError(reason=resp)

# if resp.status == 200:
# self._log.debug(f'received {parcel_type} parcels')
# _parcels = (await resp.json())['parcels']
#
# if status is not None:
# if isinstance(status, ParcelStatus):
# status = [status]
#
# _parcels = (_parcel for _parcel in _parcels if ParcelStatus[_parcel['status']] in status)
#
# if pickup_point is not None:
# if isinstance(pickup_point, str):
# pickup_point = [pickup_point]
#
# _parcels = (_parcel for _parcel in _parcels if _parcel['pickUpPoint']['name'] in pickup_point)
#
# if shipment_type is not None:
# if isinstance(shipment_type, ParcelShipmentType):
# shipment_type = [shipment_type]
#
# _parcels = (_parcel for _parcel in _parcels if
# ParcelShipmentType[_parcel['shipmentType']] in shipment_type)
#
# if parcel_size is not None:
# if isinstance(parcel_size, ParcelCarrierSize):
# parcel_size = [parcel_size]
#
# _parcels = (_parcel for _parcel in _parcels if
# ParcelCarrierSize[_parcel['parcelSize']] in parcel_size)
#
# if isinstance(parcel_size, ParcelLockerSize):
# parcel_size = [parcel_size]
#
# _parcels = (_parcel for _parcel in _parcels if
# ParcelLockerSize[_parcel['parcelSize']] in parcel_size)
#
# return _parcels if not parse else [Parcel(parcel_data=data, logger=self._log) for data in _parcels]
#
# else:
# self._log.error(f'could not get parcels')
# raise UnidentifiedAPIError(reason=resp)

async def collect_compartment_properties(self, shipment_number: str | int | None = None,
parcel_obj: Parcel | None = None, location: dict | None = None) -> bool:
"""Validates sent data and fetches required compartment properties for opening
Expand Down Expand Up @@ -522,16 +431,6 @@ async def collect_compartment_properties(self, shipment_number: str | int | None

raise UnidentifiedAPIError(reason=collect_resp)

# if collect_resp.status == 200:
# self._log.debug(f'collected compartment properties for {shipment_number}')
# parcel_obj.compartment_properties = await collect_resp.json()
# self.parcel = parcel_obj
# return True
#
# else:
# self._log.error(f'could not collect compartment properties for {shipment_number}')
# raise UnidentifiedAPIError(reason=collect_resp)

async def open_compartment(self) -> bool:
"""Opens compartment for `Inpost.parcel` object

Expand Down Expand Up @@ -568,15 +467,6 @@ async def open_compartment(self) -> bool:

raise UnidentifiedAPIError(reason=compartment_open_resp)

# if compartment_open_resp.status == 200:
# self._log.debug(f'opened comaprtment for {self.parcel.shipment_number}')
# self.parcel.compartment_properties.location = await compartment_open_resp.json()
# return True
#
# else:
# self._log.error(f'could not open compartment for {self.parcel.shipment_number}')
# raise UnidentifiedAPIError(reason=compartment_open_resp)

async def check_compartment_status(self,
expected_status: CompartmentExpectedStatus = CompartmentExpectedStatus.OPENED) -> bool:
"""Checks and compare compartment status (e.g. opened, closed) with expected status
Expand Down Expand Up @@ -608,6 +498,7 @@ async def check_compartment_status(self,
match compartment_status_resp.status:
case 200:
self._log.debug(f'checked compartment status for {self.parcel.shipment_number}')
self.parcel.compartment_status = (await compartment_status_resp.json())['status']
return CompartmentExpectedStatus[
(await compartment_status_resp.json())['status']] == expected_status
case 401:
Expand All @@ -623,13 +514,6 @@ async def check_compartment_status(self,

raise UnidentifiedAPIError(reason=compartment_status_resp)

# if compartment_status_resp.status == 200:
# self._log.debug(f'checked compartment status for {self.parcel.shipment_number}')
# return CompartmentExpectedStatus[(await compartment_status_resp.json())['status']] == expected_status
# else:
# self._log.error(f'could not check compartment status for {self.parcel.shipment_number}')
# raise UnidentifiedAPIError(reason=compartment_status_resp)

async def terminate_collect_session(self) -> bool:
"""Terminates user session in inpost api service

Expand Down Expand Up @@ -667,13 +551,6 @@ async def terminate_collect_session(self) -> bool:

raise UnidentifiedAPIError(reason=terminate_resp)

# if terminate_resp.status == 200:
# self._log.debug(f'terminated collect session for {self.parcel.shipment_number}')
# return True
# else:
# self._log.error(f'could not terminate collect session for {self.parcel.shipment_number}')
# raise UnidentifiedAPIError(reason=terminate_resp)

async def collect(self, shipment_number: str | None = None, parcel_obj: Parcel | None = None,
location: dict | None = None) -> bool:
"""Simplified method to open compartment
Expand All @@ -694,8 +571,6 @@ async def collect(self, shipment_number: str | None = None, parcel_obj: Parcel |

.. warning:: you must fill in only one parameter - shipment_number or parcel_obj!"""

self._log.info(f'collecing parcel with shipment number {self.parcel.shipment_number}')

if shipment_number and parcel_obj:
self._log.error(f'shipment_number and parcel_obj filled in')
raise SingleParamError(reason='Fields shipment_number and parcel_obj filled! Choose one!')
Expand All @@ -707,6 +582,8 @@ async def collect(self, shipment_number: str | None = None, parcel_obj: Parcel |
if shipment_number is not None and parcel_obj is None:
parcel_obj = await self.get_parcel(shipment_number=shipment_number, parse=True)

self._log.info(f'collecing parcel with shipment number {parcel_obj.shipment_number}')

if await self.collect_compartment_properties(parcel_obj=parcel_obj, location=location):
if await self.open_compartment():
if await self.check_compartment_status():
Expand Down Expand Up @@ -758,11 +635,3 @@ async def get_prices(self) -> dict:
self._log.error('could not get parcel prices, unhandled status')

raise UnidentifiedAPIError(reason=resp)

# if resp.status == 200:
# self._log.debug(f'got parcel prices')
# return await resp.json()
#
# else:
# self._log.error('could not get parcel prices')
# raise UnidentifiedAPIError(reason=resp)
Loading