@@ -68,7 +68,6 @@ def get_type_for_param(param: dict, swagger: dict | None = None) -> str:
6868 all_of = get_path (param , "allOf.0.$ref" , None )
6969 any_of = get_path (param , "anyOf" , None )
7070
71-
7271 if any_of :
7372 types = []
7473 _list = any_of
@@ -86,6 +85,8 @@ def get_type_for_param(param: dict, swagger: dict | None = None) -> str:
8685 types .append (
8786 COMPLEX_TYPES_MAP [(_type , items_type )]
8887 )
88+ if len (types ) == 1 :
89+ return types [0 ]
8990 return "Union[%s]" % ", " .join (types )
9091
9192 while ref_path or all_of :
@@ -171,8 +172,8 @@ def create_headers_block(required_header_params_without_special, optional_header
171172 "headers = self.auth.get_headers()" ,
172173 "if data_partition_id:" ,
173174 f"{ INDENT } headers['data-partition-id'] = data_partition_id" ,
174- #"if tenant:",
175- #f"{INDENT}headers['tenant'] = tenant"
175+ # "if tenant:",
176+ # f"{INDENT}headers['tenant'] = tenant"
176177 ]
177178 if required_header_params_without_special :
178179 lines .append (
@@ -326,12 +327,8 @@ def get_name_from_name_map(method: str, path: str, method_names_file: str):
326327
327328
328329def create_method_name (method : str , path : str , name_parts : list [str ], swagger : dict , method_names_file : str ) -> str :
329- # try:
330330 summary = swagger ["paths" ][path ][method ].get ("summary" )
331331 description = swagger ["paths" ][path ][method ].get ("description" , "" )
332- # except TypeError as e:
333- # print(name_parts, path)
334- # raise e
335332 name = get_name_from_name_map (method , path , method_names_file )
336333
337334 if name :
@@ -447,7 +444,7 @@ def create_validation_block(path: str, method: str, swagger: dict, name: str) ->
447444 model_class = schema_path .rsplit ("/" , 1 )[- 1 ]
448445 lines = [
449446 "if self.validation:" ,
450- "%svalidate_data(request_data, %s, %sAPIError )" % (INDENT , model_class , name ),
447+ "%svalidate_data(request_data, %s)" % (INDENT , model_class ),
451448 ]
452449 return "\n " .join (lines )
453450 return ""
@@ -493,7 +490,7 @@ def create_method(swagger: dict, name: str, method: str, path: str, path_templat
493490
494491 body_required , body_not_required = get_body_params (swagger , params , request_body )
495492
496- sig = create_method_sig (swagger , name , params , body_required , body_not_required ) + "\n "
493+ sig = create_method_sig (swagger , name , params , body_required , body_not_required , method , path ) + "\n "
497494
498495 body = create_method_body (path , method , params , body_required ,
499496 body_not_required , path_template , class_name , swagger )
@@ -513,33 +510,77 @@ def parse_request_body(swagger: dict, props: list[dict]) -> list[str]:
513510 return result
514511
515512
516- def create_method_sig (swagger : dict , name : str , params : list [str ], body_required : list [str ], body_not_required : list [str ]) -> str :
513+ def generate_doc_string (name , path_params , body_required , body_not_required , swagger , method , path , indent_offset : int = 1 ):
514+ description = swagger ['paths' ][path ][method ].get ("description" , "" )
515+ description = re .sub ('<[^<]+>' , "" , description )
516+ lines_1 = [
517+ (INDENT * (indent_offset ))+ '"""' ,
518+ (INDENT * (indent_offset ))+ description ,
519+ (INDENT * (indent_offset ))+ "Args:" ,
520+ (INDENT * (indent_offset + 1 ))+ "data_partition_id (str): identifier of the data partition to query. If None sets by auth session." ,
521+ ]
522+ for path_param in path_params :
523+ name = convert_to_snake_case (path_param ["name" ])
524+ _type = get_type_for_param (path_param , swagger )
525+ lines_1 .append (
526+ (INDENT * (indent_offset + 1 )) + f"{ name } ({ _type } ): { path_param .get ('description' , '' )} "
527+ )
528+
529+ for param in body_required :
530+ name = convert_to_snake_case (param ["name" ])
531+ _type = get_type_for_param (param , swagger )
532+ lines_1 .append (
533+ (INDENT * (indent_offset + 1 )) + f"{ name } ({ _type } ): { param .get ('description' , '' )} "
534+ )
535+
536+ for param in body_not_required :
537+ name = convert_to_snake_case (param ["name" ])
538+ _type = get_type_for_param (param , swagger )
539+ lines_1 .append (
540+ (INDENT * (indent_offset + 1 )) + f"{ name } ({ _type } ): { param .get ('description' , '' )} "
541+ )
542+
543+ lines_2 = [
544+ (INDENT * (indent_offset ))+ "Returns:" ,
545+ (INDENT * (indent_offset + 1 ))+ "response data (dict)" ,
546+ (INDENT * (indent_offset ))+ "Raises:" ,
547+ (INDENT * (indent_offset + 1 ))+ "OSDUValidation: if request values are wrong." ,
548+ (INDENT * (indent_offset + 1 ))+ "OSDUAPIError: if response is 4XX or 5XX" ,
549+ (INDENT * (indent_offset ))+ '"""'
550+ ]
551+ result = "\n " .join (lines_1 + lines_2 )
552+ return result
553+
554+
555+ def create_method_sig (swagger : dict , name : str , params : list [str ], body_required : list [str ], body_not_required : list [str ], method : str , path : str ) -> str :
517556 path_params = [
518557 p for p in params if p ['name' ] not in SPECIAL_HEADERS and p ["in" ] != "body"
519558 ]
520559 elements = [name , ]
521560 sig = ""
522- _input_parameters_strings = []
561+ _input_parameters_strings = [param_to_function_argument (p )
562+ for p in path_params ]
523563
524564 if body_required :
525565 _input_parameters_strings += parse_request_body (swagger , body_required )
566+
526567 if body_not_required :
527568 _input_parameters_strings += parse_request_body (swagger , body_not_required )
528-
529- _input_parameters_strings += [param_to_function_argument (p ) for p in sorted (path_params , key = lambda x : 0 if x .get ("required" ) else 1 )]
530-
569+
531570 if _input_parameters_strings :
532571 sig += ", " .join (_input_parameters_strings )
533572
534573 if sig :
535574 elements .append (sig )
536575
576+ doc = generate_doc_string (name , path_params , body_required , body_not_required ,
577+ swagger , method , path , indent_offset = 1 )
537578 try :
538579 if len (elements ) > 1 :
539580 sig = "def %s(self, *, %s, data_partition_id: str | None = None) -> dict:" % tuple (elements )
540581 else :
541582 sig = "def %s(self, data_partition_id: str | None = None) -> dict:" % tuple (elements )
542- return sig
583+ return sig + " \n " + doc
543584 except Exception as e :
544585 raise Exception (elements , len (elements )) from e
545586
0 commit comments