11import requests , json
22from pygments import highlight , lexers , formatters
33
4- class ApiTesting ():
4+
5+ class ApiTesting :
56 default_url = "https://127.0.0.1:8000"
67 default_headers = {}
7- invalid_schema_message = "Check whether the URL is valid or check if" + "the localhost server is active or not"
8+ invalid_schema_message = (
9+ "Check whether the URL is valid or check if"
10+ + "the localhost server is active or not"
11+ )
812 # fetches the input data for making a request
913 @classmethod
1014 def fetch_input_url (cls ):
1115 request_url = cls .default_url
1216 request_headers = cls .default_headers
13- input_url = input (' Enter URL: ' )
14- input_headers = input (' Enter Headers: ' )
15- if input_url != '' :
17+ input_url = input (" Enter URL: " )
18+ input_headers = input (" Enter Headers: " )
19+ if input_url != "" :
1620 request_url = input_url
17- if input_headers != '' :
21+ if input_headers != "" :
1822 try :
1923 request_headers = json .loads (input_headers )
2024 except Exception :
@@ -25,80 +29,100 @@ def fetch_input_url(cls):
2529 # Check if http:// or https:// is present in request_url
2630 has_protocol = cls .__check_protocol (request_url )
2731
28- if not (has_protocol ):
32+ if not (has_protocol ):
2933 request_url = "https://" + request_url
3034
3135 # Ask the user for endpoint if not present in request_url
32- if not (has_endpoint ):
33- if (request_url [- 1 ] == '/' ):
34- endpoint = input ("Input endpoint " +
35- "(Without the starting slash): " )
36+ if not (has_endpoint ):
37+ if request_url [- 1 ] == "/" :
38+ endpoint = input ("Input endpoint " + "(Without the starting slash): " )
3639 else :
3740 endpoint = input ("Input endpoint (With the starting slash): " )
3841 request_url += endpoint
3942
4043 print ("Trying ...\u26A1 " )
4144 return {
42- "request_url" : request_url ,
43- "request_headers" : request_headers ,
45+ "request_url" : request_url ,
46+ "request_headers" : request_headers ,
4447 }
48+
4549 @classmethod
4650 def read_data_from_file (cls ):
4751 filename = input ("Enter a filename (response_data.json)" )
48- if filename == '' :
52+ data = {}
53+ if len (filename .trim ()) == 0 :
4954 print ("filename empty, so default file (response_data.json) is used " )
5055 with open (filename , "r" ) as reader :
5156 file_content = reader .read ()
5257 try :
5358 json_data = json .loads (file_content )
5459 data = json_data .get ("data" )
55- return data
5660 # Make sure the data is not None and send
5761 except json .JSONDecodeError :
58- print ("Unable to parse the file, Please try again" )
59- cls .read_data_from_file ()
62+ print ("Unable to parse the file, Please try again" )
63+ cls .read_data_from_file ()
64+ return data
65+
6066 @classmethod
61- def fetch_payload_data (cls ):
67+ def enter_data_payload (cls ):
6268 store = int (input ("Please choose the below options? (1/2/3)" ))
6369 print ("Option 1: For sending data payload from terminal\n " )
6470 print ("Option 2: For sending data payload by reading from json file\n " )
65- print ("Option 3: For not sending any data to POST request" )
66- data = None
67- if (store == 1 ):
71+ data = {}
72+ if store == 1 :
6873 data = input ("Enter data as key value pairs" )
69- data = json .loads (data )
70- elif (store == 2 ):
74+ try :
75+ data = json .loads (data )
76+ except Exception as exception_obj :
77+ print ("Unable to load the data, please try again \n " )
78+ cls .enter_data_payload ()
79+ elif store == 2 :
7180 data = cls .read_data_from_file ()
72- elif (store == 3 ):
73- print (f"you have entered { store } , so sending POST request without any data" )
81+ else :
82+ print (f"you have entered { store } , please choose from above options" )
83+ cls .enter_data_payload ()
84+ return data
85+
86+ @classmethod
87+ def fetch_payload_data (cls ):
88+ store = input ("Do you want to send data payload? (Y/N)" )
89+ data = None
90+ if store .lower () == "y" :
91+ data = cls .enter_data_payload ()
92+
93+ elif store .lower () == "n" :
94+ data = {}
7495 else :
7596 print (f"You have entered { store } , please enter from the above options" )
7697 cls .fetch_payload_data ()
7798 return data
78- #saves the json response into a file
99+
100+ # saves the json response into a file
79101 @classmethod
80- def save_response_data (cls ,response_data ):
81- store_data = input (' Store response data? (Y/N): ' )
82- if ( store_data .lower () == 'y' ) :
83- filename = input ("Enter a filename (response_data.json)" )
84- if filename == '' :
85- filename = ' response_data.json'
86- with open (filename , 'w' ) as jsonFile :
87- json .dump (response_data , jsonFile , indent = 4 )
88- print (f"Response data stored in { filename } " )
89- elif (store_data .lower ()) == 'n' :
102+ def save_response_data (cls , response_data ):
103+ store_data = input (" Store response data? (Y/N): " )
104+ if store_data .lower () == "y" :
105+ filename = input ("Enter a filename (response_data.json)" )
106+ if filename == "" :
107+ filename = " response_data.json"
108+ with open (filename , "w" ) as jsonFile :
109+ json .dump (response_data , jsonFile , indent = 4 )
110+ print (f"Response data stored in { filename } " )
111+ elif (store_data .lower ()) == "n" :
90112 print (f"You have entered { store_data } , So the response is not saved" )
91113 else :
92114 print (f"You have entered { store_data } , please enter either Y or N" )
93115 cls .save_response_data (response_data )
116+
94117 # formats the response data and prints it in json on console
95118 @classmethod
96- def print_response_json (cls ,response ):
119+ def print_response_json (cls , response ):
97120 print (f"Reponse Status Code: { response .status_code } " )
98121 response_data = json .loads (response .content )
99122 parsed_json = json .dumps (response_data , indent = 4 )
100- output_json = highlight (parsed_json , lexers .JsonLexer (),
101- formatters .TerminalFormatter ())
123+ output_json = highlight (
124+ parsed_json , lexers .JsonLexer (), formatters .TerminalFormatter ()
125+ )
102126 print (output_json )
103127
104128 # Make GET request
@@ -107,7 +131,9 @@ def get_request(cls):
107131 request_data = cls .fetch_input_url ()
108132 # Make GET request and store the response in response_data.json
109133 try :
110- response = requests .get (request_data ["request_url" ], headers = request_data ["request_headers" ])
134+ response = requests .get (
135+ request_data ["request_url" ], headers = request_data ["request_headers" ]
136+ )
111137 cls .print_response_json (response )
112138 response_data = json .loads (response .content )
113139 cls .save_response_data (response_data )
@@ -116,27 +142,35 @@ def get_request(cls):
116142 print (cls .invalid_schema_message )
117143 except Exception as exception_obj :
118144 print (exception_obj )
119- #Make a POST request
145+
146+ # Make a POST request
120147 @classmethod
121148 def post_request (cls ):
122149 request_data = cls .fetch_input_url ()
123150 data = cls .fetch_payload_data ()
124151 try :
125- response = requests .post (url = request_data ["request_url" ], headers = request_data ["request_headers" ], data = data )
152+ response = requests .post (
153+ url = request_data ["request_url" ],
154+ headers = request_data ["request_headers" ],
155+ data = data ,
156+ )
126157 cls .print_response_json (response )
127158 response_data = json .loads (response .content )
128159 cls .save_response_data (response_data )
129160 except requests .exceptions .InvalidSchema :
130161 print (cls .invalid_schema_message )
131162 except Exception as exception_obj :
132163 print (exception_obj )
164+
133165 # Make a delete request
134166 @classmethod
135167 def delete_request (cls ):
136168 # request_data contains dictionary of inputs entered by user
137169 request_data = cls .fetch_input_url ()
138170 try :
139- response = requests .delete (request_data ["request_url" ], headers = request_data ["request_headers" ])
171+ response = requests .delete (
172+ request_data ["request_url" ], headers = request_data ["request_headers" ]
173+ )
140174 cls .print_response_json (response )
141175 response_data = json .loads (response .content )
142176 cls .save_response_data (response_data )
@@ -148,14 +182,14 @@ def delete_request(cls):
148182
149183 @classmethod
150184 def __check_endpoint (cls , request_url ):
151- if ( request_url == cls .default_url ) :
185+ if request_url == cls .default_url :
152186 return False
153187 else :
154188 return True
155189
156190 @classmethod
157191 def __check_protocol (cls , request_url ):
158- if ( request_url [:4 ] == ' http' ) :
192+ if request_url [:4 ] == " http" :
159193 return True
160194 else :
161- return False
195+ return False
0 commit comments