コード例 #1
0
ファイル: application.py プロジェクト: oh8/rpc.py
    def preprocess(self, request):
        """
        Preprocess request
        """
        # try return openapi
        if self.openapi is not None and request.method == "GET":
            if request.url.path[len(self.prefix):] == "openapi-docs":
                return self.return_response_class(request)(
                    OPENAPI_TEMPLATE, media_type="text/html")
            elif request.url.path[len(self.prefix):] == "get-openapi-docs":
                return self.return_response_class(request)(
                    json.dumps(self.get_openapi_docs(), ensure_ascii=False),
                    media_type="application/json",
                )

        # check request method
        if request.method != "POST":
            return self.return_response_class(request)(status_code=405)

        # check serializer
        try:
            self.request_serializer = get_serializer(request.headers)
        except SerializerNotFound as exception:
            return self.return_response_class(request)(str(exception),
                                                       status_code=415,
                                                       media_type="text/plain")
コード例 #2
0
            def wrapper(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
                post_content = self._get_content(func, *args, **kwargs)
                with self.client.stream(
                    "POST",
                    url,
                    content=post_content,
                    headers={
                        "content-type": self.request_serializer.content_type,
                        "serializer": self.request_serializer.name,
                    },
                ) as resp:
                    resp.raise_for_status()
                    sse_parser = ServerSentEventsParser()
                    serializer = get_serializer(resp.headers)
                    for line in resp.iter_lines():
                        event = sse_parser.feed(line)
                        if not event:
                            continue

                        if event["event"] == "yield":
                            yield serializer.decode(
                                b64decode(event["data"].encode("ascii"))
                            )
                        elif event["event"] == "exception":
                            raise RemoteCallError(
                                serializer.decode(b64decode(event["data"].encode("ascii")))
                            )
                        else:
                            raise RuntimeError(f"Unknown event type: {event['event']}")
コード例 #3
0
 def wrapper(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
     post_content = self._get_content(func, *args, **kwargs)
     resp = self.client.post(
         url,
         content=post_content,
         headers={
             "content-type": self.request_serializer.content_type,
             "serializer": self.request_serializer.name,
         },
     )
     resp.raise_for_status()
     return get_serializer(resp.headers).decode(resp.content)
コード例 #4
0
 async def wrapper(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
     post_content = self._get_content(func, *args, **kwargs)
     resp = await self.client.post(
         url,
         content=post_content,
         headers={
             "content-type": self.request_serializer.content_type,
             "serializer": self.request_serializer.name,
         },
     )
     resp.raise_for_status()
     content = get_serializer(resp.headers).decode(resp.content)
     if resp.headers.get("callback-status") == "exception":
         raise RemoteCallError(content)
     else:
         return content
コード例 #5
0
 def wrapper(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
     post_content = self._get_content(func, *args, **kwargs)
     with self.client.stream(
             "POST",
             url,
             content=post_content,
             headers={
                 "content-type":
                 self.request_serializer.content_type,
                 "serializer": self.request_serializer.name,
             },
     ) as resp:
         resp.raise_for_status()
         for line in resp.iter_lines():
             if not line.startswith("data:"):
                 continue
             data = line.split(":", maxsplit=1)[1]
             yield get_serializer(resp.headers).decode(
                 b64decode(data.encode("ascii")))