Exemple #1
0
def _debug(method,
           url,
           params,
           payload,
           request_headers,
           response_headers,
           response,
           status_code,
           elapsed,
           cached=None):
    try:
        if g.app.debug:
            debug = g.app.debug
        else:
            debug = False
    except NoContextError:
        debug = True

    if debug:
        log_id = string_id(length=6)
        try:
            payload = js.loads(payload)
            payload = js.dumps(payload)
        except Exception:
            pass
        try:
            response = js.loads(response)
            response = js.dumps(response)
        except Exception:
            pass

        log.debug('Method: %s' % method + ', URL: %s' % url +
                  ', Params: %s' % params + ' (%s %s)' %
                  (status_code, HTTP_STATUS_CODES[status_code]) +
                  ' Cache: %s' % cached,
                  timer=elapsed,
                  log_id=log_id)
        for header in request_headers:
            log.debug('Request Header: %s="%s"' %
                      (header, request_headers[header]),
                      log_id=log_id)
        for header in response_headers:
            log.debug('Response Header: %s="%s"' %
                      (header, response_headers[header]),
                      log_id=log_id)
        log.debug(payload, prepend='Request Payload', log_id=log_id)
        log.debug(response, prepend='Response Payload', log_id=log_id)
Exemple #2
0
 def parse(self, value):
     if isinstance(value, (dict, list)):
         return js.dumps(value, indent=0).replace('\n', '')
     if isinstance(value, str):
         try:
             return js.loads(value)
         except Exception:
             self.error("Invalid json in '%s'" % value, value)
Exemple #3
0
    def json(self):
        content_type = self.content_type
        if (content_type is not None and 'JSON' in content_type):

            if self.encoding != 'UTF-8':
                raise RestClientError('JSON requires UTF-8 Encoding')

            try:
                if self.status_code != 204:
                    return js.loads(self.body)
                else:
                    return b''

            except JSONDecodeError as e:
                raise RestClientError('JSON Decode: %s' % e)
Exemple #4
0
    def policy(self, req, resp):
        override_file = g.app.path + '/policy.json'
        luxon = Module('luxon')
        policy = luxon.read('policy.json')
        try:
            policy = js.loads(policy)
        except JSONDecodeError as exception:
            raise JSONDecodeError("Invalid Luxon 'policy.json' %s" % exception)
        if is_file(override_file):
            with open(override_file, 'r') as override:
                override = override.read()
                try:
                    override = js.loads(override)
                except JSONDecodeError as exception:
                    raise JSONDecodeError("Invalid Override 'policy.json' %s" %
                                          exception)
        else:
            override = {}

        policy.update(override)

        return render_template('photonic/policy.html',
                               policy=policy,
                               view="RBAC Policy Engine Rules")
Exemple #5
0
    def json(self):
        if self._cached_json is None:
            if self.encoding is not None and self.encoding != 'UTF-8':
                raise HTTPClientContentDecodingError(
                    'JSON requires UTF-8 Encoding') from None

            try:
                if self.status_code != 204:
                    self._cached_json = js.loads(self.content)
                else:
                    self._cached_json = None

            except JSONDecodeError as e:
                raise HTTPClientContentDecodingError('JSON Decode: %s' %
                                                     e) from None

        return self._cached_json
Exemple #6
0
 def callback_wrapper(ch, method, properties, body):
     message = js.loads(body)
     try:
         if callback(self.connection, ch, method, properties, message):
             if acks:
                 ch.basic_ack(delivery_tag=method.delivery_tag)
         else:
             if acks:
                 ch.basic_reject(delivery_tag=method.delivery_tag,
                                 requeue=False)
     except Exception as e:
         if acks:
             ch.basic_reject(delivery_tag=method.delivery_tag,
                             requeue=True)
         log.critical(
             '%s\n%s\n%s' %
             (str(e), str(message), str(traceback.format_exc(), )))