Example #1
0
 def __cborsenml_set(self, payload, request_uri):
     try:
         new = cbor.loads(payload)
         if len(new) != 1 or new[0].get(-2, "") + new[0].get(0, "") != request_uri:
             raise BadRequest("Not a single record pertaining to this resource")
         self.value = self.valuetype(new[self.cborsenml_key])
     except (KeyError, ValueError):
         raise BadRequest()
Example #2
0
 def __jsonsenml_set(self, payload, request_uri):
     try:
         new = json.loads(payload.decode('utf8'))
         if len(new) != 1 or new[0].get("bn", "") + new[0].get("n", "") != request_uri:
             raise BadRequest("Not a single record pertaining to this resource")
         self.value = self.valuetype(new[0][self.jsonsenml_key])
     except (KeyError, ValueError):
         raise BadRequest()
    async def render(self, request):
        cf = request.opt.content_format
        acc = request.opt.accept
        raise_class = UnallowedMethod
        method_would_have_worked = False

        # FIXME: manually walking the MRO is not a nice way to go about this;
        # is there no other way to query the registered handlers according to
        # the regular inheritance patterns?
        for cls in type(self).mro():
            if not issubclass(
                    cls, ContenttypeRendered) or cls is ContenttypeRendered:
                continue
            for_method = cls.__handlers.get(request.code, None)
            if for_method is None:
                continue
            raise_class = UnsupportedContentFormat
            handler, responseformat = for_method.get((acc, cf), (None, None))
            if handler is not None:
                break
        else:
            raise raise_class()

        sig = inspect.signature(handler)
        parameters = set(sig.parameters.keys())
        parameters.remove("self")
        kwargs = {}
        if request.payload and "payload" not in parameters:
            raise BadRequest("Unexpected payload")
        if request.opt.uri_query and "query" not in parameters:
            raise BadRequest("Unexepcted query arguments")

        for p in parameters:
            if p == "payload":
                kwargs['payload'] = request.payload
            elif p == "request_uri":
                # BIG FIXME: This does not give the expected results due to the
                # URI path stripping in Site, and because Message gets the
                # requested authority wrong on the server side.
                kwargs["request_uri"] = request.get_request_uri()
            else:
                raise RuntimeError("Unexpected argument requested: %s" % p)
        payload = handler(self, **kwargs)

        if payload is None:
            payload = b""
        elif isinstance(payload, str):
            payload = payload.encode('utf8')

        return Message(
            code={
                GET: CONTENT,
                PUT: CHANGED
            }[request.code],
            payload=payload,
            content_format=responseformat,
            no_response=request.opt.no_response,
        )
 def __textplain_set(self, payload):
     try:
         self.value = {
             "0": False,
             "1": True
         }[payload.decode('utf8').strip()]
     except (KeyError, ValueError):
         raise BadRequest()
Example #5
0
    async def render(self, request):
        cf = request.opt.content_format
        acc = request.opt.accept
        raise_class = UnallowedMethod
        method_would_have_worked = False

        # FIXME: manually walking the MRO is not a nice way to go about this;
        # is there no other way to query the registered handlers according to
        # the regular inheritance patterns?
        for cls in type(self).mro():
            if not issubclass(cls, ContenttypeRendered):
                continue
            for_method = cls.__handlers.get(request.code, None)
            if for_method is None:
                continue
            raise_class = UnsupportedContentFormat
            handler, responseformat = for_method.get((acc, cf), (None, None))
            if handler is not None:
                break
        else:
            raise raise_class()

        sig = inspect.signature(handler)
        if any(i != 0 and a.kind not in (inspect.Parameter.KEYWORD_ONLY,
                                         inspect.Parameter.VAR_KEYWORD)
               for (i, a) in enumerate(sig.parameters.values())):
            # there's a positional argument
            args = (request.payload, )
        else:
            if request.payload:
                raise BadRequest("Unexpected payload")
            args = ()
        # FIXME add query as kwargs?
        payload = handler(self, *args)

        if payload is None:
            payload = b""
        elif isinstance(payload, str):
            payload = payload.encode('utf8')

        return Message(code={
            GET: CONTENT,
            PUT: CHANGED
        }[request.code],
                       payload=payload,
                       content_format=responseformat)
 def __textplain_set(self, payload):
     try:
         self.value = self.valuetype(payload.decode('utf8').strip())
     except ValueError:
         raise BadRequest()
Example #7
0
 def __cborsenml_set(self, value):
     try:
         self.value = self.valuetype(new[self.cborsenml_key])
     except (KeyError, ValueError):
         raise BadRequest()
Example #8
0
 def __jsonsenml_set(self, new):
     try:
         self.value = self.valuetype(new[self.jsonsenml_key])
     except (KeyError, ValueError):
         raise BadRequest()