Ejemplo n.º 1
0
def set_message_sent(id_message, svc_port):
    log.info('Set {} sent'.format(id_message))

    import json
    import datetime
    from base_svc.comm import BaseAPIRequestHandler

    n = str(datetime.datetime.now())[:19]

    rh = BaseAPIRequestHandler()
    data = {'id_message': id_message, 'sent_time': n}
    rh.set_argument('data', json.dumps(data))
    kwargs = {}
    kwargs['request_handler'] = rh

    from base_svc.comm import call
    import base_api.mail_api.sent_mail

    try:
        res, status = call(
            'localhost',
            # base_config.settings.APP_PORT,
            svc_port,
            base_api.mail_api.sent_mail.location,
            data,
            base_api.mail_api.sent_mail.set_mail_sent.__api_method_type__)
    except ConnectionRefusedError as e:
        log.critical('Servis not working: {}'.format(e))
        return False

    if status != 204:
        log.error('Error set message {} sent: {}'.format(id_message, res))

    return True
Ejemplo n.º 2
0
def set_message_sent(id_message, svc_port):
    log.info('Set {} sent'.format(id_message))

    import json
    import datetime
    from base_svc.comm import BaseAPIRequestHandler

    n = str(datetime.datetime.now())[:19]

    rh = BaseAPIRequestHandler()
    data = {'id_message': id_message, 'sent_time': n}
    rh.set_argument('data', json.dumps(data, ensure_ascii=False))
    kwargs = {}
    kwargs['request_handler'] = rh

    from base_svc.comm import call
    import base_api.mail_api.sent_mail

    try:
        res, status = call(
            'localhost',
            # base_config.settings.APP_PORT,
            svc_port,
            base_api.mail_api.sent_mail.location,
            data,
            base_api.mail_api.sent_mail.set_mail_sent.__api_method_type__)
    except ConnectionRefusedError as e:
        log.critical('Servis not working: {}'.format(e))
        return False

    if status != 204:
        log.error('Error set message {} sent: {}'.format(id_message, res))

    return True
Ejemplo n.º 3
0
def get_first_param_uri(request):

    try:
        return qu_esc(get_param_from_uri(request.request.uri.split('?')[0]))
    except Exception as e:
        log.error(e)
        return None
Ejemplo n.º 4
0
def get_first_param_uri(request):

    try:
        return qu_esc( get_param_from_uri(request.request.uri.split('?')[0]) )
    except Exception as e:
        log.error(e)
        return None
Ejemplo n.º 5
0
    def call_api_fun(self, method):

        self.set_header('Access-Control-Allow-Origin', '*')

        j, ip = self.check()

        try:
            if method in self.apimodule_map:

                fun = self.apimodule_map[method]

                if hasattr(fun,'__api_return_type__'):
                    self.set_api_header(getattr(fun,'__api_return_type__'))
                else:
                    self.set_header('Content-Type', 'application/json')

                if not hasattr(fun, '__api_method_call__') or not fun.__api_method_call__:
                    self.set_status(500)
                    self.write(json.dumps(base_common.msg.error(amsgs.NOT_API_CALL)))
                    return

                # k = fun.__name__
                # p = fun.__parent__
                _fun_method = getattr(fun, '__api_method_type__')
                _fun_method = http_map[_fun_method]
                if method != _fun_method:
                    log.critical('Trying to call {} method on {} function from {}'.format(
                        method, fun.__name__, self.apimodule.__name__))

                    self.set_status(500)
                    self.write(json.dumps(base_common.msg.error(self.e_msgs[method])))
                    return

                if csettings.LB:

                    global _c
                    _server = csettings.BALANCE[ _c % len(csettings.BALANCE) ]
                    _c += 1

                    self._a_p = _server[-4:]
                    log.info('CALLING SERVER: {}'.format(self._a_p))
                    self.a_call(_server, method)
                    return

                else:

                    result = fun(request_handler=self, logged_user_dict=j, r_ip=ip, auth_token=self.auth_token)

                self.set_status(200)
                if 'http_status' in result:
                    self.set_status(result['http_status'])
                    del result['http_status']

                if result != {}:
                    self.write(json.dumps(result))

            else:
                log.error("ip: {}, {} not implemented".format(ip, http_rev_map[method]))
                self.set_status(404)
                self.set_header('Content-Type', 'application/json')
                self.write(json.dumps(base_common.msg.error(self.e_msgs[method])))

        except Exception as e:

            log.error(
                "ip: {}, module: {}, function: {}, exception: e:{}".format(ip, self.apimodule.__name__, method, e))
            self.set_status(500)
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(base_common.msg.error(e)))
Ejemplo n.º 6
0
    def call_api_fun(self, method):

        self.set_header('Access-Control-Allow-Origin', '*')

        j, ip = self.check()

        try:
            if method in self.apimodule_map:

                fun = self.apimodule_map[method]

                if hasattr(fun, '__api_return_type__'):
                    self.set_api_header(getattr(fun, '__api_return_type__'))
                else:
                    self.set_header('Content-Type', 'application/json')

                if not hasattr(
                        fun,
                        '__api_method_call__') or not fun.__api_method_call__:
                    self.set_status(500)
                    self.write(
                        json.dumps(base_common.msg.error(amsgs.NOT_API_CALL,
                                                         forget_status=True),
                                   ensure_ascii=False))
                    return

                # k = fun.__name__
                # p = fun.__parent__
                _fun_method = getattr(fun, '__api_method_type__')
                _fun_method = http_map[_fun_method]
                if method != _fun_method:
                    log.critical(
                        'Trying to call {} method on {} function from {}'.
                        format(method, fun.__name__, self.apimodule.__name__))

                    self.set_status(500)
                    self.write(
                        json.dumps(base_common.msg.error(self.e_msgs[method],
                                                         forget_status=True),
                                   ensure_ascii=False))
                    return

                if csettings.LB:

                    global _c
                    _server = csettings.BALANCE[_c % len(csettings.BALANCE)]
                    _c += 1

                    self._a_p = _server[-4:]
                    log.info('CALLING SERVER: {}'.format(self._a_p))
                    self.a_call(_server, method)
                    return

                else:

                    result = fun(request_handler=self,
                                 logged_user_dict=j,
                                 r_ip=ip,
                                 auth_token=self.auth_token)

                if 'redirect' in result and result['redirect']:
                    if not 'redirect_url' in result:
                        self.set_status(500)
                        self.set_header('Content-Type', 'application/json')
                        self.write(
                            json.dumps(base_common.msg.error(
                                amsgs.MISSING_REDIRECTION_URL,
                                forget_status=True),
                                       ensure_ascii=False))

                    _redirection_status = result[
                        'redirection_http_status'] if 'redirection_http_status' in result else 302
                    _permanent = _redirection_status == 301 or 'permanent' in result and result[
                        'permanent']
                    self.redirect(result['redirect_url'],
                                  permanent=_permanent,
                                  status=_redirection_status)
                    return

                self.set_status(200)
                if 'http_status' in result:
                    self.set_status(result['http_status'])
                    del result['http_status']

                if result != {}:
                    self.write(json.dumps(result, ensure_ascii=False))

            else:
                log.error("ip: {}, {} not implemented".format(
                    ip, http_rev_map[method]))
                log.error(
                    "ip: {}, {} ({}) not implemented, with module map: {}".
                    format(ip, http_rev_map[method], method,
                           self.apimodule_map))
                self.set_status(404)
                self.set_header('Content-Type', 'application/json')
                self.write(
                    json.dumps(base_common.msg.error(self.e_msgs[method],
                                                     forget_status=True),
                               ensure_ascii=False))

        except Exception as e:

            log.error(
                "ip: {}, module: {}, function: {}, exception: e:{}".format(
                    ip, self.apimodule.__name__, method, e))
            self.set_status(500)
            self.set_header('Content-Type', 'application/json')
            self.write(
                json.dumps(base_common.msg.error(e, forget_status=True),
                           ensure_ascii=False))