Ejemplo n.º 1
0
    def __init__(
        self,
        detail=None,
        headers=None,
        comment=None,
        body_template=None,
        json_formatter=None,
        **kw
    ):
        status = '%s %s' % (self.code, self.title)
        Response.__init__(self, status=status, **kw)
        Exception.__init__(self, detail)
        self.detail = self.message = detail
        if headers:
            self.headers.extend(headers)
        self.comment = comment
        if body_template is not None:
            self.body_template = body_template
            self.body_template_obj = Template(body_template)
        if json_formatter is not None:
            self._json_formatter = json_formatter

        if self.empty_body:
            del self.content_type
            del self.content_length
Ejemplo n.º 2
0
 def __init__(self, detail=None, value=None, **kw):
     status = kw.pop("status", None)
     if isinstance(status, type) and issubclass(status, HTTPException):
         status = status().status
     elif isinstance(status, str):
         try:
             int(status.split()[0])
         except Exception:
             raise ValueError(
                 "status specified as string must be of format '<code> <title>'"
             )
     elif isinstance(status, HTTPException):
         status = status.status
     elif not status:
         status = HTTPOk().status
     self.code = str(kw.pop("code", self.code))
     desc = str(detail or kw.pop("description", self.description))
     Response.__init__(self, status=status, **kw)
     Exception.__init__(self, detail)
     self.message = detail or self.description or getattr(
         self, "explanation", None)
     self.content_type = CONTENT_TYPE_APP_JSON
     value = kw.get("locator", value)
     if value:
         self.locator = value
     try:
         json_desc = self.json.get("description")
         if json_desc:
             desc = json_desc
         else:
             self.json["description"] = desc
     except Exception:  # noqa: W0703 # nosec: B110
         pass
     self.description = desc
Ejemplo n.º 3
0
    def __init__(
        self,
        detail=None,
        headers=None,
        comment=None,
        body_template=None,
        json_formatter=None,
        **kw
    ):
        status = '%s %s' % (self.code, self.title)
        Response.__init__(self, status=status, **kw)
        Exception.__init__(self, detail)
        self.detail = self.message = detail
        if headers:
            self.headers.extend(headers)
        self.comment = comment
        if body_template is not None:
            self.body_template = body_template
            self.body_template_obj = Template(body_template)
        if json_formatter is not None:
            self._json_formatter = json_formatter

        if self.empty_body:
            del self.content_type
            del self.content_length
Ejemplo n.º 4
0
 def __init__(self, detail=None, value=None, status_base=None, **kw):
     if status_base is not None:
         self.status_base = status_base
     Response.__init__(self, status=self._get_status_string(), **kw)
     Exception.__init__(self, detail)
     self.message = detail or self.explanation
     if value:
         self.locator = value
Ejemplo n.º 5
0
 def __init__(self, code=None, errors=None):
     if code:
         self.code = code
     if errors:
         self.errors = errors
     body = {
         'code': self.code,
         'errors': self.errors
         }
     Response.__init__(self, json.dumps(body))
     self.status = str(self.code)
     self.content_type = 'application/json'
Ejemplo n.º 6
0
    def __init__(self):
        # explicitly avoid calling the HTTPException init magic
        if not self.empty_body:
            Response.__init__(self, status=self.code,
                              json_body=self.json_body())
        else:
            Response.__init__(self, status=self.code)
        Exception.__init__(self)
        self.detail = self.message

        if self.empty_body:
            del self.content_type
            del self.content_length
Ejemplo n.º 7
0
    def __init__(self, code=None, json_data=None, **kw):
        if json_data is None:
            json_data = ''

        if code in HTTP_STATUSES:
            status = HTTP_STATUSES[code]
        else:
            status = HTTP_STATUSES[httpcode.InternalServerError]
            json_data = {'error': 'JSONResponse: Unable to determine HTTP status code.'}

        Response.__init__(self, status=status, body=json.dumps(json_data),
            content_type='application/json', **kw)

        self.charset = 'utf-8'
Ejemplo n.º 8
0
    def __init__(self):
        # explicitly avoid calling the HTTPException init magic
        if not self.empty_body:
            Response.__init__(self,
                              status=self.code,
                              json_body=self.json_body())
        else:
            Response.__init__(self, status=self.code)
        Exception.__init__(self)
        self.detail = self.message

        if self.empty_body:
            del self.content_type
            del self.content_length
Ejemplo n.º 9
0
 def __init__(self, obj=None, **kwargs):
     Response.__init__(self)
     self.headers[
         HTTPHeaders.CONTENT_TYPE.value] = 'application/json; charset=utf-8'
     if obj is None:
         result = dict()
     elif isinstance(obj, dict):
         result = obj.copy()
     elif hasattr(obj, '__dict__'):
         result = obj.__dict__.copy()
     else:
         result = {'data': obj}
     result.update(kwargs)
     if result:
         self.body = json.dumps(result).encode()
Ejemplo n.º 10
0
 def __init__(self, detail=None, value=None, json=None, **kw):
     # type: (Optional[str], Optional[Any], Optional[JSON], **Any) -> None
     status = kw.pop("status", None)
     if isinstance(status, type) and issubclass(status, HTTPException):
         status = status().status
     elif isinstance(status, str):
         try:
             int(status.split()[0])
         except Exception:
             raise ValueError(
                 "status specified as string must be of format '<code> <title>'"
             )
     elif isinstance(status, HTTPException):
         status = status.status
     elif not status:
         status = HTTPOk().status
     locator = kw.get("locator")
     if isinstance(json, dict):
         detail = detail or json.get("detail") or json.get("description")
         locator = locator or json.get("locator") or json.get("name")
         if value:
             json.setdefault("value", value)
     self.code = str(kw.pop("code", self.code))
     desc = str(detail or kw.pop("description", self.description))
     if json is not None:
         kw.update({"json": json})
     Response.__init__(self, status=status, **kw)
     Exception.__init__(self, detail)
     self.message = detail or self.description or getattr(
         self, "explanation", None)
     self.content_type = ContentType.APP_JSON
     if locator:
         self.locator = locator
     try:
         json_desc = self.json.get("description")
         if json_desc:
             desc = json_desc
         else:
             self.json["description"] = desc
     except Exception:  # noqa: W0703 # nosec: B110
         pass
     self.description = desc
Ejemplo n.º 11
0
    def __init__(self, request):

        # NOTE I: Pode parecer excesso de zelo... Mas, mesmo não havendo,
        # necessariamente, "views" para esses métodos eu optei por tentar
        # fechar a conexão AQUI TAMBÉM para garantir que um "raise exception"
        # da vida impeça a conexão de ser fechada! By Questor

        # NOTE II: Tentar fechar a conexão de qualquer forma!
        # -> Na criação da conexão "coautocommit=True"!
        # By Questor
        try:
            if request.context.session.is_active:
                request.context.session.close()
        except:
            pass

        self._error_message = self.title
        self.request = request
        Response.__init__(self, self.get_error(), status=self.code)
        self.content_type = 'application/json'
Ejemplo n.º 12
0
    def __init__(self, request):

        # NOTE I: Pode parecer excesso de zelo... Mas, mesmo não havendo,
        # necessariamente, "views" para esses métodos eu optei por tentar
        # fechar a conexão AQUI TAMBÉM para garantir que um "raise exception"
        # da vida impeça a conexão de ser fechada! By Questor

        # NOTE II: Tentar fechar a conexão de qualquer forma!
        # -> Na criação da conexão "coautocommit=True"!
        # By Questor
        try:
            if request.context.session.is_active:
                request.context.session.close()
        except:
            pass

        self._error_message = self.title
        self.request = request
        Response.__init__(self, self.get_error(), status=self.code)
        self.content_type = 'application/json'
Ejemplo n.º 13
0
 def __init__(self, detail=None, value=None, **kw):
     status = kw.pop("status", None)
     if isinstance(status, type) and issubclass(status, HTTPException):
         status = status().status
     elif isinstance(status, str):
         try:
             int(status.split()[0])
         except Exception:
             raise ValueError(
                 "status specified as string must be of format '<code> <title>'"
             )
     elif isinstance(status, HTTPException):
         status = status.status
     elif not status:
         status = HTTPOk().status
     Response.__init__(self, status=status, **kw)
     Exception.__init__(self, detail)
     self.message = detail or self.explanation
     self.content_type = CONTENT_TYPE_TEXT_XML
     value = kw.get("locator", value)
     if value:
         self.locator = value
Ejemplo n.º 14
0
 def __init__(self, ref: str):
     Response.__init__(self)
     self.status = HTTPStatus.OK
     self.location = ref
     self.body = ('<script>location.href = "' + ref + '"</script>').encode()
Ejemplo n.º 15
0
 def __init__(self, error):
     Response.__init__(self, json.dumps(error), status=self.code)
     self.content_type = 'application/json'
Ejemplo n.º 16
0
 def __init__(self, serializer, serializer_kw, errors, status=400):
     body = {'status': 'error', 'errors': errors}
     Response.__init__(self, serializer(body, **serializer_kw))
     self.status = status
     self.content_type = 'application/json'
Ejemplo n.º 17
0
 def __init__(self, error):
     Response.__init__(self, json.dumps(error), status=self.code)
     self.content_type = 'application/json'
Ejemplo n.º 18
0
 def __init__(self, msg='Unauthorized'):
     body = {'status': 401, 'message': msg}
     Response.__init__(self, json.dumps(body))
     self.status = 401
     self.content_type = 'application/json'
Ejemplo n.º 19
0
 def __init__(self, data=None, status=404):
     #body = {'status': 'error', 'errors': errors}
     Response.__init__(self, json.dumps(data, use_decimal=True))
     self.status = status
     self.content_type = 'application/json'
Ejemplo n.º 20
0
 def __init__(self, errors, code=400):
     self.code = code
     body = {'status': "error", "errors": errors}
     Response.__init__(self, status=code, body=render("json", body))
     self.content_type = 'application/json'
Ejemplo n.º 21
0
 def __init__(self, **kw):
     Response.__init__(self, **kw)
     Exception.__init__(self)
Ejemplo n.º 22
0
 def __init__(self, **kw):
     Response.__init__(self, **kw)
     Exception.__init__(self)
Ejemplo n.º 23
0
 def __init__(self, detail=None, value=None, **kw):
     Response.__init__(self, status='200 OK', **kw)
     Exception.__init__(self, detail)
     self.message = detail or self.explanation
     if value:
         self.locator = value
Ejemplo n.º 24
0
 def __init__(self, msg=None):
     body = {'status': self.code, 'message': msg or self.msg}
     Response.__init__(self, json.dumps(body))
     self.status = self.code
     self.content_type = 'application/json'
Ejemplo n.º 25
0
 def __init__(self, request, _error_message):
     print(traceback.format_exc())
     self._error_message = _error_message
     self.request = request
     Response.__init__(self, self.get_error(), status=self.code)
     self.content_type = 'application/json'
Ejemplo n.º 26
0
 def __init__(self, errors, status=400):
     body = {'errors': errors}
     Response.__init__(self, dumps(body))
     self.status = status
     self.content_type = 'application/json'
Ejemplo n.º 27
0
 def __init__(self, errors, status=400):
     body = {'errors': errors}
     Response.__init__(self, dumps(body))
     self.status = status
     self.content_type = 'application/json'
Ejemplo n.º 28
0
 def __init__(self, error):
     Response.__init__(self, utils.object2json(error), status=self.code)
     self.content_type = 'application/json'
Ejemplo n.º 29
0
 def __init__(self, request):
     self._error_message = self.title
     self.request = request
     Response.__init__(self, self.get_error(), status=self.code)
     self.content_type = 'application/json'
Ejemplo n.º 30
0
 def __init__(self, errors, status=400):
     body = {'status': 'error', 'errors': errors}
     Response.__init__(self, json.dumps(body, use_decimal=True))
     self.status = status
     self.content_type = 'application/json'
Ejemplo n.º 31
0
 def __init__(self, error):
     Response.__init__(self, utils.object2json(error), status=self.code)
     self.content_type = 'application/json'
Ejemplo n.º 32
0
 def __init__(self):
     # explicitly avoid calling the HTTPException init magic
     Response.__init__(self, status=self.code, json_body=self.json_body())
     Exception.__init__(self)
     self.detail = self.message
Ejemplo n.º 33
0
 def __init__(self, request, _error_message):
     print(traceback.format_exc())
     self._error_message = _error_message
     self.request = request
     Response.__init__(self, self.get_error(), status=self.code)
     self.content_type = 'application/json'
 def __init__(self, errors, status_code=400, status_message='error'):
     body = {'status': status_message, 'errors': errors}
     Response.__init__(self, json.dumps(body))
     self.status = status_code
     self.content_type = 'application/json'
Ejemplo n.º 35
0
 def __init__(self, request):
     self._error_message = self.title
     self.request = request
     Response.__init__(self, self.get_error(), status=self.code)
     self.content_type = 'application/json'
Ejemplo n.º 36
0
 def __init__(self, errors, status=400):
     Response.__init__(self, PARSE_ERROR)
     self.status = status
     self.content_type = 'application/json'
Ejemplo n.º 37
0
 def __init__(self, detail=None, value=None, **kw):
     Response.__init__(self, status='200 OK', **kw)
     Exception.__init__(self, detail)
     self.message = detail or self.explanation
     if value:
         self.locator = value
Ejemplo n.º 38
0
 def __init__(self, errors, status=400):
     Response.__init__(self, PARSE_ERROR)
     self.status = status
     self.content_type = 'application/json'
Ejemplo n.º 39
0
 def __init__(self, errors, code=400):
     self.code = code
     body = {'status': "error", "errors": errors}
     Response.__init__(self, status=code, body=render("json", body))
     self.content_type = 'application/json'
Ejemplo n.º 40
0
 def __init__(self, errors, status=400):
     body = {'status': 'error', 'errors': errors}
     Response.__init__(self, simplejson.dumps(body, use_decimal=True))
     self.status = status
     self.content_type = 'application/json'
Ejemplo n.º 41
0
 def __init__(self, errors, status=400):
     body = {"status": "error", "errors": errors}
     Response.__init__(self, json.dumps(body, use_decimal=True))
     self.status = status
     self.content_type = "application/json"