Skip to content

Commit

Permalink
feat: Add state_code field to OrderCapture (merge PR #34) (closes #33)
Browse files Browse the repository at this point in the history
  • Loading branch information
vrslev committed Sep 6, 2021
2 parents 687cbb5 + a993ffe commit 1b9de6e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/ikea_api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ def Cart(self):
self._cart = Cart(self._token)
return self._cart

def OrderCapture(self, zip_code: str):
def OrderCapture(self, zip_code: str, state_code: str | None = None):
"""Get available delivery services. Object is callable."""
from ikea_api.endpoints import OrderCapture

return OrderCapture(self._token, zip_code)()
return OrderCapture(self._token, zip_code, state_code)()

@property
def Purchases(self):
Expand Down
19 changes: 15 additions & 4 deletions src/ikea_api/endpoints/order_capture/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class OrderCapture(API):
def __init__(self, token: str, zip_code: str | int):
def __init__(self, token: str, zip_code: str | int, state_code: str | None = None):
super().__init__(token, "https://ordercapture.ikea.ru/ordercaptureapi/ru")

if Constants.COUNTRY_CODE != "ru":
Expand All @@ -23,6 +23,9 @@ def __init__(self, token: str, zip_code: str | int):
validate_zip_code(zip_code)
self._zip_code = zip_code

validate_state_code(state_code)
self._state_code = state_code

self._session.headers["X-Client-Id"] = Secrets.purchases_x_client_id

def __call__(self) -> list[dict[str, Any]]:
Expand Down Expand Up @@ -76,10 +79,12 @@ def _get_checkout(self):
raise IkeaApiError("No resourceId for checkout")

def _get_delivery_area(self, checkout: str | None):
"""Generate delivery area for checkout from zip code"""
"""Generate delivery area for checkout from Zip Code and State Code"""
data = {"enableRangeOfDays": False, "zipCode": self._zip_code}
if self._state_code is not None:
data["stateCode"] = self._state_code
response = self._call_api(
endpoint=f"{self._endpoint}/checkouts/{checkout}/delivery-areas",
data={"zipCode": self._zip_code, "enableRangeOfDays": False},
f"{self._endpoint}/checkouts/{checkout}/delivery-areas", data=data
)

if "resourceId" in response:
Expand All @@ -102,3 +107,9 @@ def get_delivery_services(self):
def validate_zip_code(zip_code: str | int):
if len(re.findall(r"[^0-9]", str(zip_code))) > 0:
raise ValueError(f"Invalid zip code: {zip_code}")


def validate_state_code(state_code: str | None):
if state_code is not None:
if len(state_code) != 2 or len(re.findall(r"[^A-z]+", state_code)) > 0:
raise ValueError(f"Invalid state code: {state_code}")

0 comments on commit 1b9de6e

Please sign in to comment.