def post(self, resource, parameters={}, data={}, headers={}): """ Perform a POST HTTP requeste on a resource. :arg resource: the resource to POST to :arg parameters: a dictionary of GET parameters for the resource :arg data: a dictionary of POST parameters for the resource :arg headers: optional headers. If specified, these will override the default headers. :returns: A response from the AltaPay service as a :samp:`dict`. :raises: :UnauthorizedAccessError: If the supplied credentials are not valid. :ResponseStatusError: If the response code from AltaPay is not a subset of the allowed response codes. """ return self._request(urljoin(self.url, resource), 'POST', params=utils.http_build_query(parameters), data=utils.http_build_query_dict(data), headers=headers or self._headers())
def post(self, resource, parameters={}, data={}, headers={}): """ Perform a POST HTTP requeste on a resource. :arg resource: the resource to POST to :arg parameters: a dictionary of GET parameters for the resource :arg data: a dictionary of POST parameters for the resource :arg headers: optional headers. If specified, these will override the default headers. :returns: A response from the AltaPay service as a :samp:`dict`. :raises: :UnauthorizedAccessError: If the supplied credentials are not valid. :ResponseStatusError: If the response code from AltaPay is not a subset of the allowed response codes. """ return self._request( urljoin(self.url, resource), 'POST', params=utils.http_build_query(parameters), data=utils.http_build_query_dict(data), headers=headers or self._headers())
def test_http_build_query_complex_dict(self): payload = OrderedDict([('customer', OrderedDict([('name', 'test'), ('address', 'testaddress')]))]) query_string = utils.http_build_query(payload) self.assertEqual( query_string, quote('customer[name]=test&customer[address]=testaddress', safe='/=&'))
def download(self, resource, parameters={}, headers={}): """ Downloads a resource. Acts as a custom HTTP GET. Not that it is considered the callers responsibility to actually flush/close the stream. """ response = requests.get( resource, params=utils.http_build_query(parameters), stream=True, headers=headers or self._headers(), auth=self._auth) return BytesIO(response.content)
def test_http_build_query_list_with_complex_dict(self): payload = OrderedDict([('orderline', [ OrderedDict([('title', 'test'), ('qty', 1)]), OrderedDict([('title', 'test2'), ('qty', 2)]) ])]) query_string = utils.http_build_query(payload) self.assertEqual( query_string, quote(('orderline[0][title]=test&orderline[0][qty]=1&' 'orderline[1][title]=test2&orderline[1][qty]=2'), safe='/=&'))
def test_http_build_query_complex_dict(self): payload = OrderedDict([ ('customer', OrderedDict([ ('name', 'test'), ('address', 'testaddress')] )) ]) query_string = utils.http_build_query(payload) self.assertEqual( query_string, quote( 'customer[name]=test&customer[address]=testaddress', safe='/=&'))
def download(self, resource, parameters={}, headers={}): """ Downloads a resource. Acts as a custom HTTP GET. Not that it is considered the callers responsibility to actually flush/close the stream. """ response = requests.get(resource, params=utils.http_build_query(parameters), stream=True, headers=headers or self._headers(), auth=self._auth) return BytesIO(response.content)
def test_http_build_query_nested_dict(self): payload = OrderedDict([ ('customer', OrderedDict([('address', OrderedDict([('name', 'testname'), ('address', 'testaddr')]))])) ]) query_string = utils.http_build_query(payload) self.assertEqual( query_string, quote(('customer[address][name]=testname&' 'customer[address][address]=testaddr'), safe='/=&'))
def test_http_build_query_nested_dict(self): payload = OrderedDict([ ('customer', OrderedDict([ ('address', OrderedDict([ ('name', 'testname'), ('address', 'testaddr') ])) ])) ]) query_string = utils.http_build_query(payload) self.assertEqual( query_string, quote(( 'customer[address][name]=testname&' 'customer[address][address]=testaddr'), safe='/=&'))
def test_http_build_query_list_with_complex_dict(self): payload = OrderedDict([ ('orderline', [ OrderedDict([ ('title', 'test'), ('qty', 1) ]), OrderedDict([ ('title', 'test2'), ('qty', 2) ]) ]) ]) query_string = utils.http_build_query(payload) self.assertEqual( query_string, quote(( 'orderline[0][title]=test&orderline[0][qty]=1&' 'orderline[1][title]=test2&orderline[1][qty]=2'), safe='/=&'))
def test_http_build_query_simple_list(self): payload = OrderedDict([('a', ['1', '2']), ('b', [3, 4])]) query_string = utils.http_build_query(payload) self.assertEqual(query_string, quote('a[0]=1&a[1]=2&b[0]=3&b[1]=4', safe='/=&'))
def test_http_build_query_simple(self): payload = OrderedDict([('a', '1'), ('b', 2)]) query_string = utils.http_build_query(payload) self.assertEqual(query_string, 'a=1&b=2')
def test_http_build_query_simple_list(self): payload = OrderedDict([('a', ['1', '2']), ('b', [3, 4])]) query_string = utils.http_build_query(payload) self.assertEqual( query_string, quote('a[0]=1&a[1]=2&b[0]=3&b[1]=4', safe='/=&'))