async def patch(self, path: str, data: dict = None, *, prefix="/api") -> dict: if data is None: data = {} res = await self.client.patch( f"{prefix}{path}", data=serialize(data, "application/json")[0], headers={"Content-Type": "application/json"}, ) self.parse_cookies(res.headers) text = await res.text() return self.try_parse(text)
async def __call__(self, request: Request): context = self.controller.context params: dict[str, Any] = {"match": {}, "query": {}, "request": request} for key in request.match_info: params["match"][key] = request.match_info[key] for key in request.query: params["query"][key] = request.query[key] try: resp = await self.route.call_middleware_chain(request, params) return resp except (APIError, APIErrors) as ex: res = Response(context).from_exception(ex) if context.env["debug"]: stack = traceback.format_exc() if isinstance(ex, InternalError): console.error(stack) res.content["trace"] = stack.split("\n") serialized, mime = serialize(res.content, "application/json") web_response = aio_web.Response(text=serialized, status=res.code, content_type=mime) return web_response
async def handle(self, request, params, next_func): async with Transaction(self.data_ctx): resp = await next_func(request, params) if resp is None: return aio_web.Response(status=204) elif isinstance(resp, aio_web.Response): return resp elif isinstance(resp, str): return aio_web.Response(text=resp, status=200, content_type="text/plain") elif not isinstance(resp, APIResponse): if self.options["model"] is not None: resp = Response(self.context).ok(data=resp) else: return aio_web.Response( text="global.response.unserializable", status=500, content_type="text/plain", ) content = resp.content if content.get("data") is not None and isinstance( content["data"], Pagination): content["pagination"] = { "page": content["data"].page, "per_page": content["data"].per_page, "total": content["data"].total, } content["data"] = content["data"].items if self.options["model"] is not None: ret_def = self.data_ctx.mapper.response(self.options["model"], self.options["key"]) if content.get("data") is not None: content["data"] = self.data_ctx.mapper.marshall( ret_def, content["data"], skip_none=self.options.get("skip_none", False), as_list=self.options.get("as_list", False), ) serialized, mime = serialize(content, "application/json") web_response = aio_web.Response(text=serialized, status=resp.code, content_type=mime) for cookie in resp.cookies: if not cookie.delete: expires = None if cookie.expires: expires = cookie.expires.strftime( "%a, %d %b %Y %H:%M:%S GMT") web_response.set_cookie( cookie.name, cookie.value, expires=expires, path=cookie.path, httponly=cookie.http_only, ) else: web_response.del_cookie(cookie.name, path=cookie.path) return web_response