-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmany_rest.py
More file actions
58 lines (47 loc) · 1.58 KB
/
many_rest.py
File metadata and controls
58 lines (47 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import requests
import logging
import sys
import json
import argparse
RESPONSE_CODE_LOG = 'response_code.log'
RESPONSE_CODE_TXT = 'response_code.txt'
DEFAULT_RETRIES = 5
logging.basicConfig(
level=logging.INFO,
handlers=[
logging.StreamHandler(),
logging.FileHandler(RESPONSE_CODE_LOG)
])
def get_api(path: str) -> None:
try:
r = requests.get(path)
if r.status_code == 200:
write_to_file(json.dumps(r.json()))
else:
logging.error(f'Received non-200 status code: {r.status_code} for URL: {path}')
except requests.exceptions.RequestException as e:
logging.error(f'Problem with http: {e}')
def write_to_file(json_data: str, file: str=RESPONSE_CODE_TXT, ) -> None:
try:
with open(file, "a") as f:
logging.info(f'Logging following reply: {json_data}')
f.write(f'{json_data}\n')
except OSError as e:
logging.error(f'Operating system error: {e}')
def many_requests(amount: int=DEFAULT_RETRIES) -> None:
for i in range(amount):
try:
get_api(sys.argv[1])
except Exception as e:
logging.error(f'Generic exception: {e}')
def main():
parser = argparse.ArgumentParser(description="Fetch data from an API endpoint.")
parser.add_argument("url", help="The URL of the API endpoint.")
parser.add_argument(
"retries", nargs="?", type=int, default=DEFAULT_RETRIES,
help="Number of retries (default: 5)."
)
args = parser.parse_args()
many_requests(args.retries)
if __name__ == "__main__":
main()