import json
import os
import random
import string
current_dir = os.path.dirname(os.path.realpath(__file__))
project_root = os.path.dirname(current_dir)
all_dir = os.path.join(project_root, "tmp-swagger-gen", "_all")
version = ""
with open(os.path.join(project_root, "go.mod"), "r") as f:
for line in f.readlines():
if line.startswith("module"):
version = line.split("/")[-1].strip()
break
if not version:
print("Could not find version in go.mod")
exit(1)
output: dict
output = {
"swagger": "2.0",
"info": {"title": "Bostrom network", "version": version},
"consumes": ["application/json"],
"produces": ["application/json"],
"paths": {},
"definitions": {},
}
for file in os.listdir(all_dir):
if not file.endswith(".json"):
continue
with open(os.path.join(all_dir, file), "r") as f:
data = json.load(f)
for key in data["paths"]:
output["paths"][key] = data["paths"][key]
for key in data["definitions"]:
output["definitions"][key] = data["definitions"][key]
for path in output["paths"]:
for method in output["paths"][path]:
if "operationId" in output["paths"][path][method]:
output["paths"][path][method][
"operationId"
] = f'{output["paths"][path][method]["operationId"]}_' + "".join(
random.choices(string.ascii_uppercase + string.digits, k=5)
)
with open(
os.path.join(project_root, "tmp-swagger-gen", "_all", "FINAL.json"), "w"
) as f:
json.dump(output, f, indent=2)