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