From ddd938fd646242be3489dd866df56909a10b90a0 Mon Sep 17 00:00:00 2001 From: Exterminator11 Date: Wed, 11 Oct 2023 07:36:37 +0530 Subject: [PATCH 1/9] Parser for OpenAPI3(Swagger) --- scripts/parser/file/openapi3_parser.py | 46 ++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 scripts/parser/file/openapi3_parser.py diff --git a/scripts/parser/file/openapi3_parser.py b/scripts/parser/file/openapi3_parser.py new file mode 100644 index 00000000..72feab71 --- /dev/null +++ b/scripts/parser/file/openapi3_parser.py @@ -0,0 +1,46 @@ +from urllib.parse import urlparse + +from openapi_parser import parse + +from base_parser import BaseParser + + +class OpenAPI3Parser(BaseParser): + def init_parser(self) -> None: + return super().init_parser() + + def get_base_urls(self, urls): + base_urls = [] + for i in urls: + parsed_url = urlparse(i) + base_url = parsed_url.scheme + "://" + parsed_url.netloc + if base_url not in base_urls: + base_urls.append(base_url) + return base_urls + + def get_info_from_paths(self, path): + info = "" + for operation in path.operations: + info += ( + f"\n{operation.method.value}=" + f"{operation.responses[0].description}" + ) + return info + + def parse_file(self, file_path): + data = parse(file_path) + results = "" + base_urls = self.get_base_urls(link.url for link in data.servers) + base_urls = ",".join([base_url for base_url in base_urls]) + results += f"Base URL:{base_urls}\n" + i = 1 + for path in data.paths: + info = self.get_info_from_paths(path) + results += ( + f"Path{i}: {path.url}\n" + f"description: {path.description}\n" + f"parameters: {path.parameters}\nmethods: {info}\n" + ) + i += 1 + with open("results.txt", "w") as f: + f.write(results) From bd70e00f084d12b539e25adcded4741d6408c702 Mon Sep 17 00:00:00 2001 From: Exterminator11 Date: Sun, 15 Oct 2023 17:00:54 +0530 Subject: [PATCH 2/9] Added tests and updated openapi3_parser.py --- scripts/parser/file/openapi3_parser.py | 20 +++-- tests/test_openapi3.yaml | 116 +++++++++++++++++++++++++ tests/test_openapi3parser.py | 29 +++++++ 3 files changed, 159 insertions(+), 6 deletions(-) create mode 100644 tests/test_openapi3.yaml create mode 100644 tests/test_openapi3parser.py diff --git a/scripts/parser/file/openapi3_parser.py b/scripts/parser/file/openapi3_parser.py index 72feab71..afea1e48 100644 --- a/scripts/parser/file/openapi3_parser.py +++ b/scripts/parser/file/openapi3_parser.py @@ -2,7 +2,10 @@ from urllib.parse import urlparse from openapi_parser import parse -from base_parser import BaseParser +try: + from scripts.parser.file.base_parser import BaseParser +except: + from base_parser import BaseParser class OpenAPI3Parser(BaseParser): @@ -20,11 +23,12 @@ class OpenAPI3Parser(BaseParser): def get_info_from_paths(self, path): info = "" - for operation in path.operations: - info += ( - f"\n{operation.method.value}=" - f"{operation.responses[0].description}" - ) + if(path.operations): + for operation in path.operations: + info += ( + f"\n{operation.method.value}=" + f"{operation.responses[0].description}" + ) return info def parse_file(self, file_path): @@ -42,5 +46,9 @@ class OpenAPI3Parser(BaseParser): f"parameters: {path.parameters}\nmethods: {info}\n" ) i += 1 + if(i==2): + with open("reff.txt", "w") as f: + f.write(str(path)) with open("results.txt", "w") as f: f.write(results) + return results \ No newline at end of file diff --git a/tests/test_openapi3.yaml b/tests/test_openapi3.yaml new file mode 100644 index 00000000..a015e14b --- /dev/null +++ b/tests/test_openapi3.yaml @@ -0,0 +1,116 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: Swagger Petstore + license: + name: MIT +servers: + - url: http://petstore.swagger.io/v1 + - url: https://api.example.com/v1/resource + - url: https://api.example.com/v1/another/resource + - url: https://api.example.com/v1/some/endpoint +paths: + /pets: + get: + summary: List all pets + operationId: listPets + tags: + - pets + parameters: + - name: limit + in: query + description: How many items to return at one time (max 100) + required: false + schema: + type: integer + maximum: 100 + format: int32 + responses: + '200': + description: A paged array of pets + headers: + x-next: + description: A link to the next page of responses + schema: + type: string + content: + application/json: + schema: + $ref: "#/components/schemas/Pets" + default: + description: unexpected error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + post: + summary: Create a pet + operationId: createPets + tags: + - pets + responses: + '201': + description: Null response + default: + description: unexpected error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + /pets/{petId}: + get: + summary: Info for a specific pet + operationId: showPetById + tags: + - pets + parameters: + - name: petId + in: path + required: true + description: The id of the pet to retrieve + schema: + type: string + responses: + '200': + description: Expected response to a valid request + content: + application/json: + schema: + $ref: "#/components/schemas/Pet" + default: + description: unexpected error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" +components: + schemas: + Pet: + type: object + required: + - id + - name + properties: + id: + type: integer + format: int64 + name: + type: string + tag: + type: string + Pets: + type: array + maxItems: 100 + items: + $ref: "#/components/schemas/Pet" + Error: + type: object + required: + - code + - message + properties: + code: + type: integer + format: int32 + message: + type: string \ No newline at end of file diff --git a/tests/test_openapi3parser.py b/tests/test_openapi3parser.py new file mode 100644 index 00000000..2374329d --- /dev/null +++ b/tests/test_openapi3parser.py @@ -0,0 +1,29 @@ +import pytest + +from scripts.parser.file.openapi3_parser import OpenAPI3Parser +from openapi_parser import parse + + +@pytest.mark.parametrize("urls, expected_base_urls", [ + (["http://petstore.swagger.io/v1", "https://api.example.com/v1/resource",'https://api.example.com/v1/another/resource','https://api.example.com/v1/some/endpoint'], ["http://petstore.swagger.io",'https://api.example.com']), +]) +def test_get_base_urls(urls, expected_base_urls): + assert OpenAPI3Parser().get_base_urls(urls) == expected_base_urls + +def test_get_info_from_paths(): + file_path = "tests/test_openapi3.yaml" + data = parse(file_path) + path=data.paths[1] + assert OpenAPI3Parser().get_info_from_paths(path)=="\nget=Expected response to a valid request" + + +def test_parse_file(): + file_path = "tests/test_openapi3.yaml" + results_real = "Base URL:http://petstore.swagger.io,https://api.example.com\nPath1: /pets\ndescription: None\nparameters: []\nmethods: \nget=A paged array of pets\npost=Null response\nPath2: /pets/{petId}\ndescription: None\nparameters: []\nmethods: \nget=Expected response to a valid request\n" + openapi_parser_test = OpenAPI3Parser() + results=openapi_parser_test.parse_file(file_path) + assert results == results_real + + +if __name__ == "__main__": + pytest.main() From 36b243e9d2a29d1d9438bd3b39285fadb50f16a8 Mon Sep 17 00:00:00 2001 From: Exterminator11 Date: Sun, 15 Oct 2023 17:16:12 +0530 Subject: [PATCH 3/9] Formatted all the changed files --- reff.txt | 1 + results.txt | 12 ++++++++ scripts/parser/file/openapi3_parser.py | 8 +++--- tests/test_openapi3parser.py | 39 ++++++++++++++++++++------ 4 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 reff.txt create mode 100644 results.txt diff --git a/reff.txt b/reff.txt new file mode 100644 index 00000000..c9e6362b --- /dev/null +++ b/reff.txt @@ -0,0 +1 @@ +Path(url='/pets', summary=None, description=None, operations=[Operation(method=, responses=[Response(is_default=False, description='A paged array of pets', code=200, content=[Content(type=, schema=Array(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_items=100, min_items=None, unique_items=None, items=Object(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_properties=None, min_properties=None, required=['id', 'name'], properties=[Property(name='id', schema=Integer(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, multiple_of=None, maximum=None, exclusive_maximum=None, minimum=None, exclusive_minimum=None, format=)), Property(name='name', schema=String(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_length=None, min_length=None, pattern=None, format=None)), Property(name='tag', schema=String(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_length=None, min_length=None, pattern=None, format=None))])))], headers=[Header(name='x-next', schema=String(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_length=None, min_length=None, pattern=None, format=None), description='A link to the next page of responses', required=False, deprecated=False)]), Response(is_default=True, description='unexpected error', code=None, content=[Content(type=, schema=Object(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_properties=None, min_properties=None, required=['code', 'message'], properties=[Property(name='code', schema=Integer(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, multiple_of=None, maximum=None, exclusive_maximum=None, minimum=None, exclusive_minimum=None, format=)), Property(name='message', schema=String(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_length=None, min_length=None, pattern=None, format=None))]))], headers=[])], summary='List all pets', description=None, operation_id='listPets', external_docs=None, request_body=None, deprecated=False, parameters=[Parameter(name='limit', location=, schema=Integer(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, multiple_of=None, maximum=100, exclusive_maximum=None, minimum=None, exclusive_minimum=None, format=), required=False, description='How many items to return at one time (max 100)', deprecated=False, style=, explode=True)], tags=['pets'], security=[]), Operation(method=, responses=[Response(is_default=False, description='Null response', code=201, content=None, headers=[]), Response(is_default=True, description='unexpected error', code=None, content=[Content(type=, schema=Object(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_properties=None, min_properties=None, required=['code', 'message'], properties=[Property(name='code', schema=Integer(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, multiple_of=None, maximum=None, exclusive_maximum=None, minimum=None, exclusive_minimum=None, format=)), Property(name='message', schema=String(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_length=None, min_length=None, pattern=None, format=None))]))], headers=[])], summary='Create a pet', description=None, operation_id='createPets', external_docs=None, request_body=None, deprecated=False, parameters=[], tags=['pets'], security=[])], parameters=[]) \ No newline at end of file diff --git a/results.txt b/results.txt new file mode 100644 index 00000000..beff8056 --- /dev/null +++ b/results.txt @@ -0,0 +1,12 @@ +Base URL:http://petstore.swagger.io,https://api.example.com +Path1: /pets +description: None +parameters: [] +methods: +get=A paged array of pets +post=Null response +Path2: /pets/{petId} +description: None +parameters: [] +methods: +get=Expected response to a valid request diff --git a/scripts/parser/file/openapi3_parser.py b/scripts/parser/file/openapi3_parser.py index afea1e48..9b3be5cd 100644 --- a/scripts/parser/file/openapi3_parser.py +++ b/scripts/parser/file/openapi3_parser.py @@ -4,7 +4,7 @@ from openapi_parser import parse try: from scripts.parser.file.base_parser import BaseParser -except: +except ModuleNotFoundError: from base_parser import BaseParser @@ -23,7 +23,7 @@ class OpenAPI3Parser(BaseParser): def get_info_from_paths(self, path): info = "" - if(path.operations): + if path.operations: for operation in path.operations: info += ( f"\n{operation.method.value}=" @@ -46,9 +46,9 @@ class OpenAPI3Parser(BaseParser): f"parameters: {path.parameters}\nmethods: {info}\n" ) i += 1 - if(i==2): + if i == 2: with open("reff.txt", "w") as f: f.write(str(path)) with open("results.txt", "w") as f: f.write(results) - return results \ No newline at end of file + return results diff --git a/tests/test_openapi3parser.py b/tests/test_openapi3parser.py index 2374329d..86517e64 100644 --- a/tests/test_openapi3parser.py +++ b/tests/test_openapi3parser.py @@ -1,27 +1,48 @@ import pytest - -from scripts.parser.file.openapi3_parser import OpenAPI3Parser from openapi_parser import parse +from scripts.parser.file.openapi3_parser import OpenAPI3Parser -@pytest.mark.parametrize("urls, expected_base_urls", [ - (["http://petstore.swagger.io/v1", "https://api.example.com/v1/resource",'https://api.example.com/v1/another/resource','https://api.example.com/v1/some/endpoint'], ["http://petstore.swagger.io",'https://api.example.com']), -]) +@pytest.mark.parametrize( + "urls, expected_base_urls", + [ + ( + [ + "http://petstore.swagger.io/v1", + "https://api.example.com/v1/resource", + "https://api.example.com/v1/another/resource", + "https://api.example.com/v1/some/endpoint", + ], + ["http://petstore.swagger.io", "https://api.example.com"], + ), + ], +) def test_get_base_urls(urls, expected_base_urls): assert OpenAPI3Parser().get_base_urls(urls) == expected_base_urls + def test_get_info_from_paths(): file_path = "tests/test_openapi3.yaml" data = parse(file_path) - path=data.paths[1] - assert OpenAPI3Parser().get_info_from_paths(path)=="\nget=Expected response to a valid request" + path = data.paths[1] + assert ( + OpenAPI3Parser().get_info_from_paths(path) + == "\nget=Expected response to a valid request" + ) def test_parse_file(): file_path = "tests/test_openapi3.yaml" - results_real = "Base URL:http://petstore.swagger.io,https://api.example.com\nPath1: /pets\ndescription: None\nparameters: []\nmethods: \nget=A paged array of pets\npost=Null response\nPath2: /pets/{petId}\ndescription: None\nparameters: []\nmethods: \nget=Expected response to a valid request\n" + results_real = ( + "Base URL:http://petstore.swagger.io,https://api.example.com\nPath1: " + + "/pets\ndescription: None\nparameters: []\nmethods: \n" + + "get=A paged array of pets\npost=Null " + + "response\nPath2: /pets/{petId}\ndescription: None\n" + + "parameters: []\nmethods: " + + "\nget=Expected response to a valid request\n" + ) openapi_parser_test = OpenAPI3Parser() - results=openapi_parser_test.parse_file(file_path) + results = openapi_parser_test.parse_file(file_path) assert results == results_real From 7159e4fbe29b67a55e62c9f2dec503b138a5e7a6 Mon Sep 17 00:00:00 2001 From: Exterminator11 Date: Sun, 15 Oct 2023 17:16:58 +0530 Subject: [PATCH 4/9] Formatted files --- reff.txt | 1 - results.txt | 12 ------------ 2 files changed, 13 deletions(-) delete mode 100644 reff.txt delete mode 100644 results.txt diff --git a/reff.txt b/reff.txt deleted file mode 100644 index c9e6362b..00000000 --- a/reff.txt +++ /dev/null @@ -1 +0,0 @@ -Path(url='/pets', summary=None, description=None, operations=[Operation(method=, responses=[Response(is_default=False, description='A paged array of pets', code=200, content=[Content(type=, schema=Array(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_items=100, min_items=None, unique_items=None, items=Object(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_properties=None, min_properties=None, required=['id', 'name'], properties=[Property(name='id', schema=Integer(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, multiple_of=None, maximum=None, exclusive_maximum=None, minimum=None, exclusive_minimum=None, format=)), Property(name='name', schema=String(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_length=None, min_length=None, pattern=None, format=None)), Property(name='tag', schema=String(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_length=None, min_length=None, pattern=None, format=None))])))], headers=[Header(name='x-next', schema=String(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_length=None, min_length=None, pattern=None, format=None), description='A link to the next page of responses', required=False, deprecated=False)]), Response(is_default=True, description='unexpected error', code=None, content=[Content(type=, schema=Object(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_properties=None, min_properties=None, required=['code', 'message'], properties=[Property(name='code', schema=Integer(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, multiple_of=None, maximum=None, exclusive_maximum=None, minimum=None, exclusive_minimum=None, format=)), Property(name='message', schema=String(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_length=None, min_length=None, pattern=None, format=None))]))], headers=[])], summary='List all pets', description=None, operation_id='listPets', external_docs=None, request_body=None, deprecated=False, parameters=[Parameter(name='limit', location=, schema=Integer(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, multiple_of=None, maximum=100, exclusive_maximum=None, minimum=None, exclusive_minimum=None, format=), required=False, description='How many items to return at one time (max 100)', deprecated=False, style=, explode=True)], tags=['pets'], security=[]), Operation(method=, responses=[Response(is_default=False, description='Null response', code=201, content=None, headers=[]), Response(is_default=True, description='unexpected error', code=None, content=[Content(type=, schema=Object(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_properties=None, min_properties=None, required=['code', 'message'], properties=[Property(name='code', schema=Integer(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, multiple_of=None, maximum=None, exclusive_maximum=None, minimum=None, exclusive_minimum=None, format=)), Property(name='message', schema=String(type=, title=None, enum=[], example=None, description=None, default=None, nullable=False, read_only=False, write_only=False, deprecated=False, extensions={}, max_length=None, min_length=None, pattern=None, format=None))]))], headers=[])], summary='Create a pet', description=None, operation_id='createPets', external_docs=None, request_body=None, deprecated=False, parameters=[], tags=['pets'], security=[])], parameters=[]) \ No newline at end of file diff --git a/results.txt b/results.txt deleted file mode 100644 index beff8056..00000000 --- a/results.txt +++ /dev/null @@ -1,12 +0,0 @@ -Base URL:http://petstore.swagger.io,https://api.example.com -Path1: /pets -description: None -parameters: [] -methods: -get=A paged array of pets -post=Null response -Path2: /pets/{petId} -description: None -parameters: [] -methods: -get=Expected response to a valid request From a7f5303eaf910e3b7375a4abfbe7a2fc2949fcf7 Mon Sep 17 00:00:00 2001 From: Exterminator11 Date: Sun, 15 Oct 2023 17:20:50 +0530 Subject: [PATCH 5/9] Cleaned up the code --- scripts/parser/file/openapi3_parser.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/scripts/parser/file/openapi3_parser.py b/scripts/parser/file/openapi3_parser.py index 9b3be5cd..4fd1ffaf 100644 --- a/scripts/parser/file/openapi3_parser.py +++ b/scripts/parser/file/openapi3_parser.py @@ -46,9 +46,6 @@ class OpenAPI3Parser(BaseParser): f"parameters: {path.parameters}\nmethods: {info}\n" ) i += 1 - if i == 2: - with open("reff.txt", "w") as f: - f.write(str(path)) with open("results.txt", "w") as f: f.write(results) return results From f328b39f57338def892b84ae0c9e27d87661aad8 Mon Sep 17 00:00:00 2001 From: Exterminator11 Date: Mon, 16 Oct 2023 21:05:47 +0530 Subject: [PATCH 6/9] Fixed import error --- scripts/requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/requirements.txt b/scripts/requirements.txt index 7d48373f..badafff0 100644 --- a/scripts/requirements.txt +++ b/scripts/requirements.txt @@ -61,6 +61,7 @@ nltk==3.8.1 numcodecs==0.11.0 numpy==1.25.2 openai==0.27.8 +openapi3-parser==1.1.14 openpyxl==3.1.2 packaging==23.1 pandas==2.0.3 From 8ebff1a9083125b047d1a32fc8f5e298376f15ba Mon Sep 17 00:00:00 2001 From: Exterminator11 Date: Tue, 24 Oct 2023 07:43:57 +0530 Subject: [PATCH 7/9] Updated test_openapi3parser.py --- tests/test_openapi3parser.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_openapi3parser.py b/tests/test_openapi3parser.py index 86517e64..2634c868 100644 --- a/tests/test_openapi3parser.py +++ b/tests/test_openapi3parser.py @@ -33,7 +33,7 @@ def test_get_info_from_paths(): def test_parse_file(): file_path = "tests/test_openapi3.yaml" - results_real = ( + results_expected = ( "Base URL:http://petstore.swagger.io,https://api.example.com\nPath1: " + "/pets\ndescription: None\nparameters: []\nmethods: \n" + "get=A paged array of pets\npost=Null " @@ -43,7 +43,7 @@ def test_parse_file(): ) openapi_parser_test = OpenAPI3Parser() results = openapi_parser_test.parse_file(file_path) - assert results == results_real + assert results == results_expected if __name__ == "__main__": From 889ce984a9ea9786bd5ecbf2bf01a08b71dad13e Mon Sep 17 00:00:00 2001 From: Exterminator11 Date: Wed, 25 Oct 2023 16:50:01 +0530 Subject: [PATCH 8/9] Made changes --- application/parser/file/openapi3_parser.py | 51 ++++++++++++++++++++++ application/requirements.txt | 1 + tests/test_openapi3parser.py | 2 +- 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 application/parser/file/openapi3_parser.py diff --git a/application/parser/file/openapi3_parser.py b/application/parser/file/openapi3_parser.py new file mode 100644 index 00000000..4fd1ffaf --- /dev/null +++ b/application/parser/file/openapi3_parser.py @@ -0,0 +1,51 @@ +from urllib.parse import urlparse + +from openapi_parser import parse + +try: + from scripts.parser.file.base_parser import BaseParser +except ModuleNotFoundError: + from base_parser import BaseParser + + +class OpenAPI3Parser(BaseParser): + def init_parser(self) -> None: + return super().init_parser() + + def get_base_urls(self, urls): + base_urls = [] + for i in urls: + parsed_url = urlparse(i) + base_url = parsed_url.scheme + "://" + parsed_url.netloc + if base_url not in base_urls: + base_urls.append(base_url) + return base_urls + + def get_info_from_paths(self, path): + info = "" + if path.operations: + for operation in path.operations: + info += ( + f"\n{operation.method.value}=" + f"{operation.responses[0].description}" + ) + return info + + def parse_file(self, file_path): + data = parse(file_path) + results = "" + base_urls = self.get_base_urls(link.url for link in data.servers) + base_urls = ",".join([base_url for base_url in base_urls]) + results += f"Base URL:{base_urls}\n" + i = 1 + for path in data.paths: + info = self.get_info_from_paths(path) + results += ( + f"Path{i}: {path.url}\n" + f"description: {path.description}\n" + f"parameters: {path.parameters}\nmethods: {info}\n" + ) + i += 1 + with open("results.txt", "w") as f: + f.write(results) + return results diff --git a/application/requirements.txt b/application/requirements.txt index b4c712f4..908d0ba2 100644 --- a/application/requirements.txt +++ b/application/requirements.txt @@ -57,6 +57,7 @@ nltk==3.8.1 numcodecs==0.11.0 numpy==1.24.2 openai==0.27.8 +openapi3-parser==1.1.14 packaging==23.0 pathos==0.3.0 Pillow==10.0.1 diff --git a/tests/test_openapi3parser.py b/tests/test_openapi3parser.py index 2634c868..1ce01ef5 100644 --- a/tests/test_openapi3parser.py +++ b/tests/test_openapi3parser.py @@ -1,6 +1,6 @@ import pytest from openapi_parser import parse -from scripts.parser.file.openapi3_parser import OpenAPI3Parser +from application.parser.file.openapi3_parser import OpenAPI3Parser @pytest.mark.parametrize( From f3540aac0f2e5028ad694207e158e592a68626f7 Mon Sep 17 00:00:00 2001 From: Exterminator11 Date: Wed, 25 Oct 2023 17:07:47 +0530 Subject: [PATCH 9/9] Changed import --- application/parser/file/openapi3_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/parser/file/openapi3_parser.py b/application/parser/file/openapi3_parser.py index 4fd1ffaf..3c5082fa 100644 --- a/application/parser/file/openapi3_parser.py +++ b/application/parser/file/openapi3_parser.py @@ -3,7 +3,7 @@ from urllib.parse import urlparse from openapi_parser import parse try: - from scripts.parser.file.base_parser import BaseParser + from application.parser.file.base_parser import BaseParser except ModuleNotFoundError: from base_parser import BaseParser