diff --git a/epicstore_api/api.py b/epicstore_api/api.py index badba9b..d7386f5 100644 --- a/epicstore_api/api.py +++ b/epicstore_api/api.py @@ -51,6 +51,21 @@ class OfferData(NamedTuple): __all__ = ['EpicGamesStoreAPI', 'OfferData'] +def _clean_1004_errors(raw): + # On some responses EGS API returns 1004 errors for no reason, however the responses being sent are valid otherwise. + # Official launcher ignores those errors, so we probably should do that as well. That function cleans up the mess + # from raw response so error handling is still possible. + if 'errors' in raw: + for error in raw['errors'].copy(): + service_response = json.loads(error.get('serviceResponse', {})) + if service_response: + if service_response.get('numericErrorCode') == 1004: + raw['errors'].remove(error) + if not raw['errors']: + raw.pop('errors') + return raw + + class EpicGamesStoreAPI: """ Class for interacting with EGS web API without user credentials TODO? @@ -94,7 +109,7 @@ def get_free_games(self, allow_countries: str = None) -> dict: 'freeGamesPromotions?locale={}&country={}&allowCountries={}' ) api_uri = api_uri.format(self.locale, self.country, allow_countries) - data = self._session.get(api_uri).json() + data = _clean_1004_errors(self._session.get(api_uri).json()) self._get_errors(data) return data @@ -142,22 +157,14 @@ def get_collection(self, collection: EGSCollectionType) -> dict: :param collection: Needed collection type. """ - raw = self._make_graphql_query( + # Cleanup for the 1004 errors that always pop up by default to not mess someone up by this. + raw = _clean_1004_errors(self._make_graphql_query( COLLECTION_QUERY, slug=collection.value, # This query always returns 1004 error by default. That is not controlled by us and the error itself # is happening even in the official EGS client itself, they're just ignoring it, so we will too. suppress_errors=True - ) - # Cleanup for the 1004 errors that always pop up by default to not mess someone up by this. - if 'errors' in raw: - for error in raw['errors'].copy(): - service_response = json.loads(error.get('serviceResponse', {})) - if service_response: - if service_response.get('numericErrorCode') == 1004: - raw['errors'].remove(error) - if not raw['errors']: - raw.pop('errors') + )) return raw def fetch_media(self, media_ref_id: str) -> dict: diff --git a/setup.py b/setup.py index 0f2c91c..596f1cd 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ import setuptools AUTHOR = 'SD4RK' -VERSION = '0.1.6' +VERSION = '0.1.7' with open("README.md", "r") as fh: long_description = fh.read()