+ resp = self.call_req(callbackUri, "", "", "POST", json.dumps(notification))
+ if resp[0] != 0:
+ logger.error('Status code is %s, detail is %s.', resp[2], resp[1])
+
+ def call_req(self, full_url, user, passwd, method, content=''):
+ callid = str(uuid.uuid1())
+ logger.debug("[%s]call_req('%s','%s','%s',%s,'%s','%s')" % (
+ callid, full_url, user, passwd, rest_no_auth, method, content))
+ ret = None
+ resp_Location = ''
+ resp_status = ''
+ try:
+ headers = {'content-type': 'application/json', 'accept': 'application/json'}
+ if user:
+ headers['Authorization'] = 'Basic %s' % base64.b64encode(
+ bytes('%s:%s' % (user, passwd), "utf-8")).decode()
+ ca_certs = None
+ for retry_times in range(3):
+ http = httplib2.Http(ca_certs=ca_certs, disable_ssl_certificate_validation=True)
+ http.follow_all_redirects = True
+ try:
+ resp, resp_content = http.request(full_url, method=method.upper(), body=content, headers=headers)
+ logger.info("resp=%s,resp_content=%s" % (resp, resp_content))
+ resp_status, resp_body = resp['status'], resp_content.decode('UTF-8')
+ resp_Location = resp.get('Location', "")
+ logger.debug("[%s][%d]status=%s,resp_body=%s)" % (callid, retry_times, resp_status, resp_body))
+ if resp_status in status_ok_list:
+ ret = [0, resp_body, resp_status, resp_Location]
+ else:
+ ret = [1, resp_body, resp_status, resp_Location]
+ break
+ except Exception as ex:
+ if 'httplib.ResponseNotReady' in str(sys.exc_info()):
+ logger.debug("retry_times=%d", retry_times)
+ logger.error(traceback.format_exc())
+ ret = [1, "Unable to connect to %s" % full_url, resp_status, resp_Location]
+ continue
+ raise ex
+ except urllib.error.URLError as err:
+ ret = [2, str(err), resp_status, resp_Location]
+ except Exception as ex:
+ logger.error(traceback.format_exc())
+ logger.error("[%s]ret=%s" % (callid, str(sys.exc_info())))
+ res_info = str(sys.exc_info())
+ if 'httplib.ResponseNotReady' in res_info:
+ res_info = "The URL[%s] request failed or is not responding." % full_url
+ ret = [3, res_info, resp_status, resp_Location]
+ except:
+ logger.error(traceback.format_exc())
+ ret = [4, str(sys.exc_info()), resp_status, resp_Location]
+
+ logger.debug("[%s]ret=%s" % (callid, str(ret)))
+ return ret