def test_urlencode(): query = { 'empty': '', # empty string 'x': 'y', # normal ASCII 'foo': u'bär', # unicode val u'bärge': 'large', # unicode key u'äah': 'yätes', # unicode key & val # a non-ASCII str, just to make things interesting 'name': 'Unique Caf\xe9', } # the same query, as utf-8 encoded strings utf8_query = dict( (k.encode('utf-8'), v.encode('utf-8') if isinstance(v, unicode) else v) for (k, v) in query.iteritems() ) # Dictionaries can re-order the url params, so we # compare the split up params as sets def assert_params_equal(left, right): assert set(left.split('&')) == set(right.split('&')) assert_params_equal(urllib_utf8.urlencode(query), urllib.urlencode(utf8_query)) # try again, but with arrays of pairs assert_params_equal(urllib_utf8.urlencode(query.items()), urllib.urlencode(utf8_query))
def test_urlencode_lists(): expected = 'exprs=foo%3A123&exprs=b%C3%A4r%3A222%F0%9F%90%B5' query = {'exprs': ['foo:123', u'bär:222🐵'.encode('UTF-8')]} assert urllib.urlencode(query, True) == expected assert urllib_utf8.urlencode(query, True) == expected query = {'exprs': ['foo:123', u'bär:222🐵']} assert urllib_utf8.urlencode(query, True) == expected
def test_urlencode_lists(): expected = 'exprs=foo%3A123&exprs=b%C3%A4r%3A222%F0%9F%90%B5' query = {'exprs': ['foo:123', u'bär:222🐵'.encode('UTF-8')]} assert urllib.parse.urlencode(query, True) == expected assert urllib_utf8.urlencode(query, True) == expected query = {'exprs': ['foo:123', u'bär:222🐵']} assert urllib_utf8.urlencode(query, True) == expected
def request(self, request_params, response_callback=None): """Sets up the request params as per Twisted Agent needs. Sets up crochet and triggers the API request in background :param request_params: request parameters for API call :type request_params: dict :param response_callback: Function to be called after receiving the response :type response_callback: method :rtype: :class: `bravado_core.http_future.HttpFuture` """ url = "%s?%s" % (request_params["url"], urllib_utf8.urlencode(request_params.get("params", []), True)) fetch_kwargs = { "method": str(request_params.get("method", "GET")), "body": stringify_body(request_params), "headers": request_params.get("headers", {}), } for fetch_kwarg in ("connect_timeout", "timeout"): if fetch_kwarg in request_params: fetch_kwargs[fetch_kwarg] = request_params[fetch_kwarg] concurrent_future = fido.fetch(url, **fetch_kwargs) return HttpFuture(concurrent_future, FidoResponseAdapter, response_callback)
def request(self, request_params, response_callback=None): """Sets up the request params as per Twisted Agent needs. Sets up crochet and triggers the API request in background :param request_params: request parameters for API call :type request_params: dict :param response_callback: Function to be called after receiving the response :type response_callback: method :rtype: :class: `bravado_core.http_future.HttpFuture` """ url = '%s?%s' % (request_params['url'], urllib_utf8.urlencode( request_params.get('params', []), True)) fetch_kwargs = { 'method': str(request_params.get('method', 'GET')), 'body': stringify_body(request_params), 'headers': request_params.get('headers', {}), } for fetch_kwarg in ('connect_timeout', 'timeout'): if fetch_kwarg in request_params: fetch_kwargs[fetch_kwarg] = request_params[fetch_kwarg] concurrent_future = fido.fetch(url, **fetch_kwargs) return HttpFuture(concurrent_future, FidoResponseAdapter, response_callback)
def start_request(self, request_params): """Sets up the request params as per Twisted Agent needs. Sets up crochet and triggers the API request in background :param request_params: request parameters for API call :type request_params: dict :return: crochet EventualResult """ # request_params has mandatory: method, url, params, headers request_params = { 'method': str(request_params.get('method', 'GET')), 'bodyProducer': stringify_body(request_params), 'headers': listify_headers(request_params.get('headers', {})), 'uri': '%s?%s' % (request_params['url'], urllib_utf8.urlencode(request_params.get('params', []), True)) } # crochet only supports bytes for the url if isinstance(request_params['uri'], unicode): request_params['uri'] = request_params['uri'].encode('utf-8') crochet.setup() return self.fetch_deferred(request_params)
def request(self, request_params, response_callback=None, also_return_response=False): """Sets up the request params as per Twisted Agent needs. Sets up crochet and triggers the API request in background :param request_params: request parameters for API call :type request_params: dict :param response_callback: Function to be called after receiving the response :type response_callback: method :param also_return_response: Consult the constructor documentation for :class:`bravado.http_future.HttpFuture`. :rtype: :class: `bravado_core.http_future.HttpFuture` """ url = '%s?%s' % (request_params['url'], urllib_utf8.urlencode( request_params.get('params', []), True)) fetch_kwargs = { 'method': str(request_params.get('method', 'GET')), 'body': stringify_body(request_params), 'headers': request_params.get('headers', {}), } for fetch_kwarg in ('connect_timeout', 'timeout'): if fetch_kwarg in request_params: fetch_kwargs[fetch_kwarg] = request_params[fetch_kwarg] concurrent_future = fido.fetch(url, **fetch_kwargs) return HttpFuture(concurrent_future, FidoResponseAdapter, response_callback, also_return_response)
def stringify_body(request_params): """Wraps the data using twisted FileBodyProducer """ headers = request_params.get("headers", {}) if "files" in request_params: return create_multipart_content(request_params, headers) if headers.get("content-type") == APP_FORM: return urllib_utf8.urlencode(request_params.get("data", {})) # TODO: same method 'stringify_body' exists with different args - fix! return param_stringify_body(request_params.get("data", ""))
def stringify_body(request_params): """Wraps the data using twisted FileBodyProducer """ headers = request_params.get('headers', {}) if 'files' in request_params: return create_multipart_content(request_params, headers) if headers.get('content-type') == APP_FORM: return urllib_utf8.urlencode(request_params.get('data', {})) # TODO: same method 'stringify_body' exists with different args - fix! return param_stringify_body(request_params.get('data', ''))
def stringify_body(request_params): """Wraps the data using twisted FileBodyProducer """ headers = request_params.get('headers', {}) if 'files' in request_params: data = create_multipart_content(request_params, headers) elif headers.get('content-type') == http_client.APP_FORM: data = urllib_utf8.urlencode(request_params.get('data', {})) else: data = client.stringify_body(request_params.get('data', '')) return FileBodyProducer(StringIO(data)) if data else None
def __call__(self, **kwargs): log.debug(u"%s?%r" % (self._json[u'nickname'], urllib_utf8.urlencode(kwargs))) request = self._construct_request(**kwargs) def response_future(response, **kwargs): # Assume status is OK, an exception would have been raised already if not response.text: return None return post_receive(response.json(), swagger_type.get_swagger_type(self._json), self._models, **kwargs) return HTTPFuture(self._http_client, request, response_future)
def request(self, request_params, operation=None, response_callbacks=None, also_return_response=False): """Sets up the request params as per Twisted Agent needs. Sets up crochet and triggers the API request in background :param request_params: request parameters for the http request. :type request_params: dict :param operation: operation that this http request is for. Defaults to None - in which case, we're obviously just retrieving a Swagger Spec. :type operation: :class:`bravado_core.operation.Operation` :param response_callbacks: List of callables to post-process the incoming response. Expects args incoming_response and operation. :param also_return_response: Consult the constructor documentation for :class:`bravado.http_future.HttpFuture`. :rtype: :class: `bravado_core.http_future.HttpFuture` """ url = '%s?%s' % (request_params['url'], urllib_utf8.urlencode( request_params.get('params', []), True)) fetch_kwargs = { 'method': str(request_params.get('method', 'GET')), 'body': stringify_body(request_params), 'headers': request_params.get('headers', {}), } for fetch_kwarg in ('connect_timeout', 'timeout'): if fetch_kwarg in request_params: fetch_kwargs[fetch_kwarg] = request_params[fetch_kwarg] concurrent_future = fido.fetch(url, **fetch_kwargs) return HttpFuture(concurrent_future, FidoResponseAdapter, operation, response_callbacks, also_return_response)
def request(self, request_params, response_callback=None): """Sets up the request params as per Twisted Agent needs. Sets up crochet and triggers the API request in background :param request_params: request parameters for API call :type request_params: dict :param response_callback: Function to be called after receiving the response :type response_callback: method :rtype: :class: `bravado_core.http_future.HttpFuture` """ url = '%s?%s' % (request_params['url'], urllib_utf8.urlencode( request_params.get('params', []), True)) request_params = { 'method': str(request_params.get('method', 'GET')), 'body': stringify_body(request_params), 'headers': request_params.get('headers', {}), } return HttpFuture(fido.fetch(url, **request_params), FidoResponseAdapter, response_callback)