diff --git a/.gitignore b/.gitignore index 4417d0f..4652e55 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/src/create_insomnia_json.py b/src/create_insomnia_json.py new file mode 100644 index 0000000..a204c91 --- /dev/null +++ b/src/create_insomnia_json.py @@ -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() diff --git a/src/serialize_data.py b/src/serialize_data.py index 138aa3f..4f38470 100644 --- a/src/serialize_data.py +++ b/src/serialize_data.py @@ -1,4 +1,5 @@ import json + import click HTTP_VERBS = ("GET", "POST", "HEAD", "OPTIONS", "PUT", "PATCH", "DELETE") @@ -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( @@ -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( @@ -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 ""