-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest_Classes3.py
More file actions
82 lines (65 loc) · 2.41 KB
/
Copy pathRequest_Classes3.py
File metadata and controls
82 lines (65 loc) · 2.41 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import requests
import json
import time
class call:
def __init__(self, url, token, package, headers=None, counter=5):
self.url = url
self.token = token
self.headers = {"Authorization": str("Bearer " + self.token), 'Content-Type': 'application/json'}
self.counter = counter
self.package = package
self.params = "none"
def update_attr(self, item, newValue):
setattr(self, item, newValue)
def get_item(self, item):
value = getattr(self, item)
return value
def execute_call(self, counter):
1 == 1
def error_catcher(self, output):
if output["errorCode"] == "4003" or output["message"] == "Rate limit exceeded":
self.rate_limit_backoff(output)
print("Found Error")
print(output)
def rate_limit_backoff(self, output):
counter = self.get_item("counter")
counter -= 1
time.sleep(60)
print("rate limit backoff initiated")
self.execute_call(counter)
class get_call(call):
def __init__(self, url, token, params, counter=5):
self.url = url
self.token = token
self.headers = {"Authorization": str("Bearer " + self.token), 'Content-Type': 'application/json'}
self.params = params
counter = counter
def execute_call(self, counter):
URL = self.get_item("url")
headers = self.get_item("headers")
params = self.get_item("params")
output = requests.get(URL, headers=headers, params=params)
output = output.json()
if "errorCode" in output:
self.error_catcher(output)
return output
class put_call(call):
def execute_call(self, counter):
URL = self.get_item("url")
headers = self.get_item("headers")
package = self.get_item("package")
output = requests.put(URL, headers=headers, data=json.dumps(package))
output = output.json()
if "errorCode" in output or "errorCode" in output:
self.error_catcher(output)
return output
class post_call(call):
def execute_call(self, counter):
URL = self.get_item("url")
headers = self.get_item("headers")
package = self.get_item("package")
output = requests.post(URL, headers=headers, data=json.dumps(package))
output = output.json()
if "errorCode" in output:
self.error_catcher(output)
return output