Skip to content

Commit acdb628

Browse files
Merge pull request #108 from harshakhmk/delete-endpoint
Add delete endpoint for dynamic #108
2 parents c3c8d76 + 68ca185 commit acdb628

File tree

3 files changed

+63
-17
lines changed

3 files changed

+63
-17
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("-DELETE",
54+
help = "Make a DELETE request to an API",
55+
action = 'store_true')
56+
5357
parser.add_argument("-p",
5458
"--playbook",
5559
help="View and organise the playbook",

src/arguments/api_test.py

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
class ApiTesting():
55
default_url = "https://127.0.0.1:8000"
66
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
89
@classmethod
9-
def get_request(cls):
10+
def fetch_input_url(cls):
1011
request_url = cls.default_url
1112
request_headers = cls.default_headers
1213
input_url = input('Enter URL: ')
@@ -37,28 +38,67 @@ def get_request(cls):
3738
request_url += endpoint
3839

3940
print("Trying ...\u26A1")
41+
return {
42+
"request_url" : request_url,
43+
"request_headers" : request_headers,
44+
}
4045

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()
4176
# Make GET request and store the response in response_data.json
4277
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)
4580
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)
5082

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)
5697

5798
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)
62102

63103
@classmethod
64104
def __check_endpoint(cls, request_url):

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.DELETE:
58+
self.api_test_object.delete_request()
5759
elif self.arguments.notion:
5860
NotionClient().get_tokenv2_cookie()
5961

0 commit comments

Comments
 (0)