44class ApiTesting ():
55 default_url = "https://127.0.0.1:8000"
66 default_headers = {}
7- # Make GET request
7+ InvalidSchemaMessage = "Check whether the URL is valid or check if " +
8+ "the localhost server is active or not"
9+ # fetches the input data for making a request
810 @classmethod
9- def get_request (cls ):
11+ def fetch_input_url (cls ):
1012 request_url = cls .default_url
1113 request_headers = cls .default_headers
1214 input_url = input ('Enter URL: ' )
@@ -37,129 +39,60 @@ def get_request(cls):
3739 request_url += endpoint
3840
3941 print ("Trying ...\u26A1 " )
42+ return {
43+ "request_url" : request_url ,
44+ "request_headers" : request_headers ,
45+ }
4046
41- # Make GET request and store the response in response_data.json
42- try :
43- response = requests .get (request_url , headers = request_headers )
44- print (f"Reponse Status Code: { response .status_code } " )
45- 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 )
50-
51- store_data = input ('Store response data? (Y/N): ' )
52- if (store_data == 'Y' or store_data == 'y' ):
47+ #saves the json response into a file
48+ @classmethod
49+ def save_response_data (cls ,output_json ):
50+ store_data = input ('Store response data? (Y/N): ' )
51+ if (store_data == 'Y' or store_data == 'y' ):
5352 with open ('response_data.json' , 'w' ) as jsonFile :
5453 json .dump (response_data , jsonFile , indent = 4 )
5554 print ("Response data stored in response_data.json" )
56-
57- 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 )
55+
56+ # formats the response data and prints it in json on console
6257 @classmethod
63- def delete_endpoint_request (cls ):
64- request_url = cls .default_url
65- request_headers = cls .default_headers
66- input_url = input ('Enter URL: ' )
67- input_headers = input ('Enter Headers: ' )
68- if input_url != '' :
69- request_url = input_url
70- if input_headers != '' :
71- try :
72- request_headers = json .loads (input_headers )
73- except Exception :
74- print ("Failed to parse Input Headers" )
75- # Check whether the request_url has an endpoint or not
76- has_endpoint = cls .__check_endpoint (request_url )
77-
78- # Check if http:// or https:// is present in request_url
79- has_protocol = cls .__check_protocol (request_url )
80-
81- if not (has_protocol ):
82- request_url = "https://" + request_url
83-
84- # Ask the user for endpoint if not present in request_url
85- if not (has_endpoint ):
86- if (request_url [- 1 ] == '/' ):
87- endpoint = input ("Input endpoint " +
88- "(Without the starting slash): " )
89- else :
90- endpoint = input ("Input endpoint (With the starting slash): " )
91- request_url += endpoint
92-
93- print ("Trying ...\u26A1 " )
94- try :
95- response = requests .delete (request_url , headers = request_headers )
96- print (f"Reponse Status Code: { response .status_code } " )
97- response_data = json .loads (response .content )
98- parsed_json = json .dumps (response_data , indent = 4 )
99- output_json = highlight (parsed_json , lexers .JsonLexer (),
58+ def print_response_json (cls ,response ):
59+ print (f"Reponse Status Code: { response .status_code } " )
60+ response_data = json .loads (response .content )
61+ parsed_json = json .dumps (response_data , indent = 4 )
62+ output_json = highlight (parsed_json , lexers .JsonLexer (),
10063 formatters .TerminalFormatter ())
101- print (output_json )
64+ print (output_json )
65+ return output_json
10266
103- store_data = input ('Store response data? (Y/N): ' )
104- if (store_data == 'Y' or store_data == 'y' ):
105- with open ('response_data.json' , 'w' ) as jsonFile :
106- json .dump (response_data , jsonFile , indent = 4 )
107- print ("Response data stored in response_data.json" )
67+ # Make GET request
68+ @classmethod
69+ def get_request (cls ):
70+ request_data = cls .fetch_input_url ()
71+ # Make GET request and store the response in response_data.json
72+ try :
73+ response = requests .get (request_data ["request_url" ], headers = request_data ["request_headers" ])
74+ output_json = cls .print_response_json (response )
75+ cls .save_response_data (output_json )
10876
10977 except requests .exceptions .InvalidSchema :
110- print ("Check whether the URL is valid or check if " +
111- "the localhost server is active or not" )
78+ print (cls .InvalidSchemaMessage )
11279 except Exception as e :
11380 print (e )
114-
115- # Make GET request and store the response in response_data.json
81+ # Make a delete request
11682 @classmethod
11783 def delete_endpoint_request (cls ):
118- request_url = cls .default_url
119- request_headers = cls .default_headers
120- input_url = input ('Enter URL: ' )
121- input_headers = input ('Enter Headers: ' )
122- if input_url != '' :
123- request_url = input_url
124- if input_headers != '' :
125- try :
126- request_headers = json .loads (input_headers )
127- except Exception :
128- print ("Failed to parse Input Headers" )
129- # Check whether the request_url has an endpoint or not
130- has_endpoint = cls .__check_endpoint (request_url )
131-
132- # Check if http:// or https:// is present in request_url
133- has_protocol = cls .__check_protocol (request_url )
134-
135- if not (has_protocol ):
136- request_url = "https://" + request_url
137-
138- # Ask the user for endpoint if not present in request_url
139- if not (has_endpoint ):
140- if (request_url [- 1 ] == '/' ):
141- endpoint = input ("Input endpoint " +
142- "(Without the starting slash): " )
143- else :
144- endpoint = input ("Input endpoint (With the starting slash): " )
145- request_url += endpoint
146-
147- print ("Trying ...\u26A1 " )
148- # Makes a DELETE request to the given endpoint
84+
85+ # request_data contains dictionary of inputs entered by user
86+ request_data = cls .fetch_input_url ()
14987 try :
150- response = requests .delete (request_url , headers = request_headers )
151- print (f"Reponse Status Code: { response .status_code } " )
152- response_data = json .loads (response .content )
153- parsed_json = json .dumps (response_data , indent = 4 )
154- output_json = highlight (parsed_json , lexers .JsonLexer (),
155- formatters .TerminalFormatter ())
156- print (output_json )
88+ response = requests .delete (request_data ["request_url" ], headers = request_data ["request_headers" ])
89+ output_json = cls .print_response_json (response )
90+ cls .save_response_data (output_json )
15791
15892 except requests .exceptions .InvalidSchema :
159- print ("Check whether the URL is valid or check if " +
160- "the localhost server is active or not" )
93+ print (cls .InvalidSchemaMessage )
16194 except Exception as e :
162- print (e )
95+ print (e )
16396
16497 @classmethod
16598 def __check_endpoint (cls , request_url ):
@@ -173,4 +106,5 @@ def __check_protocol(cls, request_url):
173106 if (request_url [:4 ] == 'http' ):
174107 return True
175108 else :
176- return False
109+ return False
110+
0 commit comments