def handle(response): """Handle a response and return its payload If all is okay, return the json payload of the response as a dict. If the status code is in the range 400 to 599, raise an HTTPError. """ try: response.raise_for_status() except HTTPError as error: log.error(error) raise error return to_json(response)
def _request_authentication(self): """Ask Open-Spending if the token is valid. """ query = dict(jwt=self.token) response = authenticate_user(params=query) authentication = handle(response) if not authentication['authenticated']: message = 'Token has expired: request a new one' log.error(message) raise InvalidToken(message) name = authentication['profile']['name'] log.info('Hello %s! You are logged into Open-Spending', name) return authentication
def _handle_promises(self): """Collect all promises from S3 uploads. """ for stream, future in zip(self._streams, self._futures): exception = future.exception() if exception: raise exception response = future.result() if response.status_code != 200: message = 'Something went wrong uploading %s to S3: %s' log.error(message, response.url, response.text) raise HTTPError(message) self._responses.append(response) stream.close()