apple / app-store-server-library-python

MIT License
167 stars 31 forks source link

Raise instead of default to Sandbox enviroment #111

Open Gerson4G opened 1 month ago

Gerson4G commented 1 month ago

When init an API Client object the parameter typing indicates it shoud receive an Environment type value but if it receives something else (like string representation of it) it pass through correctly and default the enviroment of the instance to Sandbox (https://github.com/apple/app-store-server-library-python/blob/1202058ddf4eac67c66f3947827e042e9ae6eee1/appstoreserverlibrary/api_client.py#L469); which might confuse users who think they are doing it well (It happened to me). I would expect the param to stick to only allow an Environment type value and raise otherwise.

Something like below (two suggestions I have)

class BaseAppStoreServerAPIClient:
    def __init__(self, signing_key: bytes, key_id: str, issuer_id: str, bundle_id: str, environment: Environment):
        Environment[environment] # Just check if env exists and leave it raise if not
        if environment == Environment.XCODE:
            raise ValueError("Xcode is not a supported environment for an AppStoreServerAPIClient")
        if environment == Environment.PRODUCTION:
            self._base_url = "https://api.storekit.itunes.apple.com"
        elif environment == Environment.LOCAL_TESTING:
            self._base_url = "https://local-testing-base-url"
        elif environment == Environment.SANDBOX: # Or leave the else as it is if using an early return to raise
            self._base_url = "https://api.storekit-sandbox.itunes.apple.com"
        else:
            raise ValueError(f"{environment} is not a supported environment")