refactor requests
This commit is contained in:
parent
e119514852
commit
5526cc8b16
@ -52,33 +52,24 @@ class API(object):
|
|||||||
url = self.base + uri
|
url = self.base + uri
|
||||||
auth = requests.auth.HTTPBasicAuth(self.username, self.password)
|
auth = requests.auth.HTTPBasicAuth(self.username, self.password)
|
||||||
|
|
||||||
|
kwargs = dict(auth=auth, verify=True, timeout=3.0)
|
||||||
|
if params:
|
||||||
|
kwargs.update(params=params)
|
||||||
|
|
||||||
if method == "GET":
|
if method == "GET":
|
||||||
if params:
|
fn = requests.get
|
||||||
r = requests.get(url, auth=auth, params=params, verify=True, timeout=3.0)
|
|
||||||
else:
|
|
||||||
r = requests.get(url, auth=auth, verify=True, timeout=3.0)
|
|
||||||
|
|
||||||
elif method == "POST":
|
elif method == "POST":
|
||||||
|
fn = requests.post
|
||||||
headers = {"content-type": "application/json"}
|
headers = {"content-type": "application/json"}
|
||||||
if params:
|
kwargs.update(headers=headers,data=json.dumps(data))
|
||||||
r = requests.post(
|
|
||||||
url,
|
elif method == "PUT":
|
||||||
auth=auth,
|
fn = requests.post
|
||||||
headers=headers,
|
headers = {"content-type": "application/json"}
|
||||||
data=json.dumps(data),
|
kwargs.update(headers=headers,data=json.dumps(data))
|
||||||
params=params,
|
|
||||||
verify=True,
|
r = fn(url, **kwargs)
|
||||||
timeout=3.000,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
r = requests.post(
|
|
||||||
url,
|
|
||||||
auth=auth,
|
|
||||||
headers=headers,
|
|
||||||
data=json.dumps(data),
|
|
||||||
verify=True,
|
|
||||||
timeout=3.000,
|
|
||||||
)
|
|
||||||
|
|
||||||
if (r.status_code == 200) or (r.status_code == 201):
|
if (r.status_code == 200) or (r.status_code == 201):
|
||||||
try:
|
try:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user