Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
feat: Make getting guest token inherit from API class
Browse files Browse the repository at this point in the history
  • Loading branch information
vrslev committed Aug 2, 2021
1 parent 6284553 commit 29b6108
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
5 changes: 3 additions & 2 deletions src/ikea_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Method(Enum):
class API:
"""Generic API class"""

def __init__(self, token: str, endpoint: str):
def __init__(self, token: Union[str, None], endpoint: str):
self._token, self._endpoint = token, endpoint

self._session = Session()
Expand All @@ -26,11 +26,12 @@ def __init__(self, token: str, endpoint: str):
"Accept-Language": Constants.LANGUAGE_CODE,
"Connection": "keep-alive",
"User-Agent": Constants.USER_AGENT,
"Authorization": "Bearer " + token,
"Origin": Constants.BASE_URL,
"Referer": Constants.BASE_URL + "/",
}
)
if token is not None:
self._session.headers["Authorization"] = "Bearer " + token

def _error_handler(self, status_code: int, response_json: Any):
pass
Expand Down
48 changes: 23 additions & 25 deletions src/ikea_api/auth.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, Dict, Optional

from .api import API
from .constants import Constants, Secrets
from .errors import IkeaApiError, UnauthorizedError

_driver_packages_installed = True
try:
Expand All @@ -23,35 +23,33 @@
# pyright: reportOptionalMemberAccess=false


def get_guest_token() -> str: # TODO: Make endpoint out of it
from requests import post
def get_guest_token():
return GuestAuth()()

response = post(
url="https://api.ingka.ikea.com/guest/token",
headers={
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-us",
"Origin": Constants.BASE_URL,
"Referer": Constants.BASE_URL + "/",
"Connection": "keep-alive",
"User-Agent": Constants.USER_AGENT,
"X-Client-Id": Secrets.auth_guest_token_x_client_id,
"X-Client-Secret": Secrets.auth_guest_token_x_client_secret,
},
json={"retailUnit": Constants.LANGUAGE_CODE},
)

if response.status_code == 401:
raise UnauthorizedError(response.json())
elif not response.ok:
raise IkeaApiError(response.status_code, response.text)
def get_authorized_token(username: str, password: str):
return Auth()(username, password)

return response.json()["access_token"]

class GuestAuth(API):
def __init__(self):
super().__init__(None, "https://api.ingka.ikea.com/guest/token")
self._session.headers.update(
{
"Accept": "*/*",
"Accept-Language": "en-us",
"X-Client-Id": Secrets.auth_guest_token_x_client_id,
"X-Client-Secret": Secrets.auth_guest_token_x_client_secret,
}
)

def get_token(self):
response = self._call_api(data={"retailUnit": Constants.LANGUAGE_CODE})
self._token = response["access_token"]
return self._token

def get_authorized_token(username: str, password: str):
return Auth()(username, password)
def __call__(self) -> str:
return self.get_token()


class Auth:
Expand Down
2 changes: 1 addition & 1 deletion src/ikea_api/endpoints/order_capture/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _error_handler(self, status_code: int, response_json: Dict[Any, Any]):
raise OrderCaptureError(response_json)

def _get_items_for_checkout_request(self):
cart = Cart(self._token)
cart = Cart(self._token) # type: ignore
cart_show = cart.show()
items_templated: List[Dict[str, Any]] = []
try:
Expand Down

0 comments on commit 29b6108

Please sign in to comment.