Simulates a round-trip call against the given API / URL
(
method,
api_or_module,
url,
body="",
headers=None,
params=None,
query_string="",
scheme="http",
host=DEFAULT_HOST,
**kwargs
)
| 53 | |
| 54 | |
| 55 | def call( |
| 56 | method, |
| 57 | api_or_module, |
| 58 | url, |
| 59 | body="", |
| 60 | headers=None, |
| 61 | params=None, |
| 62 | query_string="", |
| 63 | scheme="http", |
| 64 | host=DEFAULT_HOST, |
| 65 | **kwargs |
| 66 | ): |
| 67 | """Simulates a round-trip call against the given API / URL""" |
| 68 | api = API(api_or_module).http.server() |
| 69 | response = StartResponseMock() |
| 70 | headers = {} if headers is None else headers |
| 71 | if not isinstance(body, str) and "json" in headers.get("content-type", "application/json"): |
| 72 | body = output_format.json(body) |
| 73 | headers.setdefault("content-type", "application/json") |
| 74 | |
| 75 | params = params if params else {} |
| 76 | params.update(kwargs) |
| 77 | if params: |
| 78 | query_string = "{}{}{}".format( |
| 79 | query_string, "&" if query_string else "", urlencode(params, True) |
| 80 | ) |
| 81 | result = api( |
| 82 | create_environ( |
| 83 | path=url, |
| 84 | method=method, |
| 85 | headers=headers, |
| 86 | query_string=query_string, |
| 87 | body=body, |
| 88 | scheme=scheme, |
| 89 | host=host, |
| 90 | ), |
| 91 | response, |
| 92 | ) |
| 93 | if result: |
| 94 | response.data = _internal_result(result) |
| 95 | response.content_type = response.headers_dict["content-type"] |
| 96 | if "application/json" in response.content_type: |
| 97 | response.data = json.loads(response.data) |
| 98 | return response |
| 99 | |
| 100 | |
| 101 | for method in HTTP_METHODS: |