-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
128 lines (117 loc) · 4.65 KB
/
api.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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import time
from pathlib import Path
import requests
from auth import get_auth
def get_resource_list(url, list_name=None, paginate=True):
"""
Returns a list of HC resources specified by the url basename (such as .../articles.json)
:param url: A full endpoint url, such as 'https://support.zendesk.com/api/v2/help_center/articles.json'
:param list_name: The list name in the response per the docs. Required if list name not the same as resource name
:param paginate: Whether the endpoint has pagination (i.e., a 'next_page' property). Example false: missing translations
:return: List of resources, or False if the request failed
"""
if list_name:
resource = list_name
else:
resource = Path(url).stem
record_list = {resource: []}
while url:
response = requests.get(url, auth=get_auth())
if response.status_code == 429:
print('Rate limited! Please wait.')
time.sleep(int(response.headers['retry-after']))
response = requests.get(url, auth=get_auth())
if response.status_code != 200:
print('Error with status code {}'.format(response.status_code))
print(response.text)
return False
data = response.json()
if data[resource]: # guard against empty record list
record_list[resource].extend(data[resource])
if paginate:
url = data['next_page']
else:
break
return record_list[resource]
def get_resource(url):
"""
Returns a single HC resource
:param url: A full endpoint url, such as 'https://support.zendesk.com/api/v2/help_center/articles/2342572.json'
:return: Dict of a resource, or False if the request failed.
"""
resource = None
response = requests.get(url, auth=get_auth())
if response.status_code == 429:
print('Rate limited! Please wait.')
time.sleep(int(response.headers['retry-after']))
response = requests.get(url, auth=get_auth())
if response.status_code != 200:
print('Failed to get record with error {}:'.format(response.status_code))
print(response.text)
return False
for k, v in response.json().items():
resource = v
if type(resource) is dict:
return resource
return None
def post_resource(url, data, status=201):
"""
:param url:
:param data:
:param status: HTTP status. Normally 201 but some POST requests return 200
:return: Python data, or False if the request failed.
"""
resource = None
headers = {'Content-Type': 'application/json'}
response = requests.post(url, json=data, auth=get_auth(), headers=headers)
if response.status_code == 429:
print('Rate limited! Please wait.')
time.sleep(int(response.headers['retry-after']))
response = requests.post(url, json=data, auth=get_auth(), headers=headers)
if response.status_code != status:
print('Failed to create record with error {}:'.format(response.status_code))
print(response.text)
return False
for k, v in response.json().items():
resource = v
if type(resource) is dict:
return resource
return None
def put_resource(url, data):
"""
:param url:
:param data:
:return: Python data, or False if the request failed.
"""
resource = None
headers = {'Content-Type': 'application/json'}
response = requests.put(url, json=data, auth=get_auth(), headers=headers)
if response.status_code == 429:
print('Rate limited! Please wait.')
time.sleep(int(response.headers['retry-after']))
response = requests.post(url, json=data, auth=get_auth(), headers=headers)
if response.status_code != 200:
print('Failed to update record with error {}:'.format(response.status_code))
print(response.text)
return False
for k, v in response.json().items():
resource = v
if type(resource) is dict:
return resource
return None
def delete_resource(url):
"""
Runs a DELETE request on any Delete endpoint in the Zendesk API
:param url: A full endpoint url, such as 'https://support.zendesk.com/api/v2/help_center/articles/2342572.json'
:return: If successful, a 204 status code. If not, None
"""
response = requests.delete(url, auth=get_auth())
if response.status_code == 429:
print('Rate limited! Please wait.')
time.sleep(int(response.headers['retry-after']))
response = requests.delete(url, auth=get_auth())
if response.status_code != 204:
print('Failed to delete record with error {}'.format(response.status_code))
print(response.text)
return False
return None