-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalize_openapi.py
More file actions
136 lines (114 loc) · 4.38 KB
/
normalize_openapi.py
File metadata and controls
136 lines (114 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
import argparse
import copy
import json
import pathlib
SUCCESS_RESPONSE_DESCRIPTION = "Successful JustSerpAPI response."
UNEXPECTED_RESPONSE_DESCRIPTION = "Unexpected response."
def build_success_schema():
return {
"type": "object",
"description": "Standard JustSerpAPI response envelope.",
"required": ["code", "message", "data", "requestId", "timestamp"],
"properties": {
"code": {
"type": "integer",
"format": "int32",
"description": "Application-level status code."
},
"message": {
"type": "string",
"description": "Response message."
},
"data": {
"type": "object",
"description": "Endpoint-specific payload.",
"additionalProperties": True
},
"requestId": {
"type": "string",
"description": "Server-generated request identifier."
},
"timestamp": {
"type": "integer",
"format": "int64",
"description": "Server timestamp in epoch milliseconds."
}
}
}
def build_security_schemes():
return {
"JustSerpApiKeyHeader": {
"type": "apiKey",
"in": "header",
"name": "X-API-Key",
"description": "Primary authentication header for JustSerpAPI."
},
"JustSerpApiKeyQuery": {
"type": "apiKey",
"in": "query",
"name": "api_key",
"description": "Compatibility fallback query parameter for JustSerpAPI."
}
}
def normalize_operation(operation):
normalized = copy.deepcopy(operation)
normalized["tags"] = ["Google"]
responses = normalized.setdefault("responses", {})
default_response = responses.get("default", {})
examples = (
default_response.get("content", {})
.get("application/json", {})
.get("examples", {})
)
success_response = {
"description": SUCCESS_RESPONSE_DESCRIPTION,
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/JustSerpApiResponse"}
}
}
}
if examples:
success_response["content"]["application/json"]["examples"] = copy.deepcopy(examples)
responses["200"] = success_response
responses["default"] = {"description": UNEXPECTED_RESPONSE_DESCRIPTION}
normalized["security"] = [{"JustSerpApiKeyHeader": [], "JustSerpApiKeyQuery": []}]
return normalized
def normalize_spec(document):
normalized = copy.deepcopy(document)
normalized["openapi"] = "3.0.3"
normalized["servers"] = [{
"url": "https://api.justserpapi.com",
"description": "JustSerpAPI production server"
}]
normalized["tags"] = [{
"name": "Google",
"description": "Google search and discovery endpoints."
}]
normalized["security"] = [{"JustSerpApiKeyHeader": [], "JustSerpApiKeyQuery": []}]
components = normalized.setdefault("components", {})
schemas = components.setdefault("schemas", {})
schemas["JustSerpApiResponse"] = build_success_schema()
security_schemes = components.setdefault("securitySchemes", {})
security_schemes.update(build_security_schemes())
paths = normalized.get("paths", {})
for methods in paths.values():
for method_name, operation in list(methods.items()):
if isinstance(operation, dict):
methods[method_name] = normalize_operation(operation)
return normalized
def main() -> int:
parser = argparse.ArgumentParser(description="Normalize the upstream OpenAPI document for Java SDK generation.")
parser.add_argument("--input", required=True, help="Path to the raw OpenAPI JSON.")
parser.add_argument("--output", required=True, help="Path to write the normalized OpenAPI JSON.")
args = parser.parse_args()
input_path = pathlib.Path(args.input)
output_path = pathlib.Path(args.output)
payload = json.loads(input_path.read_text(encoding="utf-8"))
normalized = normalize_spec(payload)
output_path.parent.mkdir(parents=True, exist_ok=True)
output_path.write_text(json.dumps(normalized, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
return 0
if __name__ == "__main__":
raise SystemExit(main())