|
4 | 4 | class ApiTesting(): |
5 | 5 | default_url = "https://127.0.0.1:8000" |
6 | 6 | default_headers = {} |
7 | | - # Make GET request |
| 7 | + invalid_schema_message = "Check whether the URL is valid or check if" + "the localhost server is active or not" |
| 8 | + # fetches the input data for making a request |
8 | 9 | @classmethod |
9 | | - def get_request(cls): |
| 10 | + def fetch_input_url(cls): |
10 | 11 | request_url = cls.default_url |
11 | 12 | request_headers = cls.default_headers |
12 | 13 | input_url = input('Enter URL: ') |
@@ -37,28 +38,67 @@ def get_request(cls): |
37 | 38 | request_url += endpoint |
38 | 39 |
|
39 | 40 | print("Trying ...\u26A1") |
| 41 | + return { |
| 42 | + "request_url" : request_url, |
| 43 | + "request_headers" : request_headers, |
| 44 | + } |
40 | 45 |
|
| 46 | + #saves the json response into a file |
| 47 | + @classmethod |
| 48 | + def save_response_data(cls,response_data): |
| 49 | + store_data = input('Store response data? (Y/N): ') |
| 50 | + if(store_data.lower() == 'y'): |
| 51 | + filename = input("Enter a filename (response_data.json)") |
| 52 | + if filename == '': |
| 53 | + filename = 'response_data.json' |
| 54 | + with open(filename, 'w') as jsonFile: |
| 55 | + json.dump(response_data, jsonFile, indent=4) |
| 56 | + print(f"Response data stored in {filename}") |
| 57 | + elif(store_data.lower()) == 'n': |
| 58 | + print(f"You have entered {store_data}, So the response is not saved") |
| 59 | + else: |
| 60 | + print(f"You have entered {store_data}, please enter either Y or N") |
| 61 | + cls.save_response_data(response_data) |
| 62 | + # formats the response data and prints it in json on console |
| 63 | + @classmethod |
| 64 | + def print_response_json(cls,response): |
| 65 | + print(f"Reponse Status Code: {response.status_code}") |
| 66 | + response_data = json.loads(response.content) |
| 67 | + parsed_json = json.dumps(response_data, indent=4) |
| 68 | + output_json = highlight(parsed_json, lexers.JsonLexer(), |
| 69 | + formatters.TerminalFormatter()) |
| 70 | + print(output_json) |
| 71 | + |
| 72 | + # Make GET request |
| 73 | + @classmethod |
| 74 | + def get_request(cls): |
| 75 | + request_data = cls.fetch_input_url() |
41 | 76 | # Make GET request and store the response in response_data.json |
42 | 77 | try: |
43 | | - response = requests.get(request_url, headers=request_headers) |
44 | | - print(f"Reponse Status Code: {response.status_code}") |
| 78 | + response = requests.get(request_data["request_url"], headers= request_data["request_headers"]) |
| 79 | + cls.print_response_json(response) |
45 | 80 | response_data = json.loads(response.content) |
46 | | - parsed_json = json.dumps(response_data, indent=4) |
47 | | - output_json = highlight(parsed_json, lexers.JsonLexer(), |
48 | | - formatters.TerminalFormatter()) |
49 | | - print(output_json) |
| 81 | + cls.save_response_data(response_data) |
50 | 82 |
|
51 | | - store_data = input('Store response data? (Y/N): ') |
52 | | - if(store_data == 'Y' or store_data == 'y'): |
53 | | - with open('response_data.json', 'w') as jsonFile: |
54 | | - json.dump(response_data, jsonFile, indent=4) |
55 | | - print("Response data stored in response_data.json") |
| 83 | + except requests.exceptions.InvalidSchema: |
| 84 | + print(cls.invalid_schema_message) |
| 85 | + except Exception as exception_obj: |
| 86 | + print(exception_obj) |
| 87 | + # Make a delete request |
| 88 | + @classmethod |
| 89 | + def delete_request(cls): |
| 90 | + # request_data contains dictionary of inputs entered by user |
| 91 | + request_data = cls.fetch_input_url() |
| 92 | + try: |
| 93 | + response = requests.delete(request_data["request_url"], headers= request_data["request_headers"]) |
| 94 | + cls.print_response_json(response) |
| 95 | + response_data = json.loads(response.content) |
| 96 | + cls.save_response_data(response_data) |
56 | 97 |
|
57 | 98 | except requests.exceptions.InvalidSchema: |
58 | | - print("Check whether the URL is valid or check if " + |
59 | | - "the localhost server is active or not") |
60 | | - except Exception as e: |
61 | | - print(e) |
| 99 | + print(cls.invalid_schema_message) |
| 100 | + except Exception as exception_obj: |
| 101 | + print(exception_obj) |
62 | 102 |
|
63 | 103 | @classmethod |
64 | 104 | def __check_endpoint(cls, request_url): |
|
0 commit comments