コード例 #1
0
    def provider_invoke(self,halo_context, method, url, api_type, timeout, data=None, headers=None, auth=None):

        try:
            service_name = self.name
            logger.debug("send invoke to target_service:" + service_name, extra=log_json(halo_context))
            if data:
                x = str(data.decode('utf8').replace("'", '"'))
                if x != "":
                    #@todo fix this block
                    datax = json.loads(x)
                    datay = json.dump(datax)
                else:
                    datay = ""
            else:
                datay = ""
            messageDict = {"method":method,"url":url,"view":datay,"headers":headers,"auth":auth}
            print("messageDict="+str(messageDict)+" version:"+self.version)
            ret = get_provider().invoke_sync(halo_context,messageDict,service_name,version=self.version)
            print("ret:"+str(ret))
            content = json.loads(ret['Payload'].read())
            print("content:" + str(content))
            provider_response = ProviderResponse(content["body"],ret['ResponseMetadata']["HTTPHeaders"],ret['StatusCode'])
            return provider_response
        except ProviderException as e:
            logger.error("Unexpected Provider Error", extra=log_json(halo_context, messageDict, e))
            raise e
コード例 #2
0
 def handle(self,halo_request,e:Exception,traceback):
     # @todo check if stack needed and working
     e.stack = traceback.format_exc()
     logger.error(e.__str__(), extra=log_json(halo_request.context, {}, e))
     # exc_type, exc_obj, exc_tb = sys.exc_info()
     # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
     # logger.debug('An Exception occured in '+str(fname)+' lineno: '+str(exc_tb.tb_lineno)+' exc_type '+str(exc_type)+' '+e.message)
     return e
コード例 #3
0
    def exec_client(self, halo_context, method, url, api_type, timeout, data=None, headers=None, auth=None):
        """

        :param halo_context:
        :param method:
        :param url:
        :param api_type:
        :param timeout:
        :param data:
        :param headers:
        :return:
        """

        if api_type == 'service':
            return self.provider_invoke(halo_context, method, url, api_type, timeout, data, headers, auth)

        msg = "Max Try for url: ("+str(settings.HTTP_MAX_RETRY)+") " + str(url)
        for i in range(0, settings.HTTP_MAX_RETRY):
            logger.debug("try index: " + str(i), extra=log_json(halo_context))
            try:
                logger.debug("try: " + str(i), extra=log_json(halo_context))
                ret = self.do_request(method, url, timeout, data=data, headers=headers, auth=auth)
                logger.debug("request status_code=" + str(ret.status_code) +" content=" + str(ret.content), extra=log_json(halo_context))
                if ret.status_code >= 500:
                    continue
                if 200 > ret.status_code or 500 > ret.status_code >= 300:
                    err = ApiException("error status_code " + str(ret.status_code) + " in : " + url)
                    err.status_code = ret.status_code
                    err.stack = None
                    raise err
                return ret
            except requests.exceptions.ReadTimeout as e:  # this confirms you that the request has reached server
                logger.debug(str(e))
                logger.debug(
                    "ReadTimeout " + str(
                        settings.SERVICE_READ_TIMEOUT_IN_MS) + " in method=" + method + " for url=" + url,
                    extra=log_json(halo_context))
                continue
            except requests.exceptions.ConnectTimeout as e:
                logger.debug(str(e))
                logger.debug("ConnectTimeout in method=" + str(
                    settings.SERVICE_CONNECT_TIMEOUT_IN_MS) + " in method=" + method + " for url=" + url,
                             extra=log_json(halo_context))
                continue
        raise MaxTryHttpException(msg)
コード例 #4
0
 def __init__(self, req_context):
     super(DbUtil, self).__init__(req_context)
     try:
         logger.debug('start db setup')
         self.setup()
         logger.debug('finish db setup')
     except Exception as e:
         logger.error(str(e), extra=log_json(req_context))
         raise HaloError("error in db setup:" + str(e))
コード例 #5
0
    def do_event(self, event, context):
        """

        :param event:
        :param context:
        """
        req_context = Util.get_correlation_from_event(event)
        logger.debug(' get_event : ' + str(event), extra=log_json(req_context))
        self.process_event(event, context)
コード例 #6
0
    def send_event(self, messageDict, request=None, ctx=None):
        """

        :param messageDict:
        :param request:
        :param ctx:
        :return:
        """
        if messageDict:
            messageDict[self.key_name] = self.key_val
            messageDict[self.target_service + 'service_task_id'] = 'y'
            if request:
                ctx = Util.get_req_context(request)
            if ctx:
                messageDict.update(ctx)
        else:
            raise NoMessageException("not halo msg")
        if settings.SERVER_LOCAL:
            from multiprocessing.dummy import Pool
            import requests
            url = self.get_loc_url()
            pool = Pool(1)
            futures = []
            for x in range(1):
                futures.append(
                    pool.apply_async(requests.post, [url],
                                     {'view': messageDict}))
            for future in futures:
                logger.debug("future:" + str(future.get()))
            return "sent event"
        else:
            try:
                service_name = self.target_service_name[settings.ENV_TYPE]
                logger.debug("send event to target_service:" + service_name,
                             extra=log_json(ctx))
                ret = get_provider().send_event(ctx, messageDict, service_name)
            except ProviderException as e:
                logger.error("Unexpected Provider Error",
                             extra=log_json(ctx, messageDict, e))
            else:
                logger.debug("send_event to service " + self.target_service +
                             " ret: " + str(ret),
                             extra=log_json(ctx, messageDict))
        return ret
コード例 #7
0
    def set_api_base(self, base_url):
        """

        :param query:
        :return:
        """
        strx = self.url
        if "base_url" in self.url:
            strx = strx.replace("base_url",base_url)
        logger.debug("url add base: " + strx, extra=log_json(self.halo_context))
        self.url = strx
        return self.url
コード例 #8
0
    def set_api_url(self, key, val):
        """

        :param key:
        :param val:
        :return:
        """
        strx = self.url
        strx = strx.replace("$" + str(key), str(val))
        logger.debug("url replace var: " + strx, extra=log_json(self.halo_context))
        self.url = strx
        return self.url
コード例 #9
0
 def process(self,method,url, timeout, data=None, headers=None, auth=None):
     from zeep import Client,Transport
     try:
         logger.debug(
             "Api name: " + self.name  + " url: " + str(url) + " headers:" + str(headers),
             extra=log_json(self.halo_context))
         now = datetime.datetime.now()
         transport = Transport(session=self.session, timeout=timeout, operation_timeout=timeout)
         self.client = Client(url,transport=transport)
         soap_response = self.do_request(method,timeout, data, headers, auth)
         total = datetime.datetime.now() - now
         logger.info(LOGChoice.performance_data.value, extra=log_json(self.halo_context,
                                                                      {LOGChoice.type.value: SYSTEMChoice.api.value,
                                                                       LOGChoice.milliseconds.value: int(
                                                                           total.total_seconds() * 1000),
                                                                       LOGChoice.url.value: str(url)}))
         logger.debug("ret: " + str(soap_response), extra=log_json(self.halo_context))
         return soap_response
     except ApiException as e:
         msg = str(e)
         logger.debug("error: " + msg, extra=log_json(self.halo_context))
         raise e
コード例 #10
0
    def set_api_query(self, query):
        """

        :param query:
        :return:
        """
        strx = self.url
        if "?" in self.url:
            strx = strx + "&" + query
        else:
            strx = strx + "?" + query
        logger.debug("url add query: " + strx, extra=log_json(self.halo_context))
        self.url = strx
        return self.url
コード例 #11
0
 def newfunc(*args, **kwargs):
     now = datetime.datetime.now()
     result = attr(*args, **kwargs)
     total = datetime.datetime.now() - now
     logger.info(LOGChoice.performance_data.value,
                 extra=log_json(
                     self.halo_context, {
                         LOGChoice.type.value:
                         SYSTEMChoice.dbaccess.value,
                         LOGChoice.milliseconds.value:
                         int(total.total_seconds() * 1000),
                         LOGChoice.function.value:
                         str(attr.__name__)
                     }))
     return result
コード例 #12
0
    def get_url_str(self):
        """

        :return:
        """
        api_config = settings.API_CONFIG
        logger.debug("api_config: " + str(api_config), extra=log_json(self.halo_context))
        if api_config and self.name in api_config:
            url = api_config[self.name]["url"]
            type = api_config[self.name]["type"]
            protocol = "rest"
            if "protocol" in api_config[self.name]:
                protocol = api_config[self.name]["protocol"]
            return url,type,protocol

        raise NoApiDefinitionException(self.name)
コード例 #13
0
    def process_rest(self, method, url, timeout, data=None, headers=None,auth=None):
        """

        :param method:
        :param url:
        :param timeout:
        :param data:
        :param headers:
        :return:
        """
        try:
            logger.debug("Api name: " + self.name +" method: " + str(method) + " url: " + str(url) + " headers:" + str(headers), extra=log_json(self.halo_context))
            now = datetime.datetime.now()
            ret = self.exec_client(self.halo_context, method, url, self.api_type, timeout, data=data, headers=headers, auth=auth)
            total = datetime.datetime.now() - now
            logger.info(LOGChoice.performance_data.value, extra=log_json(self.halo_context,
                                                                         {LOGChoice.type.value: SYSTEMChoice.api.value, LOGChoice.milliseconds.value: int(total.total_seconds() * 1000),
                                                       LOGChoice.url.value: str(url)}))
            logger.debug("ret: " + str(ret), extra=log_json(self.halo_context))
            return ret
        except requests.ConnectionError as e:
            msg = str(e)
            logger.debug("error: " + msg, extra=log_json(self.halo_context))
            er = ApiException(msg,e)
            er.status_code = 500
            raise er
        except requests.HTTPError as e:
            msg = str(e)
            logger.debug("error: " + msg, extra=log_json(self.halo_context))
            er = ApiException(msg,e)
            er.status_code = 500
            raise er
        except requests.Timeout as e:
            msg = str(e)
            logger.debug("error: " + msg, extra=log_json(self.halo_context))
            er = ApiException(msg,e)
            er.status_code = 500
            raise er
        except requests.RequestException as e:
            msg = str(e)
            logger.debug("error: " + msg, extra=log_json(self.halo_context))
            er = ApiException(msg,e)
            er.status_code = 500
            raise er
        except ApiException as e:
            msg = str(e)
            logger.debug("error: " + msg, extra=log_json(self.halo_context))
            raise e
コード例 #14
0
    def set_api_params(self, params):
        """

        :param params:
        :return:
        """
        if not params or len(params) == 0:
            return self.url
        strx = self.url
        for key in params:
            val = params[key]
            query = key+"="+val
            if "?" in self.url:
                strx = strx + "&" + query
            else:
                strx = strx + "?" + query
        logger.debug("url add query: " + strx, extra=log_json(self.halo_context))
        self.url = strx
        return self.url