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

KeyError: 'details' when using Swedish OrderCapture API #39

Closed
Erik142 opened this issue Sep 9, 2021 · 3 comments · Fixed by #40
Closed

KeyError: 'details' when using Swedish OrderCapture API #39

Erik142 opened this issue Sep 9, 2021 · 3 comments · Fixed by #40

Comments

@Erik142
Copy link
Contributor

Erik142 commented Sep 9, 2021

Hello,

Thank you for creating this IKEA api client, I find it very useful for a project that I am currently working on. I am having an issue with the "order capture" api in Sweden, which seems to result in the same error as #33. The latest version (0.7.0) does not solve this issue for me. See error below:

Traceback (most recent call last):
  File "d:\Filer\Utveckling\Projekt\Python\IKEA-notifier\src\app.py", line 8, in <module>
  File "d:\Filer\Utveckling\Projekt\Python\IKEA-notifier\src\api\ikea_availability_api.py", line 17, in getAvailability
    availabilities = self.__IKEA_API.OrderCapture(
  File "C:\Users\Erik\AppData\Local\Programs\Python\Python39\lib\site-packages\ikea_api\core.py", line 64, in OrderCapture
    return OrderCapture(self._token, zip_code, state_code)()
  File "C:\Users\Erik\AppData\Local\Programs\Python\Python39\lib\site-packages\ikea_api\endpoints\order_capture\__init__.py", line 32, in __call__
    return self.get_delivery_services()
  File "C:\Users\Erik\AppData\Local\Programs\Python\Python39\lib\site-packages\ikea_api\endpoints\order_capture\__init__.py", line 97, in get_delivery_services
    checkout: str | None = self._get_checkout()
  File "C:\Users\Erik\AppData\Local\Programs\Python\Python39\lib\site-packages\ikea_api\endpoints\order_capture\__init__.py", line 71, in _get_checkout
    response: dict[str, str] = self._call_api(
  File "C:\Users\Erik\AppData\Local\Programs\Python\Python39\lib\site-packages\ikea_api\api.py", line 70, in _call_api
    self._error_handler(response.status_code, response_dict)
  File "C:\Users\Erik\AppData\Local\Programs\Python\Python39\lib\site-packages\ikea_api\endpoints\order_capture\__init__.py", line 36, in _error_handler
    raise OrderCaptureError(response)
    if response["message"] == response["details"]
KeyError: 'details'

The error can be reproduced by running the following code:

api = IkeaApi(country_code='se', language_code='sv')
api.login_as_guest()

api.Cart.clear()
api.Cart.add_items({"20512245": 1})
cart_show = api.Cart.show()    # The show method works as expected, showing the added item in the cart
api.OrderCapture(zip_code="12345")    # Mock zip-code here, but it does not work with a valid zip code either

I have debugged the issue myself and have traced it down to the hard coded "ru" language code in the file "src/ikea_api/endpoints/order_capture/__init__.py" (line 66):

data = {
"shoppingType": "ONLINE",
"channel": "WEBAPP",
"checkoutType": "STANDARD",
"languageCode": "ru",
"items": items,
"deliveryArea": None,
}

By changing this to:

data = {
            "shoppingType": "ONLINE",
            "channel": "WEBAPP",
            "checkoutType": "STANDARD",
            "languageCode": Constants.LANGUAGE_CODE,
            "items": items,
            "deliveryArea": None,
        }

the issue is solved for me. I do not think that this change would create any further issues for other regions and languages, so I will go ahead and create a PR to fix this.

If there is any potential issue with this change, please let me know.

Thanks!

@vrslev
Copy link
Owner

vrslev commented Sep 9, 2021

Thanks for contribution! Just merged the PR.
Tested with se/sv, ru/ru and us/en — everything works fine.
Just curious, can you share for what project you are using this package? 😃

@Erik142
Copy link
Contributor Author

Erik142 commented Sep 9, 2021

Thank you for accepting the change, and for the quick response!

So far it is just the beginning of a private project with the goal of being a website where users can track the online availability for certain IKEA products, and get email notifications when products that are out of stock get restocked! I figured that I can use the CaptureOrder class for this, to see whether or not a product can be delivered to a specific zip code! I'll make sure to post the project on Github eventually. I'll keep you posted! 😄

@vrslev
Copy link
Owner

vrslev commented Sep 9, 2021

Pretty cool! Alright :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants