|
| 1 | +# processors/rest_json1.py |
| 2 | + |
| 3 | +import json, yaml |
| 4 | +from pathlib import Path |
| 5 | +import sys |
| 6 | +from processors.shared_functions import ( |
| 7 | + LiteralStr, |
| 8 | + literal_str_representer, |
| 9 | + html_to_md, |
| 10 | + init_openapi_spec, |
| 11 | + add_info, |
| 12 | + add_servers, |
| 13 | + add_component_schema_string, |
| 14 | + add_component_schema_boolean, |
| 15 | + add_component_schema_integer, |
| 16 | + add_component_schema_timestamp, |
| 17 | + add_component_schema_double, |
| 18 | + add_component_schema_float, |
| 19 | + add_component_schema_long, |
| 20 | + add_component_schema_blob, |
| 21 | + add_component_schema_enum, |
| 22 | + add_component_schema_map, |
| 23 | + add_component_schema_document, |
| 24 | + add_component_schema_list, |
| 25 | + add_component_schema_union, |
| 26 | + add_component_schema_structure, |
| 27 | + add_operation, |
| 28 | +) |
| 29 | + |
| 30 | +yaml.add_representer(LiteralStr, literal_str_representer) |
| 31 | + |
1 | 32 | def process(model_entry): |
| 33 | + |
| 34 | + services_to_skip = [] |
| 35 | + file_name = model_entry['filename'] |
| 36 | + if file_name in services_to_skip: |
| 37 | + print(f"skipping {file_name}") |
| 38 | + return |
| 39 | + |
| 40 | + protocol = model_entry['protocol'] |
2 | 41 | service_name = model_entry['servicename'].split('#')[0].split('com.amazonaws.')[1] |
3 | | - print(f"processing {service_name} with protocol {model_entry['protocol']}") |
| 42 | + print(f"processing {service_name} with protocol {protocol}") |
| 43 | + |
| 44 | + model_path = Path(model_entry['filepath']) |
| 45 | + |
| 46 | + with open(model_path, "r", encoding="utf-8") as f: |
| 47 | + model_data = json.load(f) |
| 48 | + |
| 49 | + # Basic OpenAPI structure |
| 50 | + openapi_spec = init_openapi_spec(service_name, file_name, protocol) |
| 51 | + |
| 52 | + shapes = model_data.get("shapes", model_data) |
| 53 | + |
| 54 | + shapes_dict = { |
| 55 | + "service": [], |
| 56 | + "operation": [] |
| 57 | + } |
| 58 | + |
| 59 | + for shape_name, shape in shapes.items(): |
| 60 | + if shape.get("type") == "service": |
| 61 | + add_info(openapi_spec, shape) |
| 62 | + add_servers(openapi_spec, file_name, shape) |
| 63 | + shape["my_name"] = shape_name |
| 64 | + shapes_dict["service"].append(shape) |
| 65 | + elif shape.get("type") == "string": |
| 66 | + add_component_schema_string(openapi_spec, shape_name, shape) |
| 67 | + elif shape.get("type") == "boolean": |
| 68 | + add_component_schema_boolean(openapi_spec, shape_name, shape) |
| 69 | + elif shape.get("type") == "integer": |
| 70 | + add_component_schema_integer(openapi_spec, shape_name, shape) |
| 71 | + elif shape.get("type") == "timestamp": |
| 72 | + add_component_schema_timestamp(openapi_spec, shape_name, shape) |
| 73 | + elif shape.get("type") == "double": |
| 74 | + add_component_schema_double(openapi_spec, shape_name, shape) |
| 75 | + elif shape.get("type") == "float": |
| 76 | + add_component_schema_float(openapi_spec, shape_name, shape) |
| 77 | + elif shape.get("type") == "long": |
| 78 | + add_component_schema_long(openapi_spec, shape_name, shape) |
| 79 | + elif shape.get("type") == "blob": |
| 80 | + add_component_schema_blob(openapi_spec, shape_name, shape) |
| 81 | + elif shape.get("type") == "enum": |
| 82 | + add_component_schema_enum(openapi_spec, shape_name, shape) |
| 83 | + elif shape.get("type") == "map": |
| 84 | + add_component_schema_map(openapi_spec, shape_name, shape) |
| 85 | + elif shape.get("type") == "document": |
| 86 | + add_component_schema_document(openapi_spec, shape_name, shape) |
| 87 | + elif shape.get("type") == "list": |
| 88 | + add_component_schema_list(openapi_spec, shape_name, shape) |
| 89 | + elif shape.get("type") == "union": |
| 90 | + add_component_schema_union(openapi_spec, shape_name, shape) |
| 91 | + elif shape.get("type") == "structure": |
| 92 | + add_component_schema_structure(openapi_spec, shape_name, shape) |
| 93 | + elif shape.get("type") == "operation": |
| 94 | + 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"] = {} |
| 106 | + |
| 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) |
| 111 | + |
| 112 | + # Write output YAML |
| 113 | + outdir = Path("testdir") |
| 114 | + outdir.mkdir(exist_ok=True) |
| 115 | + outfile = outdir / f"{service_name}.yaml" |
| 116 | + with open(outfile, "w", encoding="utf-8") as f: |
| 117 | + yaml.dump(openapi_spec, f, sort_keys=False, allow_unicode=True) |
| 118 | + |
| 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)} |
| 175 | + |
| 176 | + error_code += 1 |
| 177 | + |
| 178 | + return result |
0 commit comments