def test_get_examples_from_api(self): api = APIModel() api.id = 11 api.name = 'Example 1' api.url = '{{HOST}}/sample/url/path' api.body = self.request_body api.method = 'POST' self.document_generator.response_id = 10 self.document_generator.api_id_counter = 1 examples = self.document_generator.get_examples(api, []) self.assertEqual(1, len(examples)) api_example = examples[0] self.assertEqual('1', api_example.request_id) self.assertEqual('response_11', api_example.id) self.assertEqual('Example 1', api_example.name) self.assertEqual('POST', api_example.method) self.assertEqual('{{HOST}}/sample/url/path', api_example.url) self.assertEqual( '\nPOST {{HOST}}/sample/url/path\n' + self.request_body.raw, api_example.request_body) self.assertIsNone(api_example.status) self.assertIsNone(api_example.code)
def add_apis(self, item: json): """ Creates an APIModel and adds it to api_info :param item: json node representing an api """ api = APIModel() api.id = self.api_id_counter api.name = item.get(NAME, NOT_FOUND) api.body = None if item.get(REQUEST, {}).get(DESCRIPTION, None) is not None: api.description = item.get(REQUEST, {}).get(DESCRIPTION, None) api.description = self.markdown_to_html(api.description) if item.get(REQUEST, {}).get(BODY, None) is not None: api.body = self.get_body(item.get(REQUEST).get(BODY)) if api.body is not None and api.body.raw is not None: api.body.raw = '\n' + api.body.raw.strip( ) # append a line break for better formatting of jsons api.body.raw = bleach.clean(api.body.raw) api.method = item.get(REQUEST, {}).get(METHOD, None) headers = item.get(REQUEST, {}).get(HEADER, None) if headers is not None: api.headers = DocumentGenerator.get_key_values(headers) if item.get(REQUEST, {}).get(URL, None) is not None: api.url = item.get(REQUEST, {}).get(URL, {}).get(RAW, None) query_params = item.get(REQUEST, {}).get(URL, {}).get(QUERY, None) if query_params is not None: api.params = DocumentGenerator.get_key_values(query_params) path_variables = item.get(REQUEST, {}).get(URL, {}).get(PATH_VARIABLE, None) if path_variables is not None: api.path_variables = DocumentGenerator.get_key_values( path_variables) api.examples = self.get_examples(api, item.get(RESPONSE, [])) self.api_info.append(api)