11from __future__ import annotations
22
3+ from collections import defaultdict
34from dataclasses import dataclass , field
45from enum import Enum
56from typing import Dict , List , Optional , Set
67
78import stringcase
89
910from .properties import Property , property_from_dict , ListProperty , RefProperty , EnumProperty
10-
11-
12- class Method (Enum ):
13- """ HTTP Methods """
14-
15- GET = "get"
16- POST = "post"
17- PATCH = "patch"
11+ from .responses import Response , response_from_dict
1812
1913
2014class ParameterLocation (Enum ):
@@ -47,31 +41,40 @@ class Endpoint:
4741 """
4842
4943 path : str
50- method : Method
44+ method : str
5145 description : Optional [str ]
5246 name : str
5347 parameters : List [Parameter ]
54- tag : Optional [ str ] = None
48+ responses : List [ Response ]
5549
5650 @staticmethod
57- def get_list_from_dict (d : Dict [str , Dict [str , Dict ]], / ) -> List [Endpoint ]:
51+ def get_by_tags_from_dict (d : Dict [str , Dict [str , Dict ]], / ) -> Dict [ str , List [Endpoint ] ]:
5852 """ Parse the openapi paths data to get a list of endpoints """
59- endpoints = []
53+ # TODO: handle requestBody
54+ endpoints_by_tag : Dict [str , List [Endpoint ]] = defaultdict (list )
6055 for path , path_data in d .items ():
6156 for method , method_data in path_data .items ():
6257 parameters : List [Parameter ] = []
58+ responses : List [Response ] = []
6359 for param_dict in method_data .get ("parameters" , []):
6460 parameters .append (Parameter .from_dict (param_dict ))
61+ tag = method_data .get ("tags" , ["default" ])[0 ]
62+ for code , response_dict in method_data ["responses" ].items ():
63+ response = response_from_dict (
64+ status_code = int (code ),
65+ data = response_dict ,
66+ )
67+ responses .append (response )
6568 endpoint = Endpoint (
6669 path = path ,
67- method = Method ( method ) ,
70+ method = method ,
6871 description = method_data .get ("description" ),
6972 name = method_data ["operationId" ],
7073 parameters = parameters ,
71- tag = method_data . get ( "tags" , [ None ])[ 0 ] ,
74+ responses = responses ,
7275 )
73- endpoints .append (endpoint )
74- return endpoints
76+ endpoints_by_tag [ tag ] .append (endpoint )
77+ return endpoints_by_tag
7578
7679
7780@dataclass
@@ -119,7 +122,7 @@ class OpenAPI:
119122 version : str
120123 security_schemes : Dict
121124 schemas : Dict [str , Schema ]
122- endpoints : List [Endpoint ]
125+ endpoints_by_tag : Dict [ str , List [Endpoint ] ]
123126 enums : Dict [str , EnumProperty ]
124127
125128 @staticmethod
@@ -144,7 +147,7 @@ def from_dict(d: Dict, /) -> OpenAPI:
144147 title = d ["info" ]["title" ],
145148 description = d ["info" ]["description" ],
146149 version = d ["info" ]["version" ],
147- endpoints = Endpoint .get_list_from_dict (d ["paths" ]),
150+ endpoints_by_tag = Endpoint .get_by_tags_from_dict (d ["paths" ]),
148151 schemas = schemas ,
149152 security_schemes = d ["components" ]["securitySchemes" ],
150153 enums = enums ,
0 commit comments