mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-07-30 14:19:56 +00:00
use http client class for all http requests
This commit is contained in:
@ -60,31 +60,31 @@ def exception_handler(logger: logging.Logger) -> MiddlewareType:
|
||||
async def handle(request: Request, handler: HandlerType) -> StreamResponse:
|
||||
try:
|
||||
return await handler(request)
|
||||
except HTTPUnauthorized as e:
|
||||
except HTTPUnauthorized as ex:
|
||||
if _is_templated_unauthorized(request):
|
||||
context = {"code": e.status_code, "reason": e.reason}
|
||||
return aiohttp_jinja2.render_template("error.jinja2", request, context, status=e.status_code)
|
||||
return json_response(data={"error": e.reason}, status=e.status_code)
|
||||
except HTTPMethodNotAllowed as e:
|
||||
if e.method == "OPTIONS":
|
||||
context = {"code": ex.status_code, "reason": ex.reason}
|
||||
return aiohttp_jinja2.render_template("error.jinja2", request, context, status=ex.status_code)
|
||||
return json_response(data={"error": ex.reason}, status=ex.status_code)
|
||||
except HTTPMethodNotAllowed as ex:
|
||||
if ex.method == "OPTIONS":
|
||||
# automatically handle OPTIONS method, idea comes from
|
||||
# https://github.com/arcan1s/ffxivbis/blob/master/src/main/scala/me/arcanis/ffxivbis/http/api/v1/HttpHandler.scala#L32
|
||||
raise HTTPNoContent(headers={"Allow": ",".join(sorted(e.allowed_methods))})
|
||||
if e.method == "HEAD":
|
||||
raise HTTPNoContent(headers={"Allow": ",".join(sorted(ex.allowed_methods))})
|
||||
if ex.method == "HEAD":
|
||||
# since we have special autogenerated HEAD method, we need to remove it from list of available
|
||||
e.allowed_methods = {method for method in e.allowed_methods if method != "HEAD"}
|
||||
e.headers["Allow"] = ",".join(sorted(e.allowed_methods))
|
||||
raise e
|
||||
ex.allowed_methods = {method for method in ex.allowed_methods if method != "HEAD"}
|
||||
ex.headers["Allow"] = ",".join(sorted(ex.allowed_methods))
|
||||
raise ex
|
||||
raise
|
||||
except HTTPClientError as e:
|
||||
return json_response(data={"error": e.reason}, status=e.status_code)
|
||||
except HTTPServerError as e:
|
||||
except HTTPClientError as ex:
|
||||
return json_response(data={"error": ex.reason}, status=ex.status_code)
|
||||
except HTTPServerError as ex:
|
||||
logger.exception("server exception during performing request to %s", request.path)
|
||||
return json_response(data={"error": e.reason}, status=e.status_code)
|
||||
return json_response(data={"error": ex.reason}, status=ex.status_code)
|
||||
except HTTPException: # just raise 2xx and 3xx codes
|
||||
raise
|
||||
except Exception as e:
|
||||
except Exception as ex:
|
||||
logger.exception("unknown exception during performing request to %s", request.path)
|
||||
return json_response(data={"error": str(e)}, status=500)
|
||||
return json_response(data={"error": str(ex)}, status=500)
|
||||
|
||||
return handle
|
||||
|
@ -64,8 +64,8 @@ class AddView(BaseView):
|
||||
try:
|
||||
data = await self.extract_data(["packages"])
|
||||
packages = self.get_non_empty(lambda key: [package for package in data[key] if package], "packages")
|
||||
except Exception as e:
|
||||
raise HTTPBadRequest(reason=str(e))
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
username = await self.username()
|
||||
process_id = self.spawner.packages_add(packages, username, now=True)
|
||||
|
@ -68,8 +68,8 @@ class PGPView(BaseView):
|
||||
try:
|
||||
key = self.get_non_empty(self.request.query.getone, "key")
|
||||
server = self.get_non_empty(self.request.query.getone, "server")
|
||||
except Exception as e:
|
||||
raise HTTPBadRequest(reason=str(e))
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
try:
|
||||
key = self.service.repository.sign.key_download(server, key)
|
||||
@ -107,8 +107,8 @@ class PGPView(BaseView):
|
||||
|
||||
try:
|
||||
key = self.get_non_empty(data.get, "key")
|
||||
except Exception as e:
|
||||
raise HTTPBadRequest(reason=str(e))
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
process_id = self.spawner.key_import(key, data.get("server"))
|
||||
|
||||
|
@ -65,8 +65,8 @@ class RebuildView(BaseView):
|
||||
data = await self.extract_data(["packages"])
|
||||
packages = self.get_non_empty(lambda key: [package for package in data[key] if package], "packages")
|
||||
depends_on = next(iter(packages))
|
||||
except Exception as e:
|
||||
raise HTTPBadRequest(reason=str(e))
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
username = await self.username()
|
||||
process_id = self.spawner.packages_rebuild(depends_on, username)
|
||||
|
@ -64,8 +64,8 @@ class RemoveView(BaseView):
|
||||
try:
|
||||
data = await self.extract_data(["packages"])
|
||||
packages = self.get_non_empty(lambda key: [package for package in data[key] if package], "packages")
|
||||
except Exception as e:
|
||||
raise HTTPBadRequest(reason=str(e))
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
process_id = self.spawner.packages_remove(packages)
|
||||
|
||||
|
@ -64,8 +64,8 @@ class RequestView(BaseView):
|
||||
try:
|
||||
data = await self.extract_data(["packages"])
|
||||
packages = self.get_non_empty(lambda key: [package for package in data[key] if package], "packages")
|
||||
except Exception as e:
|
||||
raise HTTPBadRequest(reason=str(e))
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
username = await self.username()
|
||||
process_id = self.spawner.packages_add(packages, username, now=False)
|
||||
|
@ -69,8 +69,8 @@ class SearchView(BaseView):
|
||||
try:
|
||||
search: list[str] = self.get_non_empty(lambda key: self.request.query.getall(key, default=[]), "for")
|
||||
packages = AUR.multisearch(*search, pacman=self.service.repository.pacman)
|
||||
except Exception as e:
|
||||
raise HTTPBadRequest(reason=str(e))
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
if not packages:
|
||||
raise HTTPNotFound(reason=f"No packages found for terms: {search}")
|
||||
|
@ -63,8 +63,8 @@ class UpdateView(BaseView):
|
||||
"""
|
||||
try:
|
||||
data = await self.extract_data()
|
||||
except Exception as e:
|
||||
raise HTTPBadRequest(reason=str(e))
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
username = await self.username()
|
||||
process_id = self.spawner.packages_update(
|
||||
|
@ -120,8 +120,8 @@ class UploadView(BaseView):
|
||||
|
||||
try:
|
||||
reader = await self.request.multipart()
|
||||
except Exception as e:
|
||||
raise HTTPBadRequest(reason=str(e))
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
max_body_size = self.configuration.getint("web", "max_body_size", fallback=None)
|
||||
target = self.configuration.repository_paths.packages
|
||||
|
@ -138,8 +138,8 @@ class LogsView(BaseView):
|
||||
created = data["created"]
|
||||
record = data["message"]
|
||||
version = data["version"]
|
||||
except Exception as e:
|
||||
raise HTTPBadRequest(reason=str(e))
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
self.service.logs_update(LogRecordId(package_base, version), created, record)
|
||||
|
||||
|
@ -138,8 +138,8 @@ class PackageView(BaseView):
|
||||
try:
|
||||
package = Package.from_json(data["package"]) if "package" in data else None
|
||||
status = BuildStatusEnum(data["status"])
|
||||
except Exception as e:
|
||||
raise HTTPBadRequest(reason=str(e))
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
try:
|
||||
self.service.package_update(package_base, status, package)
|
||||
|
@ -99,8 +99,8 @@ class StatusView(BaseView):
|
||||
try:
|
||||
data = await self.extract_data()
|
||||
status = BuildStatusEnum(data["status"])
|
||||
except Exception as e:
|
||||
raise HTTPBadRequest(reason=str(e))
|
||||
except Exception as ex:
|
||||
raise HTTPBadRequest(reason=str(ex))
|
||||
|
||||
self.service.status_update(status)
|
||||
|
||||
|
Reference in New Issue
Block a user