def _http(self, path, method, headers={}, body=None, expected="200"): # see if we need to reauth? if need_to_reauth(**self.auth): if self.debug: print "Refreshing access token" # TODO add error handling self.auth = reauth(**self.auth) self.auth_object.save(self.auth) # add the auth header all_headers = self._headers() all_headers.update(headers) if body: all_headers.update(Wink.content_headers) if type(body) is not str: body = json.dumps(body) if self.debug: print "Request: %s %s" % (method, path) if headers: print "Extra headers:", headers if body: print "Body:", pprint(body) resp, content = self.http.request( self._url(path), method, headers=all_headers, body=body ) if self.debug: print "Response:", resp["status"] # coerce to JSON, if possible if content: try: content = json.loads(content) if "errors" in content and content["errors"]: raise RuntimeError("\n".join(content["errors"])) except: pass if self.debug: pprint(content) if type(expected) is str: expected = set([expected]) if resp["status"] not in expected: raise RuntimeError( "expected code %s, but got %s for %s %s" % ( expected, resp["status"], method, path, ) ) if content: return content return {}
def _http(self, path, method, headers={}, body=None, expected="200"): # see if we need to reauth? if need_to_reauth(**self.auth): if self.debug: print "Refreshing access token" # TODO add error handling self.auth = reauth(**self.auth) self.auth_object.save(self.auth) # add the auth header all_headers = self._headers() all_headers.update(headers) if body: all_headers.update(Wink.content_headers) if type(body) is not str: body = json.dumps(body) if self.debug: print "Request: %s %s" % (method, path) if headers: print "Extra headers:", headers if body: print "Body:", pprint(body) resp, content = self.http.request(self._url(path), method, headers=all_headers, body=body) if self.debug: print "Response:", resp["status"] # coerce to JSON, if possible if content: try: content = json.loads(content) if "errors" in content and content["errors"]: raise RuntimeError("\n".join(content["errors"])) except: pass if self.debug: pprint(content) if type(expected) is str: expected = set([expected]) if resp["status"] not in expected: raise RuntimeError("expected code %s, but got %s for %s %s" % ( expected, resp["status"], method, path, )) if content: return content return {}