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,48 +29,102 @@ 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 }
4548
46- #saves the json response into a file
4749 @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' :
50+ def read_data_from_file (cls ):
51+ filename = input ("Enter a filename (response_data.json)" )
52+ data = {}
53+ if filename .strip () == "" :
54+ filename = "response_data.json"
55+ print (f"filename empty, so default file { filename } is used " )
56+ with open (filename , "r" ) as reader :
57+ file_content = reader .read ()
58+ try :
59+ json_data = json .loads (file_content )
60+ data = json_data .get ("data" )
61+ # Make sure the data is not None and send
62+ except json .JSONDecodeError :
63+ print ("Unable to parse the file, Please try again" )
64+ cls .read_data_from_file ()
65+ return data
66+
67+ @classmethod
68+ def enter_data_payload (cls ):
69+ print ("Option 1: For sending data payload from terminal\n " )
70+ print ("Option 2: For sending data payload by reading from json file\n " )
71+ store = int (input ("Please choose the above options? (1/2)" ))
72+
73+ data = {}
74+ if store == 1 :
75+ data = input ("Enter data as key value pairs" )
76+ try :
77+ data = json .loads (data )
78+ except Exception as exception_obj :
79+ print (f"Unable to load the data due to { exception_obj } , please try again \n " )
80+ cls .enter_data_payload ()
81+ elif store == 2 :
82+ data = cls .read_data_from_file ()
83+ else :
84+ print (f"you have entered { store } , please choose from above options" )
85+ cls .enter_data_payload ()
86+ return data
87+
88+ @classmethod
89+ def fetch_payload_data (cls ):
90+ store = input ("Do you want to send data payload? (Y/N)" )
91+ data = None
92+ if store .lower () == "y" :
93+ data = cls .enter_data_payload ()
94+
95+ elif store .lower () == "n" :
96+ data = {}
97+ else :
98+ print (f"You have entered { store } , please enter from the above options" )
99+ cls .fetch_payload_data ()
100+ return data
101+
102+ # saves the json response into a file
103+ @classmethod
104+ def save_response_data (cls , response_data ):
105+ store_data = input ("Store response data? (Y/N): " )
106+ if store_data .lower () == "y" :
107+ filename = input ("Enter a filename (response_data.json)" )
108+ if filename == "" :
109+ filename = "response_data.json"
110+ with open (filename , "w" ) as jsonFile :
111+ json .dump (response_data , jsonFile , indent = 4 )
112+ print (f"Response data stored in { filename } " )
113+ elif (store_data .lower ()) == "n" :
58114 print (f"You have entered { store_data } , So the response is not saved" )
59115 else :
60116 print (f"You have entered { store_data } , please enter either Y or N" )
61117 cls .save_response_data (response_data )
118+
62119 # formats the response data and prints it in json on console
63120 @classmethod
64- def print_response_json (cls ,response ):
121+ def print_response_json (cls , response ):
65122 print (f"Reponse Status Code: { response .status_code } " )
66123 response_data = json .loads (response .content )
67124 parsed_json = json .dumps (response_data , indent = 4 )
68- output_json = highlight (parsed_json , lexers .JsonLexer (),
69- formatters .TerminalFormatter ())
125+ output_json = highlight (
126+ parsed_json , lexers .JsonLexer (), formatters .TerminalFormatter ()
127+ )
70128 print (output_json )
71129
72130 # Make GET request
@@ -75,7 +133,9 @@ def get_request(cls):
75133 request_data = cls .fetch_input_url ()
76134 # Make GET request and store the response in response_data.json
77135 try :
78- response = requests .get (request_data ["request_url" ], headers = request_data ["request_headers" ])
136+ response = requests .get (
137+ request_data ["request_url" ], headers = request_data ["request_headers" ]
138+ )
79139 cls .print_response_json (response )
80140 response_data = json .loads (response .content )
81141 cls .save_response_data (response_data )
@@ -84,13 +144,35 @@ def get_request(cls):
84144 print (cls .invalid_schema_message )
85145 except Exception as exception_obj :
86146 print (exception_obj )
147+
148+ # Make a POST request
149+ @classmethod
150+ def post_request (cls ):
151+ request_data = cls .fetch_input_url ()
152+ data = cls .fetch_payload_data ()
153+ try :
154+ response = requests .post (
155+ url = request_data ["request_url" ],
156+ headers = request_data ["request_headers" ],
157+ data = data ,
158+ )
159+ cls .print_response_json (response )
160+ response_data = json .loads (response .content )
161+ cls .save_response_data (response_data )
162+ except requests .exceptions .InvalidSchema :
163+ print (cls .invalid_schema_message )
164+ except Exception as exception_obj :
165+ print (exception_obj )
166+
87167 # Make a delete request
88168 @classmethod
89169 def delete_request (cls ):
90170 # request_data contains dictionary of inputs entered by user
91171 request_data = cls .fetch_input_url ()
92172 try :
93- response = requests .delete (request_data ["request_url" ], headers = request_data ["request_headers" ])
173+ response = requests .delete (
174+ request_data ["request_url" ], headers = request_data ["request_headers" ]
175+ )
94176 cls .print_response_json (response )
95177 response_data = json .loads (response .content )
96178 cls .save_response_data (response_data )
@@ -102,14 +184,14 @@ def delete_request(cls):
102184
103185 @classmethod
104186 def __check_endpoint (cls , request_url ):
105- if ( request_url == cls .default_url ) :
187+ if request_url == cls .default_url :
106188 return False
107189 else :
108190 return True
109191
110192 @classmethod
111193 def __check_protocol (cls , request_url ):
112- if ( request_url [:4 ] == ' http' ) :
194+ if request_url [:4 ] == " http" :
113195 return True
114196 else :
115- return False
197+ return False
0 commit comments