@@ -89,10 +89,13 @@ def get_swag_json(
8989
9090
9191def  get_url_basepath (sw_dict : dict ) ->  str :
92-     try :
92+     if   "basePath"   in   sw_dict :
9393        return  sw_dict ["basePath" ]
94-     except  KeyError :  # no base path defined 
95-         return  "" 
94+     if  "servers"  in  sw_dict :
95+         # When the API path doesn't match the OAS path 
96+         return  sw_dict ["servers" ][0 ]["url" ]
97+ 
98+     return  "" 
9699
97100
98101def  check_sw_version (sw_dict : dict ) ->  None :
@@ -111,74 +114,80 @@ def check_sw_version(sw_dict: dict) -> None:
111114
112115def  get_endpoints (sw_dict : dict ) ->  dict :  # noqa: C901 
113116    """ 
114-     Get all the URLs accepting the "GET" method , together with their description and the tags 
117+     Get all the URLs, together with their description and the tags 
115118    """ 
116119    url_details  =  {}
117120
118121    check_sw_version (sw_dict )
119122
120123    for  p_k , p_o  in  sw_dict ["paths" ].items ():
121-         # will track only the "get" methods, which are the ones that give us data 
122-         if  "get"  in  p_o .keys ():
123-             if  "200"  in  p_o ["get" ]["responses" ].keys ():
124-                 base_res  =  p_o ["get" ]["responses" ]["200" ]
125-             elif  200  in  p_o ["get" ]["responses" ].keys ():
126-                 # if you read a plain yml file the 200 will be an integer 
127-                 base_res  =  p_o ["get" ]["responses" ][200 ]
128-             else :
129-                 # the endpoint does not have a 200 response 
130-                 continue 
131- 
132-             if  "description"  in  p_o ["get" ].keys ():
133-                 desc  =  p_o ["get" ]["description" ]
134-             elif  "summary"  in  p_o ["get" ].keys ():
135-                 desc  =  p_o ["get" ]["summary" ]
136-             else :  # still testing 
137-                 desc  =  "" 
138- 
139-             try :
140-                 tags  =  p_o ["get" ]["tags" ]
141-             except  KeyError :
142-                 tags  =  []
143- 
144-             url_details [p_k ] =  {"description" : desc , "tags" : tags }
145- 
146-             # trying if dataset is defined in swagger... 
147-             if  "content"  in  base_res .keys ():
148-                 res_cont  =  base_res ["content" ]
149-                 if  "application/json"  in  res_cont .keys ():
150-                     ex_field  =  None 
151-                     if  "example"  in  res_cont ["application/json" ]:
152-                         ex_field  =  "example" 
153-                     elif  "examples"  in  res_cont ["application/json" ]:
154-                         ex_field  =  "examples" 
155- 
156-                     if  ex_field :
157-                         if  isinstance (res_cont ["application/json" ][ex_field ], dict ):
158-                             url_details [p_k ]["data" ] =  res_cont ["application/json" ][
159-                                 ex_field 
160-                             ]
161-                         elif  isinstance (res_cont ["application/json" ][ex_field ], list ):
162-                             # taking the first example 
163-                             url_details [p_k ]["data" ] =  res_cont ["application/json" ][
164-                                 ex_field 
165-                             ][0 ]
166-                     else :
167-                         logger .warning (
168-                             f"Field in swagger file does not give consistent data --- { p_k }  
169-                         )
170-                 elif  "text/csv"  in  res_cont .keys ():
171-                     url_details [p_k ]["data" ] =  res_cont ["text/csv" ]["schema" ]
172-             elif  "examples"  in  base_res .keys ():
173-                 url_details [p_k ]["data" ] =  base_res ["examples" ]["application/json" ]
174- 
175-             # checking whether there are defined parameters to execute the call... 
176-             if  "parameters"  in  p_o ["get" ].keys ():
177-                 url_details [p_k ]["parameters" ] =  p_o ["get" ]["parameters" ]
124+         method  =  list (p_o )[0 ]
125+         if  "200"  in  p_o [method ]["responses" ].keys ():
126+             base_res  =  p_o [method ]["responses" ]["200" ]
127+         elif  200  in  p_o [method ]["responses" ].keys ():
128+             # if you read a plain yml file the 200 will be an integer 
129+             base_res  =  p_o [method ]["responses" ][200 ]
130+         else :
131+             # the endpoint does not have a 200 response 
132+             continue 
133+ 
134+         if  "description"  in  p_o [method ].keys ():
135+             desc  =  p_o [method ]["description" ]
136+         elif  "summary"  in  p_o [method ].keys ():
137+             desc  =  p_o [method ]["summary" ]
138+         else :  # still testing 
139+             desc  =  "" 
140+ 
141+         try :
142+             tags  =  p_o [method ]["tags" ]
143+         except  KeyError :
144+             tags  =  []
145+ 
146+         url_details [p_k ] =  {"description" : desc , "tags" : tags , "method" : method }
147+ 
148+         example_data  =  check_for_api_example_data (base_res , p_k )
149+         if  example_data :
150+             url_details [p_k ]["data" ] =  example_data 
151+ 
152+         # checking whether there are defined parameters to execute the call... 
153+         if  "parameters"  in  p_o [method ].keys ():
154+             url_details [p_k ]["parameters" ] =  p_o [method ]["parameters" ]
178155
179156    return  dict (sorted (url_details .items ()))
180157
181158
159+ def  check_for_api_example_data (base_res : dict , key : str ) ->  dict :
160+     """ 
161+     Try to determine if example data is defined for the endpoint, and return it 
162+     """ 
163+     data  =  {}
164+     if  "content"  in  base_res .keys ():
165+         res_cont  =  base_res ["content" ]
166+         if  "application/json"  in  res_cont .keys ():
167+             ex_field  =  None 
168+             if  "example"  in  res_cont ["application/json" ]:
169+                 ex_field  =  "example" 
170+             elif  "examples"  in  res_cont ["application/json" ]:
171+                 ex_field  =  "examples" 
172+ 
173+             if  ex_field :
174+                 if  isinstance (res_cont ["application/json" ][ex_field ], dict ):
175+                     data  =  res_cont ["application/json" ][ex_field ]
176+                 elif  isinstance (res_cont ["application/json" ][ex_field ], list ):
177+                     # taking the first example 
178+                     data  =  res_cont ["application/json" ][ex_field ][0 ]
179+             else :
180+                 logger .warning (
181+                     f"Field in swagger file does not give consistent data --- { key }  
182+                 )
183+         elif  "text/csv"  in  res_cont .keys ():
184+             data  =  res_cont ["text/csv" ]["schema" ]
185+     elif  "examples"  in  base_res .keys ():
186+         data  =  base_res ["examples" ]["application/json" ]
187+ 
188+     return  data 
189+ 
190+ 
182191def  guessing_url_name (url : str , examples : dict ) ->  str :
183192    """ 
184193    given a url and dict of extracted data, we try to guess a working URL. Example: 
@@ -314,12 +323,10 @@ def extract_fields(
314323            return  ["contains_a_string" ], {"contains_a_string" : dict_data [0 ]}
315324        else :
316325            raise  ValueError ("unknown format" )
317-     if  len (dict_data . keys () ) >  1 :
326+     if  len (dict_data ) >  1 :
318327        # the elements are directly inside the dict 
319328        return  flatten2list (dict_data ), dict_data 
320-     dst_key  =  list (dict_data .keys ())[
321-         0 
322-     ]  # the first and unique key is the dataset's name 
329+     dst_key  =  list (dict_data )[0 ]  # the first and unique key is the dataset's name 
323330
324331    try :
325332        return  flatten2list (dict_data [dst_key ]), dict_data [dst_key ]
0 commit comments