-
Notifications
You must be signed in to change notification settings - Fork 0
/
invokes.py
39 lines (33 loc) · 1.27 KB
/
invokes.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
import requests
SUPPORTED_HTTP_METHODS = set([
"GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", "DELETE"
])
def invoke_http(url, method='GET', json=None, **kwargs):
"""A simple wrapper for requests methods.
url: the url of the http service;
method: the http method;
data: the JSON input when needed by the http method;
return: the JSON reply content from the http service if the call succeeds;
otherwise, return a JSON object with a "code" name-value pair.
"""
code = 200
result = {}
try:
if method.upper() in SUPPORTED_HTTP_METHODS:
r = requests.request(method, url, json = json, **kwargs)
else:
raise Exception("HTTP method {} unsupported.".format(method))
except Exception as e:
code = 500
result = {"code": code, "message": "invocation of service fails: " + url + ". " + str(e)}
if code not in range(200,300):
return result
## Check http call result
if r.status_code != requests.codes.ok:
code = r.status_code
try:
result = r.json() if len(r.content)>0 else ""
except Exception as e:
code = 500
result = {"code": code, "message": "Invalid JSON output from service: " + url + ". " + str(e)}
return result