From 889ce984a9ea9786bd5ecbf2bf01a08b71dad13e Mon Sep 17 00:00:00 2001 From: Exterminator11 Date: Wed, 25 Oct 2023 16:50:01 +0530 Subject: [PATCH] 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 0000000..4fd1ffa --- /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 b4c712f..908d0ba 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 2634c86..1ce01ef 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(