-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequests_get_json.py
34 lines (27 loc) · 932 Bytes
/
requests_get_json.py
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
import requests
from requests.exceptions import HTTPError, Timeout
REQUEST_TIMEOUT = 300
def main(url: str, payload: dict):
with requests.Session() as session:
try:
response = session.get(url, params=payload, timeout=REQUEST_TIMEOUT)
response.raise_for_status()
except Timeout as err:
print(f'The request timed out: {err}')
return False
except HTTPError as err:
print(f'HTTP error occurred: {err}')
return False
except Exception as err:
print(f'Other error occurred: {err}')
return False
if response.status_code == requests.codes.ok:
result = response.json()
print(result)
return True
else:
return False
if __name__ == "__main__":
test_url = 'https://api.github.com'
data = {'q': 'requests+language:python'}
main(url=test_url, payload=data)