Skip to content

Commit f197196

Browse files
committed
Add POST endpoint
1 parent acdb628 commit f197196

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
help="Make a GET request to an API",
5151
action='store_true')
5252

53+
parser.add_argument("-POST",
54+
help="Make a POST request to an API",
55+
action='store_true')
56+
5357
parser.add_argument("-DELETE",
5458
help = "Make a DELETE request to an API",
5559
action = 'store_true')

src/arguments/api_test.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,39 @@ def fetch_input_url(cls):
4242
"request_url" : request_url,
4343
"request_headers" : request_headers,
4444
}
45-
45+
@classmethod
46+
def read_data_from_file(cls):
47+
filename = input("Enter a filename (response_data.json)")
48+
if filename == '':
49+
print("filename empty, so default file (response_data.json) is used ")
50+
with open(filename, "r") as reader:
51+
file_content = reader.read()
52+
try:
53+
json_data = json.loads(file_content)
54+
data = json_data.get("data")
55+
return data
56+
# Make sure the data is not None and send
57+
except json.JSONDecodeError:
58+
print("Unable to parse the file, Please try again")
59+
cls.read_data_from_file()
60+
@classmethod
61+
def fetch_payload_data(cls):
62+
store = int(input("Please choose the below options? (1/2/3)"))
63+
print("Option 1: For sending data payload from terminal\n")
64+
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):
68+
data = input("Enter data as key value pairs")
69+
data = eval(data)
70+
elif(store == 2):
71+
data = cls.read_data_from_file()
72+
elif(store == 3):
73+
print(f"you have entered {store}, so sending POST request without any data")
74+
else:
75+
print(f"You have entered {store_data}, please enter from the above options")
76+
cls.fetch_payload_data()
77+
return data
4678
#saves the json response into a file
4779
@classmethod
4880
def save_response_data(cls,response_data):
@@ -84,6 +116,20 @@ def get_request(cls):
84116
print(cls.invalid_schema_message)
85117
except Exception as exception_obj:
86118
print(exception_obj)
119+
#Make a POST request
120+
@classmethod
121+
def post_request(cls):
122+
request_data = cls.fetch_input_url()
123+
data = cls.fetch_payload_data()
124+
try:
125+
response= requests.post(url = request_data["request_url"], headers= request_data["request_headers"], data = data)
126+
cls.print_response_json(response)
127+
response_data = json.loads(response.content)
128+
cls.save_response_data(response_data)
129+
except requests.exceptions.InvalidSchema:
130+
print(cls.invalid_schema_message)
131+
except Exception as exception_obj:
132+
print(exception_obj)
87133
# Make a delete request
88134
@classmethod
89135
def delete_request(cls):

src/arguments/search.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def search_args(self):
5454
update.check_for_updates()
5555
elif self.arguments.GET:
5656
self.api_test_object.get_request()
57+
elif self.arguments.POST:
58+
self.api_test_object.post_request()
5759
elif self.arguments.DELETE:
5860
self.api_test_object.delete_request()
5961
elif self.arguments.notion:

0 commit comments

Comments
 (0)