def call(method, api_or_module, url, body='', headers=None, params=None, query_string='', scheme='http', **kwargs): """Simulates a round-trip call against the given API / URL""" api = API(api_or_module).http.server() response = StartResponseMock() headers = {} if headers is None else headers if not isinstance(body, str) and 'json' in headers.get('content-type', 'application/json'): body = output_format.json(body) headers.setdefault('content-type', 'application/json') params = params if params else {} params.update(kwargs) if params: query_string = '{}{}{}'.format(query_string, '&' if query_string else '', urlencode(params, True)) result = api(create_environ(path=url, method=method, headers=headers, query_string=query_string, body=body, scheme=scheme), response) if result: try: response.data = result[0].decode('utf8') except TypeError: response.data = [] for chunk in result: response.data.append(chunk.decode('utf8')) response.data = "".join(response.data) except UnicodeDecodeError: response.data = result[0] response.content_type = response.headers_dict['content-type'] if response.content_type == 'application/json': response.data = json.loads(response.data) return response
def call(method, api_module, url, body='', headers=None, **params): '''Simulates a round-trip call against the given api_module / url''' api = server(api_module) response = StartResponseMock() if not isinstance(body, str): body = output_format.json(body) headers = {} if headers is None else headers headers['content-type'] = 'application/json' result = api( create_environ(path=url, method=method, headers=headers, query_string=urlencode(params), body=body), response) if result: try: response.data = result[0].decode('utf8') except TypeError: response.data = [] for chunk in result: response.data.append(chunk.decode('utf8')) response.data = "".join(response.data) except UnicodeDecodeError: response.data = result[0] response.content_type = response.headers_dict['content-type'] if response.content_type == 'application/json': response.data = json.loads(response.data) return response
def call(method, api_or_module, url, body='', headers=None, params=None, query_string='', scheme='http', **kwargs): """Simulates a round-trip call against the given API / URL""" api = API(api_or_module).http.server() response = StartResponseMock() headers = {} if headers is None else headers if not isinstance(body, str) and 'json' in headers.get('content-type', 'application/json'): body = output_format.json(body) headers.setdefault('content-type', 'application/json') params = params if params else {} params.update(kwargs) if params: query_string = '{}{}{}'.format(query_string, '&' if query_string else '', urlencode(params, True)) result = api(create_environ(path=url, method=method, headers=headers, query_string=query_string, body=body, scheme=scheme), response) if result: try: response.data = result[0].decode('utf8') except TypeError: data = BytesIO() for chunk in result: data.write(chunk) data = data.getvalue() try: response.data = data.decode('utf8') except UnicodeDecodeError: # pragma: no cover response.data = data except (UnicodeDecodeError, AttributeError): response.data = result[0] response.content_type = response.headers_dict['content-type'] if 'application/json' in response.content_type: response.data = json.loads(response.data) return response
def call(method, api_module, url, body="", headers=None, **params): """Simulates a round-trip call against the given api_module / url""" api = server(api_module) response = StartResponseMock() if not isinstance(body, str): body = output_format.json(body) headers = {} if headers is None else headers headers["content-type"] = "application/json" result = api( create_environ(path=url, method=method, headers=headers, query_string=urlencode(params), body=body), response ) if result: try: response.data = result[0].decode("utf8") except TypeError: response.data = [] for chunk in result: response.data.append(chunk.decode("utf8")) response.data = "".join(response.data) response.content_type = response.headers_dict["content-type"] if response.content_type == "application/json": response.data = json.loads(response.data) return response
def call(method, api_or_module, url, body='', headers=None, **params): """Simulates a round-trip call against the given API / URL""" api = API(api_or_module).http.server() response = StartResponseMock() if not isinstance(body, str): body = output_format.json(body) headers = {} if headers is None else headers headers.setdefault('content-type', 'application/json') result = api(create_environ(path=url, method=method, headers=headers, query_string=urlencode(params), body=body), response) if result: try: response.data = result[0].decode('utf8') except TypeError: response.data = [] for chunk in result: response.data.append(chunk.decode('utf8')) response.data = "".join(response.data) except UnicodeDecodeError: response.data = result[0] response.content_type = response.headers_dict['content-type'] if response.content_type == 'application/json': response.data = json.loads(response.data) return response
def call(method, api_module, url, body='', headers=None, **params): '''Simulates a round-trip call against the given api_module / url''' api = server(api_module) response = StartResponseMock() if not isinstance(body, str): body = output_format.json(body) headers = {} if headers is None else headers headers['content-type'] = 'application/json' result = api(create_environ(path=url, method=method, headers=headers, query_string=urlencode(params), body=body), response) if result: response.data = result[0].decode('utf8') response.content_type = response.headers_dict['content-type'] if response.content_type == 'application/json': response.data = json.loads(response.data) return response
def call(method, api_or_module, url, body="", headers=None, params=None, query_string="", scheme="http", host=DEFAULT_HOST, **kwargs): """Simulates a round-trip call against the given API / URL""" api = API(api_or_module).http.server() response = StartResponseMock() headers = {} if headers is None else headers if not isinstance(body, str) and "json" in headers.get( "content-type", "application/json"): body = output_format.json(body) headers.setdefault("content-type", "application/json") params = params if params else {} params.update(kwargs) if params: query_string = "{}{}{}".format(query_string, "&" if query_string else "", urlencode(params, True)) result = api( create_environ( path=url, method=method, headers=headers, query_string=query_string, body=body, scheme=scheme, host=host, ), response, ) if result: response.data = _internal_result(result) response.content_type = response.headers_dict["content-type"] if "application/json" in response.content_type: response.data = json.loads(response.data) return response