Ponytech / appstoreconnectapi

Python wrapper around Apple App Store Api
https://ponytech.net/projects/app-store-connect
MIT License
159 stars 74 forks source link

APIError should expose the HTTP status code returned by App Store Connect API response #26

Closed GClunies closed 3 years ago

GClunies commented 3 years ago

Happy to take a stab at this @ppawlak.

Issue Description

Ideally, when an APIError exception is raised, the APIerror should also expose the HTTP status code as defined in the Apple docs. I think the best way to do this is to add the HTTP status code as an attribute to the APIError object so that the end user can check against this value.

By exposing the HTTP status code to the end user as an attribute, errors can be maintained as contracts. Currently the only way to check for a APIError is using the error description. Something like...

REPORT_NOT_AVAILABLE_ERROR = (
    "Report is not available yet. Daily reports for the Americas are available"
    " by 5 am Pacific Time; Japan, Australia, and New Zealand by 5 am Japan"
    " Standard Time; and 5 am Central European Time for all other territories."
)
NO_DATA_DATE_SPECIFIED_ERROR = "There were no sales for the date specified."

except APIError as e:
    # Only except certain errors, full list of possible errors is found here: 
    # https://developer.apple.com/documentation/appstoreconnectapi/interpreting_and_handling_errors/about_the_http_status_code  # noqa
    if str(e) == REPORT_NOT_AVAILABLE_ERROR or str(e) == NO_DATA_DATE_SPECIFIED_ERROR:
        LOGGER.error("{0}".format(e))
    else:
        raise e

If Apple changes the error description slightly this approach will break. I would expect the HTTP status codes to be much more stable.

ppawlak commented 3 years ago

Thanks for reporting this @GClunies . It is a very valid point.

I'll review your PR as soon as possible.

ppawlak commented 3 years ago

@GClunies I have merged your PR and pushed a small modification : the code is stored as an int (rather than converting it when checking for its value) and I renamed the field status_code (rather than just code)

I'll make a PyPI release soon.

Thanks again for this addition.

GClunies commented 3 years ago

Thanks @ppawlak!

ppawlak commented 3 years ago

Version 0.8.4 with your PR is on PyPI : https://pypi.org/project/appstoreconnect/0.8.4/