def request(self, method, endpoint, params={}, opts={}): url = self.api_url + endpoint # These methods don't have a request body if method in ('GET', 'HEAD', 'DELETE'): payload = None # Make params into GET parameters if len(params) > 0: url = url + "?" + util.uri_encode(params) # Otherwise, encode request body to JSON else: payload = json.dumps(params, separators=(',', ':')) try: resp = requests.request(method, url, headers=self.build_headers(opts), data=payload, auth=HTTPBasicAuth(self.api_key, '')) if (resp.status_code >= 400): self.handle_api_error(resp) except requests.exceptions.RequestException as e: self.handle_network_error(e) return self.parse(resp)
def request(self, method, endpoint, params={}): url = self.api_url + endpoint headers = { 'content-type': "application/json", 'user-agent': "Invoiced Python/" + version.VERSION } # These methods don't have a request body if method in ('GET', 'HEAD', 'DELETE'): payload = None # Make params into GET parameters if len(params) > 0: url = url + "?" + util.uri_encode(params) # Otherwise, encode request body to JSON else: payload = json.dumps(params, separators=(',', ':')) try: resp = requests.request(method, url, headers=headers, data=payload, auth=HTTPBasicAuth(self.api_key, '')) if (resp.status_code >= 400): self.handle_api_error(resp) except requests.exceptions.RequestException as e: self.handle_network_error(e) return self.parse(resp)
def request(self, method, endpoint, params={}): url = self.api_url + endpoint headers = { 'content-type': "application/json", 'user-agent': "Invoiced Python/"+version.VERSION } # These methods don't have a request body if method in ('GET', 'HEAD', 'DELETE'): payload = None # Make params into GET parameters if len(params) > 0: url = url + "?" + util.uri_encode(params) # Otherwise, encode request body to JSON else: payload = json.dumps(params, separators=(',', ':')) try: resp = requests.request(method, url, headers=headers, data=payload, auth=HTTPBasicAuth(self.api_key, '')) if (resp.status_code >= 400): self.rescue_api_error(resp) except requests.exceptions.RequestException as e: self.rescue_requests_error(e) return self.parse(resp)
def test_uri_encode(self): params = { "test": "property", "filter": { "levels": "work", "nesting": { "works": True } }, "array": [ "should", {"also": True}, ["work"] ] } encoded = util.uri_encode(params) self.assertEqual(encoded, "array[]=should&array[][also]=True&array[]=work&filter[levels]=work&filter[nesting][works]=True&test=property") # noqa
def test_uri_encode(self): params = { "test": "property", "filter": { "levels": "work", "nesting": { "works": True } }, "array": ["should", { "also": True }, ["work"]] } encoded = util.uri_encode(params) self.assertEqual( encoded, "array[]=should&array[][also]=True&array[]=work&filter[levels]=work&filter[nesting][works]=True&test=property" ) # noqa