|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from enum import Enum |
| 5 | +from typing import Dict, List, Optional |
| 6 | + |
| 7 | +from .properties import Property, property_from_dict |
| 8 | + |
| 9 | + |
| 10 | +class Method(Enum): |
| 11 | + """ HTTP Methods """ |
| 12 | + GET = "get" |
| 13 | + POST = "post" |
| 14 | + PATCH = "patch" |
| 15 | + |
| 16 | + |
| 17 | +class ParameterLocation(Enum): |
| 18 | + """ The places Parameters can be put when calling an Endpoint """ |
| 19 | + QUERY = "query" |
| 20 | + PATH = "path" |
| 21 | + |
| 22 | + |
| 23 | +@dataclass |
| 24 | +class Parameter: |
| 25 | + """ A parameter in an Endpoint """ |
| 26 | + location: ParameterLocation |
| 27 | + property: Property |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + def from_dict(d: Dict, /) -> Parameter: |
| 31 | + """ Construct a parameter from it's OpenAPI dict form """ |
| 32 | + return Parameter( |
| 33 | + location=ParameterLocation(d["in"]), |
| 34 | + property=property_from_dict( |
| 35 | + name=d["name"], |
| 36 | + required=d["required"], |
| 37 | + data=d["schema"], |
| 38 | + ), |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +@dataclass |
| 43 | +class Endpoint: |
| 44 | + """ |
| 45 | + Describes a single endpoint on the server |
| 46 | + """ |
| 47 | + path: str |
| 48 | + method: Method |
| 49 | + description: Optional[str] |
| 50 | + name: str |
| 51 | + parameters: List[Parameter] |
| 52 | + tag: Optional[str] = None |
| 53 | + |
| 54 | + @staticmethod |
| 55 | + def get_list_from_dict(d: Dict[str, Dict[str, Dict]], /) -> List[Endpoint]: |
| 56 | + """ Parse the openapi paths data to get a list of endpoints """ |
| 57 | + endpoints = [] |
| 58 | + for path, path_data in d.items(): |
| 59 | + for method, method_data in path_data.items(): |
| 60 | + parameters: List[Parameter] = [] |
| 61 | + for param_dict in method_data.get("parameters", []): |
| 62 | + parameters.append(Parameter.from_dict(param_dict)) |
| 63 | + endpoint = Endpoint( |
| 64 | + path=path, |
| 65 | + method=Method(method), |
| 66 | + description=method_data.get("description"), |
| 67 | + name=method_data["operationId"], |
| 68 | + parameters=parameters, |
| 69 | + tag=method_data.get("tags", [None])[0], |
| 70 | + ) |
| 71 | + endpoints.append(endpoint) |
| 72 | + return endpoints |
| 73 | + |
| 74 | + |
| 75 | +@dataclass |
| 76 | +class Schema: |
| 77 | + """ |
| 78 | + Describes a schema, AKA data model used in requests. |
| 79 | +
|
| 80 | + These will all be converted to dataclasses in the client |
| 81 | + """ |
| 82 | + |
| 83 | + title: str |
| 84 | + properties: List[Property] |
| 85 | + description: str |
| 86 | + |
| 87 | + @staticmethod |
| 88 | + def from_dict(d: Dict, /) -> Schema: |
| 89 | + """ A single Schema from its dict representation """ |
| 90 | + required = set(d.get("required", [])) |
| 91 | + properties: List[Property] = [] |
| 92 | + for key, value in d["properties"].items(): |
| 93 | + properties.append(property_from_dict(name=key, required=key in required, data=value,)) |
| 94 | + return Schema(title=d["title"], properties=properties, description=d.get("description", ""),) |
| 95 | + |
| 96 | + @staticmethod |
| 97 | + def dict(d: Dict, /) -> Dict[str, Schema]: |
| 98 | + """ Get a list of Schemas from an OpenAPI dict """ |
| 99 | + result = {} |
| 100 | + for data in d.values(): |
| 101 | + s = Schema.from_dict(data) |
| 102 | + result[s.title] = s |
| 103 | + return result |
| 104 | + |
| 105 | + |
| 106 | +@dataclass |
| 107 | +class OpenAPI: |
| 108 | + """ Top level OpenAPI spec """ |
| 109 | + |
| 110 | + title: str |
| 111 | + description: str |
| 112 | + version: str |
| 113 | + security_schemes: Dict |
| 114 | + schemas: Dict[str, Schema] |
| 115 | + endpoints: List[Endpoint] |
| 116 | + |
| 117 | + @staticmethod |
| 118 | + def from_dict(d: Dict, /) -> OpenAPI: |
| 119 | + """ Create an OpenAPI from dict """ |
| 120 | + return OpenAPI( |
| 121 | + title=d["info"]["title"], |
| 122 | + description=d["info"]["description"], |
| 123 | + version=d["info"]["version"], |
| 124 | + endpoints=Endpoint.get_list_from_dict(d["paths"]), |
| 125 | + schemas=Schema.dict(d["components"]["schemas"]), |
| 126 | + security_schemes=d["components"]["securitySchemes"], |
| 127 | + ) |
0 commit comments