Exemplo n.º 1
0
async def tag(
    pretty: bool = False,
    request: Request = None,
):
    data = await request.body()

    async def mock_stream(data):
        yield data
        yield b""

    parser = FormParser(request.headers, mock_stream(data))
    form = await parser.parse()

    data = form.get("data") or str(data, "utf-8")
    action = form.get("action") or "pos_tagging"
    pretty = pretty or form.get("pretty") or False

    if not data:
        return {"error": "No data posted or data incorrectly encoded"}

    action = ACTIONS[action]()
    tagged_json = action.parse(data)

    if pretty:
        return maybe_pretty_json_response(indent=4,
                                          separators=(", ", ": "))(tagged_json)

    return maybe_pretty_json_response(indent=None,
                                      separators=(",", ":"))(tagged_json)
Exemplo n.º 2
0
 async def form(self) -> FormData:
     if self.content_type == "multipart/form-data":
         multipart_parser = MultiPartParser(self.headers, self.stream())
         return await multipart_parser.parse()
     if self.content_type == "application/x-www-form-urlencoded":
         form_parser = FormParser(self.headers, self.stream())
         return await form_parser.parse()
     return FormData()
Exemplo n.º 3
0
 async def form(self) -> FormData:
     if not hasattr(self, "_form"):
         assert (
             parse_options_header is not None
         ), "The `python-multipart` library must be installed to use form parsing."
         content_type_header = self.headers.get("Content-Type")
         content_type, options = parse_options_header(content_type_header)
         if content_type == b"multipart/form-data":
             multipart_parser = MultiPartParser(self.headers, self.stream())
             self._form = await multipart_parser.parse()
         elif content_type == b"application/x-www-form-urlencoded":
             form_parser = FormParser(self.headers, self.stream())
             self._form = await form_parser.parse()
         else:
             self._form = FormData()
     return self._form
Exemplo n.º 4
0
 async def form(self) -> FormData:
     if not hasattr(self, "_form"):
         assert (
             parse_options_header is not None
         ), "The `python-multipart` library must be installed to use form parsing."
         content_type_header = self.headers.get("Content-Type")
         content_type: bytes
         content_type, _ = parse_options_header(content_type_header)
         if content_type == b"multipart/form-data":
             try:
                 multipart_parser = MultiPartParser(self.headers,
                                                    self.stream())
                 self._form = await multipart_parser.parse()
             except MultiPartException as exc:
                 if "app" in self.scope:
                     raise HTTPException(status_code=400,
                                         detail=exc.message)
                 raise exc
         elif content_type == b"application/x-www-form-urlencoded":
             form_parser = FormParser(self.headers, self.stream())
             self._form = await form_parser.parse()
         else:
             self._form = FormData()
     return self._form