Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ venv/
src/app.py
src/proposed_endpoints.md
src/endpoints_data.json
src/endpoints_data_insomnia.json

# tests cache
.pytest_cache/
100 changes: 100 additions & 0 deletions src/create_insomnia_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import copy
import datetime
import json
import time
import uuid


def random_id():
return uuid.uuid4().hex


def current_timestamp():
return int(round(time.time() * 1000))


def current_date():
date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")
return date[:-3] + "Z"


wrk_id = random_id()

base_json = {
"_type": "export",
"__export_format": 4,
"__export_date": current_date(),
"__export_source": "insomnia.desktop.app:v7.1.1",
"resources": [
{
"_id": f"wrk_{wrk_id}",
"created": current_timestamp(),
"description": "",
"modified": current_timestamp(),
"name": "Stub API",
"parentId": "null",
"_type": "workspace"
},
{
"_id": f"env_{random_id()}",
"color": "null",
"created": current_timestamp(),
"data": {},
"dataPropertyOrder": "null",
"isPrivate": "false",
"modified": current_timestamp(),
"name": "Base Environment",
"parentId": f"wrk_{wrk_id}",
"_type": "environment"
},
]
}


base_request_resource = {
"_id": "",
"authentication": {},
"body": {},
"created": current_timestamp(),
"description": "",
"headers": [],
"isPrivate": "false",
"method": "",
"modified": current_timestamp(),
"name": "",
"parameters": [],
"parentId": f"wrk_{wrk_id}",
"url": "",
"_type": "request"
}


def create_json(list_of_endpoints):
for endpoint in list_of_endpoints:
request_resource = copy.deepcopy(base_request_resource)
request_resource["_id"] = f"req_{random_id()}"
request_resource["method"] = endpoint["method"]
if endpoint["request_body"] != "":
request_resource["body"]["mimetype"] = "application/json"
request_resource["body"]["text"] = json.dumps(
endpoint["request_body"], indent=2
)
request_resource["name"] = endpoint["description"]
request_resource["url"] = endpoint["endpoint"]
base_json["resources"].append(request_resource)
return base_json


def create_insomnia_api_endpoints(
json_filename="endpoints_data.json", filename="endpoints_data_insomnia.json"
):
data = None
with open(json_filename, "r") as json_file:
data = json.load(json_file)
final_json = create_json(data)
with open(filename, "w") as outfile:
json.dump(final_json, outfile)


if __name__ == "__main__":
create_insomnia_api_endpoints()
15 changes: 11 additions & 4 deletions src/serialize_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json

import click

HTTP_VERBS = ("GET", "POST", "HEAD", "OPTIONS", "PUT", "PATCH", "DELETE")
Expand All @@ -24,10 +25,11 @@ def get_single_endpoint_detail(lines):
endpoint_details["endpoint"] = endpoint
endpoint_details["method"] = method
continue
if line.startswith("__Example__"):
if line.startswith("**Request**") or line.startswith("__Example__"):
json_data = parse_and_get_json_from_subsequent_lines(lines_iterator)
try:
endpoint_details["request_body"] = json.loads(json_data)
if json_data != "":
endpoint_details["request_body"] = json.loads(json_data)
except ValueError as e:
print(
"Error in parsing request_body of {}: {}".format(
Expand All @@ -37,10 +39,11 @@ def get_single_endpoint_detail(lines):
print("Invalid JSON: {}".format(json_data))
return None
continue
if line.startswith("__Response__"):
if line.startswith("**Response**") or line.startswith("__Response__"):
json_data = parse_and_get_json_from_subsequent_lines(lines_iterator)
try:
endpoint_details["response_body"] = json.loads(json_data)
if json_data != "":
endpoint_details["response_body"] = json.loads(json_data)
except ValueError as e:
print(
"Error in parsing response_body of {}: {}".format(
Expand All @@ -60,6 +63,10 @@ def parse_and_get_json_from_subsequent_lines(lines_iterator):
return ""
while next_line != "```json":
try:
# To handle case when an empty request body
# is provided.
if next_line.startswith("No-Content"):
raise StopIteration
next_line = next(lines_iterator)
except StopIteration:
return ""
Expand Down