refactor requests

master
Marek Isalski 2 years ago
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)
if method == "GET": kwargs = dict(auth=auth, verify=True, timeout=3.0)
if params: if params:
r = requests.get(url, auth=auth, params=params, verify=True, timeout=3.0) kwargs.update(params=params)
else:
r = requests.get(url, auth=auth, verify=True, timeout=3.0) if method == "GET":
fn = requests.get
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…
Cancel
Save