def _do_collect(self, table, params, limit): rest_uri = self._get_uri(table, params, limit) http = rest.build_http_connection(self.config, self.config["duration"]) _LOGGER.info("start %s", rest_uri) response, content = None, None try: credentials = base64.b64encode( "%s:%s" % (self.config["username"], self.config["password"])) response, content = http.request(rest_uri, headers={ "Accept-Encoding": "gzip", "Accept": "application/json", "Authorization": "Basic %s" % credentials }) if response.status not in (200, 201): _LOGGER.error("Failed to connect %s, reason=%s", rest_uri, response.reason) except Exception: _LOGGER.error("Failed to connect %s, reason=%s", rest_uri, traceback.format_exc()) _LOGGER.info("end %s", rest_uri) return response, content
def _handle_event(self, event): event_data = self._prepare_data(event) if not event_data: self.logger.info("No event data is available") return event_data = json.dumps(event_data) headers = { "Content-type": "application/json", "Accept": "application/json", } endpoint = self._get_endpoint() endpoint = "{0}{1}".format(self.snow_account["url"], endpoint) http = rest.build_http_connection(self.snow_account) self.logger.debug("Sending request to %s: %s", endpoint, event_data) for _ in xrange(3): ok, result = self._do_event(http, endpoint, event_data, headers) if ok: return result return None
def _retrieve_incident(self, snow_account, correlation_id): http_connection = rest.build_http_connection(snow_account) url = self._get_incident_url(snow_account["url"], correlation_id) try: resp, content = http_connection.request(url) _LOGGER.info("Got response content %s from %s" % (content, url)) response_as_json = json.loads(content) except Exception: msg = ("Failed to get incident for correlation id '%s', " "reason=%s") % (correlation_id, traceback.format_exc()) _LOGGER.error(msg) raise Exception(msg) else: if resp.status not in (200, 201): msg = ("Failed to get incident for correlation id '%s', " "code=%s, reason=%s") % (correlation_id, resp.status, resp.reason) _LOGGER.error(msg) raise Exception(msg) return response_as_json, url
def verify_user_pass(defaults): _LOGGER.info("Verify username and password.") url = defaults["url"] if not url.startswith("https://"): url = "https://{}".format(url) uri = ("{}/incident.do?JSONv2&sysparm_query=" "sys_updated_on>=2000-01-01+00:00:00&sysparm_record_count=1") url = uri.format(url) http = rest.build_http_connection(defaults) try: resp, content = http.request(url) except Exception: msg = ("Failed to verify ServiceNow username and password, " "reason={}".format(traceback.format_exc())) _LOGGER.error(msg) raise Exception(msg) else: if resp.status not in (200, 201): msg = ("Failed to verify ServcieNow username and password, " "code={}, reason={}").format(resp.status, resp.reason) _LOGGER.error(msg) raise Exception(msg)