-
Notifications
You must be signed in to change notification settings - Fork 1
/
general_call.py
45 lines (40 loc) · 1.59 KB
/
general_call.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
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: utf-8 -*-
import json
import requests
from requests.auth import HTTPBasicAuth
import constants
def call_hdh_api(api_uri):
"""
send api request
:type api_uri: string
"""
response = requests.get(api_uri, headers=constants.HEADERS,
auth=HTTPBasicAuth(constants.HDH_USER_NAME, constants.PASSWORD))
if response.status_code == 200:
print('success to reach the api')
# print(type(response.content.decode('utf-8'))) # json string
return json.loads(response.content.decode('utf-8')) # dict
# return response.content.decode('utf-8')
elif response.status_code >= 500:
print('[!] [{0}] Internal Server Error'.format(response.status_code))
return None
elif response.status_code == 404:
print('[!] [{0}] URL not found: [{1}]'.format(response.status_code, api_uri))
return None
elif response.status_code == 401:
print('[!] [{0}] Authentication Failed'.format(response.status_code))
return None
elif response.status_code >= 400:
print('[!] [{0}] Bad Request'.format(response.status_code))
print(response.content )
return None
elif response.status_code >= 300:
print('[!] [{0}] Unexpected redirect.'.format(response.status_code))
return None
else:
print('[?] Unexpected Error: [HTTP {0}]: Content: {1}'.format(response.status_code, response.content))
return None
if __name__ == '__main__':
uri = '{0}/patients'.format(constants.API_URL_BASE)
response = call_hdh_api(uri)
print(response.status_code)