async def _request(self, method, path, *args, **kwargs): url = self._AUTHORITY + path logging.debug(f'{method}, {url}, {args}, {kwargs}') if 'params' not in kwargs: kwargs['params'] = self._DEFAULT_PARAMS with handle_exception(): return await self._session.request(method, url, *args, **kwargs)
async def get_subscription_products_with_gamekeys( self) -> t.AsyncGenerator[dict, None]: """ Yields list of subscription products (json) - historically backward subscriptions info for Humble Choice proceeded by Humble Monthly. Used by HumbleBundle in `https://www.humblebundle.com/subscription/home` Every product includes only A FEW representative games from given subscription and other data. For Choice: `gamekey` field presence means user has unlocked that month and made choices. For Monhly: `download_url` field presence means user has subscribed this month. """ cursor = '' while True: res = await self._request('GET', self._SUBSCRIPTION_PRODUCTS + f"/{cursor}", raise_for_status=False) if res.status == 404: # Ends with "Humble Monthly" in November 2015 return with handle_exception(): res.raise_for_status() res_json = await res.json() for product in res_json['products']: yield product cursor = res_json['cursor']
async def _request(self, method, path, *args, **kwargs): url = self._AUTHORITY + path if 'params' not in kwargs: kwargs['params'] = self._DEFAULT_PARAMS with handle_exception(): try: return await self._session.request(method, url, *args, **kwargs) except Exception as e: logging.error(repr(e)) raise
async def request(self, method, url, *args, **kwargs): with handle_exception(): try: return await self._session.request(method, url, *args, **kwargs) except aiohttp.ClientResponseError as error: if error.status >= 500: log.warning( "Got status %d while performing %s request for %s", error.status, error.request_info.method, str(error.request_info.url) ) raise error
async def _is_session_valid(self): """Simply asks about order list to know if session is valid. galaxy.api.errors instances cannot be catched so galaxy.http.handle_excpetion is the final check with all the logic under its context. """ with handle_exception(): try: await self._session.request( 'get', self._AUTHORITY + self._ORDER_LIST_URL) except aiohttp.ClientResponseError as e: if e.status == HTTPStatus.UNAUTHORIZED: return False raise return True
async def get_previous_subscription_months(self, from_product: str): """Generator wrapper for get_subscription_history previous months""" while True: res = await self.get_subscription_history(from_product) if res.status == 404: return with handle_exception(): res.raise_for_status() data = await res.json() if not data['previous_months']: return for prev_month in data['previous_months']: yield ChoiceMonth(prev_month) from_product = prev_month['machine_name']
async def _authenticate(self, grant_type, secret): headers = { "Authorization": "basic " + self._BASIC_AUTH_CREDENTIALS, "User-Agent": self.LAUNCHER_USER_AGENT } data = {"grant_type": grant_type, "token_type": "eg1"} data[grant_type] = secret try: with handle_exception(): try: response = await self._session.request("POST", self._OAUTH_URL, headers=headers, data=data) except aiohttp.ClientResponseError as e: logging.error(e) if e.status == 400: # override 400 meaning for auth purpose raise AuthenticationRequired() except AuthenticationRequired as e: logging.exception( f"Authentication failed, grant_type: {grant_type}, exception: {repr(e)}" ) raise AuthenticationRequired() result = await response.json() try: self._access_token = result["access_token"] self._refresh_token = result["refresh_token"] self._account_id = result["account_id"] credentials = {"refresh_token": self._refresh_token} self._store_credentials(credentials) except KeyError: logging.exception( "Could not parse backend response when authenticating") raise UnknownBackendResponse()
async def request(self, method, url, *args, **kwargs): with handle_exception(): return await self._session.request(method, url, *args, **kwargs)
def test_handle_exception(aiohttp_exception, expected_exception_type): with pytest.raises(expected_exception_type): with handle_exception(): raise aiohttp_exception
async def _request(self, method, path, *args, **kwargs): url = self._AUTHORITY + path logging.debug(f'{method}, {url}, {args}, {kwargs}') with handle_exception(): return await self._session.request(method, url, *args, **kwargs)
async def _request(self, *args, **kwargs): with handle_exception(): if 'params' not in kwargs: kwargs['params'] = self._DEFAULT_PARAMS return await self._session.request(*args, **kwargs)