-
Notifications
You must be signed in to change notification settings - Fork 0
/
servicenowRestAPI.py
346 lines (243 loc) · 11.7 KB
/
servicenowRestAPI.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import json
import requests
import datetime
import re
import os
import mimetypes
import logging
import random
from logging import error
from datetime import datetime
def getMeetingCasestoProcess(url_servicenow,user,pwd):
# Get list of cases to process
url = url_servicenow + '/api/now/table/sn_customerservice_case?' + 'sysparm_query=state=1^category%3D5%5Esubcategory%3D800%5Edue_dateRELATIVEGE%40dayofweek%40ago%405&sysparm_fields=account%2Caccount.name%2Ccase%2Csys_id'
#active=true^category=5^subcategory=800^state=1
headers = {"Content-Type":"application/json","Accept":"application/json"}
# Do the HTTP request
response = requests.get(url, auth=(user, pwd), headers=headers )
print response
# Check for HTTP codes other than 200
if response.status_code != 200:
print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
exit()
# Decode the JSON response into a dictionary and use the data
attachmentdata = json.loads(response.text)
caselist = attachmentdata["result"]
return caselist
def updateCaseStatus(url_servicenow,record_sys_id,status,work_notes,user,pwd):
serviceNOWtable = 'sn_customerservice_case'
# Set the request parameters
url = url_servicenow + '/api/now/table/' + serviceNOWtable + '/' + record_sys_id + '?sysparm_input_display_value=true'
# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}
datavalues = "{\"state\":\"STATUS\",\"work_notes\":\"WORKNOTE\"}"
datavalues = re.sub('STATUS',status,datavalues)
datavalues = re.sub('WORKNOTE',work_notes,datavalues)
try:
response = requests.put(url, auth=(user, pwd), headers=headers ,data=datavalues)
# Check for HTTP codes other than 200
if response.status_code != 200:
logging.error('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
return response.status_code
except requests.exceptions.RequestException as e:
logging.error(e)
raise
finally:
data = response.json()
print 'Update Status: ' + str(data)
return data
# Decode the JSON response into a dictionary and use the data
return 'true'
# used to limit the specific attacchment
def getSpecificAttachment(url_servicenow, user,pwd,file_limiter,account_sys_id):
print 'in get specific'
fileList_url = url_servicenow + '/api/now/attachment?table_sys_id=' + account_sys_id
headers = {"Content-Type":"application/json","Accept":"application/json"}
# Do the HTTP request
response = requests.get(fileList_url, auth=(user, pwd), headers=headers )
print response
# Check for HTTP codes other than 200
if response.status_code != 200:
print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
exit()
# Decode the JSON response into a dictionary and use the data
attachmentdata = json.loads(response.text)
attachmentdata_list = attachmentdata["result"]
## check to see how many match the file name and if ther is a more then one then error
print attachmentdata_list
# Error check to see if there is more then one template?
filecounter = substring_indexes(file_limiter ,str(attachmentdata_list))
logging.info('On the Account there are: ' + str(filecounter) + ' files. There MUST be only one file')
if filecounter != 1:
if filecounter == 0:
errorMessage = 'ERROR: There is no templete attached to the Account to process'
logging.error(errorMessage)
return errorMessage
if filecounter > 1:
errorMessage = 'ERROR: There is more then one Template on the Account and therefore dont know how to process'
logging.error(errorMessage)
return errorMessage
## Find the write list and process here should only be one. otherwwise we need to re-write this area.
# hard coded digits to find the write file name
for item in attachmentdata_list:
attachment_sys_id = item.get("sys_id")
file_name = item.get("file_name")
table_sys_id = item.get("table_sys_id")
print file_name + ' ' + file_name[-13:]
if file_name[-14:] == file_limiter:
return attachment_sys_id
def substring_indexes(substring, string):
last_found = -1 # Begin at -1 so the next position to search from is 0
counter = 0
while True:
# Find next index of substring, by starting after its last known position
last_found = string.find(substring, last_found+1)
if last_found != -1:
counter = counter+1
if last_found == -1:
return counter
def getParentAccount(url_servicenow,account_sys_id,user,pwd):
url = url_servicenow + '/api/now/table/customer_account?sysparm_query=parent%3D' + account_sys_id # + '&sysparm_limit=1'
headers = {"Content-Type":"application/json","Accept":"application/json"}
# Do the HTTP request
try:
response = requests.get(url, auth=(user, pwd), headers=headers )
if response.status_code != 200:
logging.error('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
exit()
# Decode the JSON response into a dictionary and use the data
data = json.loads(response.text)
accountList = data["result"]
except requests.exceptions.RequestException as e:
logging.error(e)
raise
finally:
return accountList
def getCaseswithTasks(url_servicenow,case_sys_id,user,pwd):
# Get list of cases to process
url = url_servicenow + '/api/now/table/sn_customerservice_task?sysparm_display_value=all&sysparm_query=u_case='+ case_sys_id
headers = {"Content-Type":"application/json","Accept":"application/json"}
# Do the HTTP request
try:
response = requests.get(url, auth=(user, pwd), headers=headers )
if response.status_code != 200:
logging.error('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
exit()
# Decode the JSON response into a dictionary and use the data
data = json.loads(response.text)
caseandtasklist = data["result"]
except requests.exceptions.RequestException as e:
logging.error(e)
raise
finally:
return caseandtasklist
# download a specific attachment and add time stamp
def download_attachment(url_servicenow,account_sys_id,user,pwd,filedirectory,localfile,attachment_sys_id):
# file_url = 'https://redbrickhealthdev.service-now.com/api/now/attachment/c8b78f256f255300a1af77f16a3ee46e/file'
file_url = url_servicenow + '/api/now/attachment/' + attachment_sys_id + '/file'
varBinary_headers = {"Contxlsxent-Type":"application/xml","Accept":"application/xml"}
try:
varBinary = requests.get(file_url, auth=(user, pwd), headers=varBinary_headers,stream=True)
if response.status_code != 200:
logging.error('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
exit()
except requests.exceptions.RequestException as e:
logging.error(e)
raise
finally:
json_obj = varBinary.headers['x-attachment-metadata']
json_objMetadata = json.loads(json_obj)
for k,v in json_objMetadata.items():
if k == 'file_name':
file_name = v
#build file dir
file_name = re.sub('.xlsx', '',file_name)
vardatetime = str(datetime.now().strftime('%Y-%m-%d-%I%M%S')) +'_' + str(random.randint(1,1001))
file_name = file_name + '_' + vardatetime + '.xlsx'
# with open('/Users/ttjornhom/IdeaProjects/snCSMExcelDoc/template_tracker99.xlsx', 'w+') as f:
with open(file_name, 'w+') as f:
for chunk in varBinary:
f.write(chunk)
if os.path.isfile(file_name) == 'true':
logging.info('validated File: ' + file_name)
return file_name
def getcasesTOupdateExcel(url_servicenow,user,pwd,account_sys_id):
caselist = {}
url = url_servicenow + '/api/now/table/sn_customerservice_case?sysparm_display_value=true&sysparm_query=categoryIN2%2C5%2C3%2C6%2C4%2C10%5Eaccount%3D' + account_sys_id
headers = {"Content-Type":"application/json","Accept":"application/json"}
# Do the HTTP request
try:
response = requests.get(url, auth=(user, pwd), headers=headers )
if response.status_code != 200:
logging.error('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
exit()
# Decode the JSON response into a dictionary and use the data
data = json.loads(response.text)
caselist = data["result"]
except requests.exceptions.RequestException as e:
logging.error(e)
raise
finally:
return caselist
def postfiletoServiceNOW(url_servicenow,file_name,user,pwd,table_name,table_sys_id):
#TODO find current work on uploading a file
print 'In POST FILE'
content_type,fileEncoding = mimetypes.guess_type(file_name)
print 'In POST FILE:content_type:' + str(content_type)
headers = {
'Accept': 'application/json',
'Content-Type': content_type,
}
# specify files to send as binary
data = open(file_name, 'rb').read()
file_url = url_servicenow + '/api/now/attachment/file?table_name=' + table_name + '&table_sys_id=' + table_sys_id+ '&file_name=' + file_name
print file_url
response = requests.post(file_url, auth=(user,pwd), headers=headers, data=data)
print response
# this rest api indicates good as 201 and not 200
if response.status_code != 201:
print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
exit()
attachmentdata = json.loads(response.text)
#TODO fixLOOP
for k,v in attachmentdata.items():
if k == 'file_name':
file_name = v
return file_name
def getAccountinfo(url_servicenow,account_sys_id,user,pwd):
# Set the request parameters
acct_url = url_servicenow + '/api/now/table/customer_account?sysparm_limit=1&sys_id='+ account_sys_id
# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}
# Do the HTTP request
try:
response = requests.get(url, auth=(user, pwd), headers=headers )
if response.status_code != 200:
logging.error('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
exit()
# Decode the JSON response into a dictionary and use the data
data = json.loads(response.text)
returnlist = data["result"]
except requests.exceptions.RequestException as e:
logging.error(e)
raise
finally:
return returnlist
def getgeneralTableinfo(url_servicenow,table,sysparms,user,pwd):
# Set the request parameters
url = url_servicenow + '/api/now/table/'+table + sysparms
# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}
try:
response = requests.get(url, auth=(user, pwd), headers=headers )
if response.status_code != 200:
logging.error('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
exit()
# Decode the JSON response into a dictionary and use the data
data = json.loads(response.text)
returnlist = data["result"]
except requests.exceptions.RequestException as e:
logging.error(e)
raise
finally:
return returnlist