22
33import json , yaml
44from pathlib import Path
5+ import sys
56from processors .shared_functions import (
67 LiteralStr ,
78 literal_str_representer ,
9+ html_to_md ,
810 init_openapi_spec ,
911 add_info ,
1012 add_servers ,
@@ -49,10 +51,17 @@ def process(model_entry):
4951
5052 shapes = model_data .get ("shapes" , model_data )
5153
54+ shapes_dict = {
55+ "service" : [],
56+ "operation" : []
57+ }
58+
5259 for shape_name , shape in shapes .items ():
5360 if shape .get ("type" ) == "service" :
5461 add_info (openapi_spec , shape )
5562 add_servers (openapi_spec , file_name , shape )
63+ shape ["my_name" ] = shape_name
64+ shapes_dict ["service" ].append (shape )
5665 elif shape .get ("type" ) == "string" :
5766 add_component_schema_string (openapi_spec , shape_name , shape )
5867 elif shape .get ("type" ) == "boolean" :
@@ -83,7 +92,22 @@ def process(model_entry):
8392 add_component_schema_structure (openapi_spec , shape_name , shape )
8493 elif shape .get ("type" ) == "operation" :
8594 add_operation (openapi_spec , shape_name , shape , shapes )
95+ shape ["my_name" ] = shape_name
96+ shapes_dict ["operation" ].append (shape )
97+
98+ # process the service to get the paths
99+ service_name2 = model_entry ['servicename' ].split ('#' )[1 ]
100+
101+ # Sort the operations, we will need them to be in alphabetic order for creating paths
102+ shapes_dict ["operation" ].sort (key = lambda x : x ["my_name" ])
103+
104+ # Setup the "paths" attribute
105+ openapi_spec ["paths" ] = {}
86106
107+ # create the path
108+ for operation in shapes_dict ["operation" ]:
109+ key_string = "/#X-Amz-Target=" + service_name2 + "." + operation ["my_name" ].split ('#' )[1 ]
110+ openapi_spec ["paths" ][key_string ] = create_path (operation , service_name2 )
87111
88112 # Write output YAML
89113 outdir = Path ("testdir" )
@@ -92,8 +116,63 @@ def process(model_entry):
92116 with open (outfile , "w" , encoding = "utf-8" ) as f :
93117 yaml .dump (openapi_spec , f , sort_keys = False , allow_unicode = True )
94118
119+ def create_path (operation , service_name2 ):
120+ result = {}
121+ result ["post" ] = {}
122+ result_post = result ["post" ]
123+
124+ result_post ["operationId" ] = operation ["my_name" ].split ('#' )[1 ]
125+ result_post ["description" ] = LiteralStr (html_to_md (operation ["traits" ].get ("smithy.api#documentation" , "" )))
126+
127+ result_post ["requestBody" ] = {}
128+ result_request_body = result_post ["requestBody" ]
129+ result_request_body ["required" ] = True
130+ result_request_body ["content" ] = {}
131+ result_request_body ["content" ]["application/json" ] = {}
132+ result_request_body ["content" ]["application/json" ]["schema" ] = {}
133+ result_request_body ["content" ]["application/json" ]["schema" ]["$ref" ] = "#/components/schemas/" + operation ["input" ]["target" ].split ("#" )[1 ]
134+
135+ result_post ["parameters" ] = {}
136+ result_parameters_inside = result_post ["parameters" ]
137+ result_parameters_inside ["name" ] = "X-Amz-Target"
138+ result_parameters_inside ["in" ] = "header"
139+ result_parameters_inside ["required" ] = True
140+ result_parameters_inside ["schema" ] = {}
141+ result_parameters_inside ["schema" ]["type" ] = "string"
142+ result_parameters_inside ["schema" ]["enum" ] = [service_name2 + "." + operation ["my_name" ].split ('#' )[1 ]]
143+
144+ # Static Information
145+ result ["parameters" ] = []
146+ result ["parameters" ].append ({ '$ref' : '#/components/parameters/X-Amz-Content-Sha256' })
147+ result ["parameters" ].append ({ '$ref' : '#/components/parameters/X-Amz-Date' })
148+ result ["parameters" ].append ({ '$ref' : '#/components/parameters/X-Amz-Algorithm' })
149+ result ["parameters" ].append ({ '$ref' : '#/components/parameters/X-Amz-Credential' })
150+ result ["parameters" ].append ({ '$ref' : '#/components/parameters/X-Amz-Security-Token' })
151+ result ["parameters" ].append ({ '$ref' : '#/components/parameters/X-Amz-Signature' })
152+ result ["parameters" ].append ({ '$ref' : '#/components/parameters/X-Amz-SignedHeaders' })
153+
154+ result_post ["responses" ] = {}
155+ result_responses = result_post ["responses" ]
156+ # 200 response
157+ result_responses ["200" ] = {}
158+ result_responses ["200" ]["description" ] = "Success"
159+ result_responses ["200" ]["content" ] = {}
160+ result_responses ["200" ]["content" ]["application/json" ] = {}
161+ result_responses ["200" ]["content" ]["application/json" ]["schema" ] = {"$ref" : ('#/components/schemas/' + operation ["output" ]["target" ].split ('#' )[1 ])}
162+
163+ error_code = 480
164+
165+ if operation .get ("errors" , False ) is not False :
166+ for error in operation ["errors" ]:
167+ error_string = str (error_code )
168+ error_name = error ["target" ].split ('#' )[1 ]
169+
170+ result_responses [error_string ] = {}
171+ result_responses [error_string ]["description" ] = error_name
172+ result_responses [error_string ]["content" ] = {}
173+ result_responses [error_string ]["content" ]["application/json" ] = {}
174+ result_responses [error_string ]["content" ]["application/json" ]["schema" ] = {"$ref" : ('#/components/schemas/' + error_name )}
95175
176+ error_code += 1
96177
97- # def process(model_entry):
98- # service_name = model_entry['servicename'].split('#')[0].split('com.amazonaws.')[1]
99- # print(f"processing {service_name} with protocol {model_entry['protocol']}")
178+ return result
0 commit comments