Skip to content

Commit bf632a3

Browse files
author
yasht01
committed
Add color formatting to output and option to save output
1 parent 27f23eb commit bf632a3

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/arguments/api_test.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import requests, json
2+
from pygments import highlight, lexers, formatters
23

34
class ApiTesting():
45
default_url = "127.0.0.1:8000"
@@ -28,10 +29,17 @@ def get_request(cls):
2829
try:
2930
response = requests.get(request_url)
3031
print(f"Reponse Status Code: {response.status_code}")
31-
print("Response data stored in response_data.json")
32-
parsed = json.loads(response.content)
33-
with open('response_data.json', 'w') as jsonFile:
34-
json.dump(parsed, jsonFile, indent=4)
32+
response_data = json.loads(response.content)
33+
parsed_json = json.dumps(response_data, indent=4)
34+
output_json = highlight(parsed_json, lexers.JsonLexer(), formatters.TerminalFormatter())
35+
print(output_json)
36+
37+
store_data = input('Store response data? (Y/N): ')
38+
if(store_data == 'Y' or store_data == 'y'):
39+
with open('response_data.json', 'w') as jsonFile:
40+
json.dump(response_data, jsonFile, indent=4)
41+
print("Response data stored in response_data.json")
42+
3543
except requests.exceptions.InvalidSchema:
3644
print("Check whether the URL is valid or check if the localhost server is active or not")
3745
except Exception as e:

0 commit comments

Comments
 (0)