Skip to content

Instantly share code, notes, and snippets.

@cinek810
Last active February 20, 2018 19:15
Show Gist options
  • Save cinek810/1437448adaf9fb2f7c95cba98bd2ff81 to your computer and use it in GitHub Desktop.
Save cinek810/1437448adaf9fb2f7c95cba98bd2ff81 to your computer and use it in GitHub Desktop.
def do_POST(self):
content_len=int(self.headers.getheader('content-length',0))
received=self.rfile.read(content_len)
received=json.loads(received)
if self.path=='/annotation':
self._set_headers()
try:
rjson=json.loads(received)
annoReply='[{"annotation:": '+rjson["annotation"]["name"]+', "time": "0", "title": "Snow title"}]'
self.wfile.write(annoReply)
except ValueError:
print("Received incorrect json")
elif self.path=="/search":
response=["SELECT incident_number,assigned_to_last_name FROM incidents where incident_state not in 6,7,8"]
self._set_headers()
self.wfile.write(json.dumps(response));
elif self.path=='/query':
incidents_description= { "1": "New" , "2": "Assigned", "12": "Referred", "4": "Await User", "5": "Await Evidance", "10": "Await Change", "8": "Await Vendor", "11": "Await Vendor Change", "6": "Resolved", "7": "Closed"}
self._set_headers()
now=calendar.timegm(time.gmtime())
queryReply=[{}]
if (now - self.lastQueryReply[0] > 60):
#Do request to service-now
snow = requests.Session()
snow.headers.update({"Accept": "application/json" })
snow.auth=self.snowAuth
snow.verify=False
r=snow.get(self.snowUrl+"//api/now/table/incident",params=self.snowFilter)incidents=r.json()
queryReply[0]["columns"]=[{"text": "Number", "type": "string"}, {"text": "Assigned to", "type": "string"}, { "text": "Incident state", "type": "string"}]
queryReply[0]["rows"]=[]
for incident in incidents["result"]:
try:
if(incident["assigned_to"]==""):
queryReply[0]["rows"].append([ incident["number"], "unassigned" , incidents_description[incident["incident_state"]]] )
else:
queryReply[0]["rows"].append([ incident["number"], self.knownUsers[incident["assigned_to"]["value"]] , incidents_description[incident["incident_state"]]])
except KeyError:
self.knownUsers[incident["assigned_to"]["value"]]=self._get_person_by_link(incident["assigned_to"]["link"])["last_name"]
queryReply[0]["rows"].append([ incident["number"], self.knownUsers[incident["assigned_to"]["value"]], incidents_description[incident["incident_state"]]])
queryReply[0]["type"]="table"
self.lastQueryReply[1]=queryReply
self.lastQueryReply[0]=now
else:
queryReply=self.lastQueryReply[1]
cacheAge=now - self.lastQueryReply[0]
self.wfile.write(json.dumps(queryReply))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment