def get(self, url, extra_headers, exception_on_401=False): """Make an HTTP GET request using the active Bearer Token.""" headers = dict(Authorization="Bearer %s" % self.token) headers.update(extra_headers) status_code, headers, content = self._do_get(url, headers) if status_code == 401: if exception_on_401: # This is our second try. Give up. raise BadResponseException.from_response( url, "Something's wrong with the Overdrive OAuth Bearer Token!", (status_code, headers, content)) else: # Refresh the token and try again. self.check_creds(True) return self.get(url, extra_headers, True) else: return status_code, headers, content
def test_helper_constructor(self): response = MockRequestsResponse(102, content="nonsense") exc = BadResponseException.from_response( "http://url/", "Terrible response, just terrible", response) # Turn the exception into a problem detail document, and it's full # of useful information. doc, status_code, headers = exc.as_problem_detail_document( debug=True).response doc = json.loads(doc) eq_('Bad response', doc['title']) eq_( 'The server made a request to http://url/, and got an unexpected or invalid response.', doc['detail']) eq_( u'Terrible response, just terrible\n\nStatus code: 102\nContent: nonsense', doc['debug_message']) # Unless debug is turned off, in which case none of that # information is present. doc, status_code, headers = exc.as_problem_detail_document( debug=False).response assert 'debug_message' not in json.loads(doc)