Skip to content

Commit 01751b2

Browse files
Merge pull request #80 from yasht01/get-request
Implement GET in dynamic to get the response from an API #80
2 parents ab2e256 + e20092f commit 01751b2

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ dmypy.json
2828

2929
# Cython debug symbols
3030
cython_debug/
31+
32+
# User files related to API testing and authentication
33+
access_token.json
34+
response_data.json

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Options: <br>
4646
`-d, --debug -> Turn on Debugging mode` <br>
4747
`-c, --custom -> Setup a custom API key` <br>
4848
`-h, --help -> Shows this message and exit` <br>
49+
`-GET -> Make a GET request to an API` <br>
4950

5051
## License🗄
5152

main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
help="Check updates for the application",
4747
action="store_true")
4848

49+
parser.add_argument("-GET",
50+
help="Make a GET request to an API",
51+
action='store_true')
52+
4953
ARGV = parser.parse_args()
5054

5155
search_flag = Search(ARGV)

src/arguments/api_test.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import requests, json
2+
from pygments import highlight, lexers, formatters
3+
4+
class ApiTesting():
5+
default_url = "https://127.0.0.1:8000"
6+
7+
# Make GET request
8+
@classmethod
9+
def get_request(cls):
10+
request_url = cls.default_url
11+
input_url = input('Enter URL: ')
12+
if input_url != '':
13+
request_url = input_url
14+
15+
# Check whether the request_url has an endpoint or not
16+
has_endpoint = cls.__check_endpoint(request_url)
17+
18+
# Check if http:// or https:// is present in request_url
19+
has_protocol = cls.__check_protocol(request_url)
20+
21+
if not(has_protocol):
22+
request_url = "https://" + request_url
23+
24+
# Ask the user for endpoint if not present in request_url
25+
if not(has_endpoint):
26+
if(request_url[-1] == '/'):
27+
endpoint = input("Input endpoint (Without the starting slash): ")
28+
else:
29+
endpoint = input("Input endpoint (With the starting slash): ")
30+
request_url += endpoint
31+
32+
print("Trying ...\u26A1")
33+
34+
# Make GET request and store the response in response_data.json
35+
try:
36+
response = requests.get(request_url)
37+
print(f"Reponse Status Code: {response.status_code}")
38+
response_data = json.loads(response.content)
39+
parsed_json = json.dumps(response_data, indent=4)
40+
output_json = highlight(parsed_json, lexers.JsonLexer(), formatters.TerminalFormatter())
41+
print(output_json)
42+
43+
store_data = input('Store response data? (Y/N): ')
44+
if(store_data == 'Y' or store_data == 'y'):
45+
with open('response_data.json', 'w') as jsonFile:
46+
json.dump(response_data, jsonFile, indent=4)
47+
print("Response data stored in response_data.json")
48+
49+
except requests.exceptions.InvalidSchema:
50+
print("Check whether the URL is valid or check if the localhost server is active or not")
51+
except Exception as e:
52+
print(e)
53+
54+
@classmethod
55+
def __check_endpoint(cls, request_url):
56+
if(request_url == cls.default_url):
57+
return False
58+
else:
59+
return True
60+
61+
@classmethod
62+
def __check_protocol(cls, request_url):
63+
if(request_url[:4] == 'http'):
64+
return True
65+
else:
66+
return False

src/arguments/search.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from .utility import Utility
99
from .save import SaveSearchResults
1010
from .update import UpdateApplication
11+
from .api_test import ApiTesting
1112

1213
version = "0.1.0"
1314
class Prompt():
@@ -28,6 +29,7 @@ class Search():
2829
def __init__(self, arguments):
2930
self.arguments = arguments
3031
self.utility_object = Utility()
32+
self.api_test_object = ApiTesting()
3133

3234
def search_args(self):
3335
if self.arguments.search:
@@ -45,6 +47,8 @@ def search_args(self):
4547
elif self.arguments.update:
4648
update = UpdateApplication(version)
4749
update.check_for_updates()
50+
elif self.arguments.GET:
51+
self.api_test_object.get_request()
4852

4953
def search_for_results(self, save=False):
5054
queries = ["What do you want to search", "Tags"]
@@ -83,4 +87,4 @@ def search_for_results(self, save=False):
8387
filename = SaveSearchResults(data)
8488
print(
8589
colored(f"\U0001F604 Answers successfully saved into {filename}",
86-
"green"))
90+
"green"))

0 commit comments

Comments
 (0)