|
| 1 | +import os |
| 2 | +import re |
| 3 | +from typing import List |
| 4 | +from pathlib import Path |
| 5 | +from constants import EXAMPLES_FOLDER |
| 6 | +from dataclasses import dataclass, field |
| 7 | + |
| 8 | +@dataclass |
| 9 | +class EndpointInfo: |
| 10 | + path: str = None |
| 11 | + has_examples_subfolder: bool = False |
| 12 | + num_request_examples: int = 0 |
| 13 | + num_response_examples: int = 0 |
| 14 | + num_examples: int = 0 |
| 15 | + examples_subfolders = [] |
| 16 | + recognized_examples_subfolders: set[str] = field(default_factory=set) |
| 17 | + examples_response_codes: set[str] = field(default_factory=set) |
| 18 | + |
| 19 | +class EndpointInfoGenerator: |
| 20 | + def __init__(self): |
| 21 | + self.spec_path = "." |
| 22 | + |
| 23 | + # Get all the folders in a path |
| 24 | + def get_folders(self, path: str) -> List[Path]: |
| 25 | + return [f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))] |
| 26 | + |
| 27 | + def get_files(self, path: str) -> List[str]: |
| 28 | + return [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))] |
| 29 | + |
| 30 | + def is_example_file(self, file: str) -> bool: |
| 31 | + if file.endswith(".yaml"): |
| 32 | + return True |
| 33 | + print(f"WARNING: Found non-YAML example file: {file}") |
| 34 | + return False |
| 35 | + |
| 36 | + def get_example_files_in_folder(self, path: str) -> List[str]: |
| 37 | + example_files = [] |
| 38 | + for file in self.get_files(path): |
| 39 | + if self.is_example_file(file): |
| 40 | + example_files.append(file) |
| 41 | + return example_files |
| 42 | + |
| 43 | + def get_request_subfolder(self, endpoint_path) -> str: |
| 44 | + examples_path = os.path.join(endpoint_path, EXAMPLES_FOLDER) |
| 45 | + request_examples_path = os.path.join(examples_path, "request") |
| 46 | + if os.path.exists(request_examples_path): |
| 47 | + return "request" |
| 48 | + return None |
| 49 | + |
| 50 | + def get_response_subfolders(self, endpoint_path) -> int: |
| 51 | + examples_path = os.path.join(endpoint_path, EXAMPLES_FOLDER) |
| 52 | + response_examples_folders = [] |
| 53 | + response_examples_path = os.path.join(examples_path, "response") |
| 54 | + if os.path.exists(response_examples_path): |
| 55 | + response_examples_folders.append("response") |
| 56 | + else: |
| 57 | + examples_subfolders = self.get_folders(examples_path) |
| 58 | + for examples_subfolder in examples_subfolders: |
| 59 | + # Look for folders of the pattern "nnn_response" |
| 60 | + if re.match(r"[0-9]{3}_response", examples_subfolder): |
| 61 | + response_examples_folders.append(examples_subfolder) |
| 62 | + return response_examples_folders |
| 63 | + |
| 64 | + def get_response_code_from_response_folder(self, folder: str) -> str: |
| 65 | + if folder == "response": |
| 66 | + return "200" |
| 67 | + match = re.match(r"(\d{3})_response", folder) |
| 68 | + if match: |
| 69 | + return match.group(1) |
| 70 | + raise Exception(f"Invalid response folder: {folder}") |
| 71 | + |
| 72 | + def get_endpoint_info(self, endpoint_path: str) -> EndpointInfo: |
| 73 | + endpoint_path_relative_to_spec = os.path.relpath(endpoint_path, self.spec_path) |
| 74 | + endpoint_info = EndpointInfo(path=endpoint_path_relative_to_spec) |
| 75 | + # If there is no 'examples' folder, return EndpointInfo with |
| 76 | + # default values |
| 77 | + examples_path = os.path.join(endpoint_path, EXAMPLES_FOLDER) |
| 78 | + if not os.path.exists(examples_path): |
| 79 | + return endpoint_info |
| 80 | + endpoint_info.examples_subfolders = self.get_folders(examples_path) |
| 81 | + request_subfolder = self.get_request_subfolder(endpoint_path) |
| 82 | + if request_subfolder: |
| 83 | + endpoint_info.recognized_examples_subfolders.add(request_subfolder) |
| 84 | + examples_request_path = os.path.join(examples_path, request_subfolder) |
| 85 | + endpoint_info.num_request_examples = len(self.get_example_files_in_folder(examples_request_path)) |
| 86 | + response_subfolders = self.get_response_subfolders(endpoint_path) |
| 87 | + if (len(response_subfolders) > 0): |
| 88 | + endpoint_info.recognized_examples_subfolders.update(response_subfolders) |
| 89 | + for response_subfolder in response_subfolders: |
| 90 | + response_code = self.get_response_code_from_response_folder(response_subfolder) |
| 91 | + endpoint_info.examples_response_codes.add(response_code) |
| 92 | + examples_response_path = os.path.join(examples_path, response_subfolder) |
| 93 | + endpoint_info.num_response_examples += len(self.get_example_files_in_folder(examples_response_path)) |
| 94 | + endpoint_info.num_examples = endpoint_info.num_request_examples + endpoint_info.num_response_examples |
| 95 | + return endpoint_info |
0 commit comments