This repository has been archived by the owner on Jan 2, 2018. It is now read-only.
/
Desk.py
260 lines (199 loc) · 9.02 KB
/
Desk.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import requests
from requests_oauthlib import OAuth1Session
import Config
from Logging import Logging
from Marshal import Marshal
class Desk(object):
def __init__(self, sitename):
self.url = 'https://%s.desk.com/api/v2/' % sitename
def get_cases(self, case_filter=''):
"""
Get all cases that match the supplied filter. If no filter is supplied, all cases will be fetched.
:param case_filter: Either Customer ID, Company ID, or Filter ID
:type case_filter: str
:return: All Cases that match the filter, If the request was completed successfully.
:rtype: str, bool
"""
results, ok = self.get(self.url, 'cases' + case_filter)
all_cases = results['_embedded']['entries']
Logging.get_logger(__name__).debug('Get Cases returned %s' % str(results))
while results['_links']['next'] is not None:
results, ok = self.get(self.url, results['_links']['next']['href'].split('/')[-1])
Logging.get_logger(__name__).debug('Get Cases returned %s' % str(results))
all_cases.append(results['_embedded']['entries'])
Logging.get_logger(__name__).info('Fetched a total of %d cases.' % len(all_cases))
marshaled_results = Marshal.desk_case(all_cases)
for case in marshaled_results:
case.customer = self.get_customer(case.customer_id)[0]
return marshaled_results, ok
def get_customer(self, customer_id):
"""
Get the Customer Information for a specified id
:param customer_id:
:type customer_id: int
:return: Customer Information, If the request was completed successfully.
:rtype: str, bool
"""
results, ok = self.get(self.url, 'customers/' + str(customer_id))
return Marshal.desk_customer(results), ok
def get_filters(self):
"""
Get all the filters in Desk.com
:return: All Filters in Desk, If the request was completed successfully.
:rtype: str, bool
"""
results, ok = self.get(self.url, 'filters')
Logging.get_logger(__name__).debug('Get Filters returned %s' % str(results))
return results, ok
def get_groups(self):
"""
Get all the Groups in Desk.com
:return: All Groups in Desk.com, If the request was successful
:rtype: str, bool
"""
results, ok = self.get(self.url, 'groups')
if ok:
results = results['_embedded']['entries']
Logging.get_logger(__name__).debug('Get Groups returned %s' % str(results))
return results, ok
def get_cases_by_group(self, group_name):
"""
Get all cases assigned to a certain group.
Requires that a filter be setup that has all the cases for the desired group.
:param group_name:
:type group_name: str
:return: All Cases belonging to the Group specified in the Parameters
:rtype: str, bool
"""
groups, ok = self.get_groups()
assert ok
group_id = 0
for group in groups:
if group['name'] == group_name:
group_id = group['id']
break
filters, ok = self.get_filters()
assert ok
filter_id = 0
for f in filters['_embedded']['entries']:
# noinspection PyBroadException
try:
filter_group = int(f['_links']['group']['href'].split('/')[-1])
if filter_group == group_id:
filter_id = f['id']
break
except:
pass
results, ok = self.get_cases(case_filter='?filter_id=%i' % filter_id)
return results, ok
def add_note_to_case(self, case_id, note):
"""
Add a note to the Supplied Case
:param case_id: Case to add note to
:type case_id: int
:param note: Note to be added to Case
:type note: str
:return: The Updated Case, If the request was made successfully.
:rtype: str, bool
"""
Logging.get_logger(__name__).info("Adding Note to Case with ID: %d" % case_id)
result, ok = self.post(self.url, 'cases/' + str(case_id) + '/notes',
{"body": note})
return result, ok
def resolve_case(self, case_id):
"""
Mark a Case as Resolved
:param case_id: Case to Resolve
:type case_id: int
:return: The Updated Case, If the request was made successfully.
:rtype: str, bool
"""
Logging.get_logger(__name__).info("Resolving Case with ID: %d" % case_id)
result, ok = self.patch(self.url, 'cases/' + str(case_id), {"status": "resolved"})
return result, ok
@staticmethod
def get(url, endpoint):
"""
Make a GET request to the desk.com API using the pre-configured authentication method
:param url: URL to make the GET Request to
:type url: str
:param endpoint: Path of the URL to make the request to
:type endpoint: str
:return: Returned JSON
:rtype: str
"""
url_endpoint = url + endpoint
Logging.get_logger(__name__).info('Making GET Request to ' + url_endpoint)
desk_config = Config.load_params()['Desk.com']
if desk_config['auth_method'] == 'oauth':
oauth_session = OAuth1Session(desk_config['client_key'],
client_secret=desk_config['client_secret'],
resource_owner_key=desk_config['resource_owner_key'],
resource_owner_secret=desk_config['resource_owner_secret'])
request = oauth_session.get(url_endpoint)
else:
request = requests.get(url_endpoint, auth=(desk_config['username'], desk_config['password']))
try:
return request.json(), request.status_code == 200
except Exception, e:
Logging.get_logger(__name__).error('Problem Getting JSON from GET Request - %s' % e.message)
return []
@staticmethod
def post(url, endpoint, payload):
"""
Make a POST request to the desired URL with the supplied Payload
:param url: URL to make the POST Request to
:type url: str
:param endpoint: Path of the URL to make the request to
:type endpoint: str
:param payload: POST Payload
:type payload: dict
:return: Result JSON, If the request was made successfully
:rtype: str, bool
"""
url_endpoint = url + endpoint
Logging.get_logger(__name__).info('Making POST Request to ' + url_endpoint)
desk_config = Config.load_params()['Desk.com']
if desk_config['auth_method'] == 'oauth':
oauth_session = OAuth1Session(desk_config['client_key'],
client_secret=desk_config['client_secret'],
resource_owner_key=desk_config['resource_owner_key'],
resource_owner_secret=desk_config['resource_owner_secret'])
request = oauth_session.post(url_endpoint, payload)
else:
request = requests.post(url_endpoint, json=payload, auth=(desk_config['username'], desk_config['password']))
try:
return request.json(), request.status_code == 200
except Exception, e:
Logging.get_logger(__name__).error('Problem Getting JSON from POST Request - %s' % e.message)
return []
@staticmethod
def patch(url, endpoint, payload):
"""
Make a PATCH request to the desired URL with the supplied Payload
:param url: URL to make the PATCH Request to
:type url: str
:param endpoint: Path of the URL to make the request to
:type endpoint: str
:param payload: PATCH Payload
:type payload: dict
:return: Result JSON, If the request was made successfully
:rtype: str, bool
"""
url_endpoint = url + endpoint
Logging.get_logger(__name__).info('Making PATCH Request to ' + url_endpoint)
desk_config = Config.load_params()['Desk.com']
if desk_config['auth_method'] == 'oauth':
oauth_session = OAuth1Session(desk_config['client_key'],
client_secret=desk_config['client_secret'],
resource_owner_key=desk_config['resource_owner_key'],
resource_owner_secret=desk_config['resource_owner_secret'])
request = oauth_session.patch(url_endpoint, payload)
else:
request = requests.patch(url_endpoint, json=payload,
auth=(desk_config['username'], desk_config['password']))
try:
return request.json(), request.status_code == 200
except Exception, e:
Logging.get_logger(__name__).error('Problem Getting JSON from PATCH Request - %s' % e.message)
return []