Skip to content

Commit

Permalink
feat: forgot about pre-commit :<
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Łoboda committed Apr 13, 2024
1 parent 944fa5f commit e6e9975
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
max-line-length = 120
max-complexity = 12
select = B,C,E,F,W,T4,B9
ignore = E501, E731, W503, F401, F403
ignore = E501, E731, W503, F401, F403, E121, E127
exclude = docs
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
repos:
- repo: https://github.com/pycqa/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 23.3.0
rev: 24.4.0
hooks:
- id: black
exclude: ^docs/
language_version: python3
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
rev: 7.0.0
hooks:
- id: flake8
exclude: ^docs/
Expand All @@ -19,7 +19,7 @@ repos:
- flake8-comprehensions
- flake8-pytest
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.3.0
rev: v1.9.0
hooks:
- id: mypy
exclude: ^docs/
Expand All @@ -29,7 +29,7 @@ repos:
- id: darglint
exclude: ^docs/
- repo: https://github.com/commitizen-tools/commitizen
rev: v3.3.0
rev: v3.22.0
hooks:
- id: commitizen
stages: [commit-msg]
155 changes: 76 additions & 79 deletions inpost/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,37 @@
class Inpost:
"""Python representation of an Inpost app. Essentially implements methods to manage all incoming parcels"""

def __init__(self,
prefix: str,
phone_number: str,
sms_code=None,
auth_token=None,
refr_token=None,
):
def __init__(
self,
prefix: str,
phone_number: str,
sms_code=None,
auth_token=None,
refr_token=None,
):
"""Constructor method
:param prefix: country code
:type prefix: str
:param phone_number: phone number
:type phone_number: str
:param sms_code: sms code from inpost
:type sms_code: str | None
:param auth_token: authorization token from inpost
:type auth_token: str
:param refr_token: refresh token from inpost
:type refr_token: str
:raises PhoneNumberError: Wrong phone number format or is not digit
"""

if isinstance(phone_number, int):
phone_number = str(phone_number)

if (not (len(phone_number) == 9 and phone_number.isdigit())
or not (prefix.startswith('+') and prefix[1:].isdigit() and 2 <= len(prefix) <= 3)):
raise PhoneNumberError(f"Wrong phone number format: {phone_number} "
f"(should be +xxx123123123 or +xx123123123)")
if not (len(phone_number) == 9 and phone_number.isdigit()) or not (
prefix.startswith("+") and prefix[1:].isdigit() and 2 <= len(prefix) <= 3
):
raise PhoneNumberError(
f"Wrong phone number format: {phone_number} " f"(should be +xxx123123123 or +xx123123123)"
)
self.prefix: str = prefix
self.phone_number: str = phone_number[-9:]
self.sms_code: str | None = sms_code
Expand All @@ -102,12 +112,7 @@ def combined_phone_number(self) -> str:

@property
def login_auth_data(self) -> dict:
return {
"phoneNumber": {
"prefix": self.prefix,
"value": self.phone_number
}
}
return {"phoneNumber": {"prefix": self.prefix, "value": self.phone_number}}

def __repr__(self):
return f"{self.__class__.__name__}(phone_number={self.phone_number})"
Expand All @@ -119,16 +124,16 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
return self.disconnect()

async def request(
self,
method: str,
action: str,
url: StrOrURL,
auth: bool = True,
headers: dict | None = None,
params: dict | None = None,
data: dict | None = None,
autorefresh: bool = True,
**kwargs,
self,
method: str,
action: str,
url: StrOrURL,
auth: bool = True,
headers: dict | None = None,
params: dict | None = None,
data: dict | None = None,
autorefresh: bool = True,
**kwargs,
) -> ClientResponse:
"""Validates sent data and fetches required compartment properties for opening
Expand Down Expand Up @@ -349,7 +354,7 @@ async def disconnect(self) -> bool:
return False

async def get_parcel(
self, shipment_number: int | str, parcel_type: ParcelType = ParcelType.TRACKED, parse=False
self, shipment_number: int | str, parcel_type: ParcelType = ParcelType.TRACKED, parse=False
) -> dict | Parcel | SentParcel | ReturnParcel:
"""Fetches single parcel from provided shipment number
Expand Down Expand Up @@ -414,12 +419,12 @@ async def get_parcel(
raise UnidentifiedAPIError(reason=resp)

async def get_parcels(
self,
parcel_type: ParcelType = ParcelType.TRACKED,
status: ParcelStatus | List[ParcelStatus] | None = None,
pickup_point: str | List[str] | None = None,
shipment_type: ParcelShipmentType | List[ParcelShipmentType] | None = None,
parse: bool = False,
self,
parcel_type: ParcelType = ParcelType.TRACKED,
status: ParcelStatus | List[ParcelStatus] | None = None,
pickup_point: str | List[str] | None = None,
shipment_type: ParcelShipmentType | List[ParcelShipmentType] | None = None,
parse: bool = False,
) -> List[dict] | List[Parcel]:
"""Fetches all available parcels for set `Inpost.phone_number` and optionally filters them
Expand Down Expand Up @@ -463,7 +468,7 @@ async def get_parcels(
raise ParcelTypeError(reason=f"Unknown parcel type: {parcel_type}")

async with await self.request(
method="get", action="get parcels", url=url, auth=True, headers=None, data=None, autorefresh=True
method="get", action="get parcels", url=url, auth=True, headers=None, data=None, autorefresh=True
) as resp:
if resp.status != 200:
self._log.debug(f"Could not get parcels due to HTTP error {resp.status}")
Expand Down Expand Up @@ -534,8 +539,7 @@ async def get_multi_compartment(self, multi_uuid: str | int, parse: bool = False
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
self, shipment_number: str | int | None = None, parcel_obj: Parcel | None = None, location: dict | None = None
) -> Parcel:
"""Validates sent data and fetches required compartment properties for opening
Expand Down Expand Up @@ -575,22 +579,22 @@ async def collect_compartment_properties(
raise NoParcelError(reason="Could not obtain desired parcel!")

self._log.info(f"collecting compartment properties for {parcel_obj_.shipment_number}")

resp = await self.request(
method="post",
action="collect compartment properties",
url=collect_url,
auth=True,
headers=None,
data={
"parcel": parcel_obj_.compartment_open_data | {
"receiverPhoneNumber": {
"prefix": self.prefix,
"value": self.phone_number
},
},
"geoPoint": location if location is not None else parcel_obj_.mocked_location,
},
# fmt: off
data={"parcel": parcel_obj_.compartment_open_data | {"receiverPhoneNumber":
{
"prefix": self.prefix,
"value": self.phone_number
}
},
"geoPoint": location if location is not None else parcel_obj_.mocked_location,
},
# fmt: on
autorefresh=True,
)

Expand Down Expand Up @@ -638,7 +642,7 @@ async def open_compartment(self, parcel_obj: Parcel) -> Parcel:
raise UnidentifiedAPIError(reason=resp)

async def check_compartment_status(
self, parcel_obj: Parcel, expected_status: CompartmentExpectedStatus = CompartmentExpectedStatus.OPENED
self, parcel_obj: Parcel, expected_status: CompartmentExpectedStatus = CompartmentExpectedStatus.OPENED
) -> bool:
"""Checks and compare compartment status (e.g. opened, closed) with expected status
Expand Down Expand Up @@ -716,7 +720,7 @@ async def terminate_collect_session(self, parcel_obj: Parcel) -> bool:
raise UnidentifiedAPIError(reason=resp)

async def collect(
self, shipment_number: str | None = None, parcel_obj: Parcel | None = None, location: dict | None = None
self, shipment_number: str | None = None, parcel_obj: Parcel | None = None, location: dict | None = None
) -> Parcel | None:
"""Simplified method to open compartment
Expand Down Expand Up @@ -815,13 +819,13 @@ async def reopen_compartment(self, parcel_obj: Parcel) -> bool:
raise UnidentifiedAPIError(reason=resp)

async def get_parcel_points(
self,
query: str | None = None,
latitude: float | None = None,
longitude: float | None = None,
per_page: int = 1000,
operation: ParcelPointOperations = ParcelPointOperations.CREATE,
parse: bool = True,
self,
query: str | None = None,
latitude: float | None = None,
longitude: float | None = None,
per_page: int = 1000,
operation: ParcelPointOperations = ParcelPointOperations.CREATE,
parse: bool = True,
) -> dict | List[Point]:
"""Fetches parcel points for inpost services
Expand Down Expand Up @@ -915,13 +919,13 @@ async def blik_status(self) -> bool:
return False

async def create_parcel(
self,
delivery_type: DeliveryType,
parcel_size: ParcelLockerSize | ParcelCarrierSize,
price: float | str,
sender: Sender,
receiver: Receiver,
delivery_point: Point,
self,
delivery_type: DeliveryType,
parcel_size: ParcelLockerSize | ParcelCarrierSize,
price: float | str,
sender: Sender,
receiver: Receiver,
delivery_point: Point,
) -> dict | None:
"""Fetches parcel points for inpost services
Expand Down Expand Up @@ -972,7 +976,7 @@ async def create_parcel(
raise UnidentifiedAPIError(reason=resp)

async def create_blik_session(
self, amount: float | str, shipment_number: str, currency: str = "PLN"
self, amount: float | str, shipment_number: str, currency: str = "PLN"
) -> None | dict:
"""Creates blik session for sending parcel
Expand Down Expand Up @@ -1017,11 +1021,11 @@ async def create_blik_session(
raise UnidentifiedAPIError(reason=resp)

async def validate_send(
self,
drop_off_point: str,
shipment_number: str | None = None,
parcel_obj: SentParcel | None = None,
location: dict | None = None,
self,
drop_off_point: str,
shipment_number: str | None = None,
parcel_obj: SentParcel | None = None,
location: dict | None = None,
) -> SentParcel:
"""Validates sending parcel
Expand Down Expand Up @@ -1159,7 +1163,7 @@ async def reopen_send_compartment(self, parcel_obj: SentParcel) -> bool:
return False

async def check_send_compartment_status(
self, parcel_obj: SentParcel, expected_status: CompartmentExpectedStatus = CompartmentExpectedStatus.OPENED
self, parcel_obj: SentParcel, expected_status: CompartmentExpectedStatus = CompartmentExpectedStatus.OPENED
) -> bool:
"""Checks and compare compartment status (e.g. opened, closed) with expected status
Expand Down Expand Up @@ -1528,14 +1532,7 @@ async def share_parcel(self, uuid: str, shipment_number: int | str) -> bool:
auth=True,
headers=None,
data={
"parcels": [
{
"shipmentNumber": shipment_number,
"friendUuids": [
uuid
]
}
],
"parcels": [{"shipmentNumber": shipment_number, "friendUuids": [uuid]}],
},
autorefresh=True,
)
Expand Down
6 changes: 3 additions & 3 deletions inpost/static/parcels.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def compartment_status(self, status) -> None:
self._log.warning(f"wrong ParcelShipmentType: {repr(self.shipment_type)}")

@property
def compartment_open_data(self) -> dict | None:
def compartment_open_data(self) -> dict:
"""Returns a compartment open data for :class:`Parcel`
:return: dict containing compartment open data for :class:`Parcel`
Expand All @@ -275,7 +275,7 @@ def compartment_open_data(self) -> dict | None:

self._log.debug("getting compartment open data")
if self.receiver is None:
return None
return {}

if self.shipment_type == ParcelShipmentType.parcel:
self._log.debug("got compartment open data")
Expand All @@ -285,7 +285,7 @@ def compartment_open_data(self) -> dict | None:
}

self._log.warning(f"wrong ParcelShipmentType: {repr(self.shipment_type)}")
return None
return {}

@property
def mocked_location(self) -> dict | None:
Expand Down

0 comments on commit e6e9975

Please sign in to comment.