From e2b003a1bd2b15532ade970ec381413776cd8c6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0varc?= Date: Sat, 15 Jul 2023 18:17:29 +0200 Subject: [PATCH] removing hard dependency on GCP --- pyproject.toml | 2 +- src/surquest/fastapi/utils/GCP/catcher.py | 2 +- src/surquest/fastapi/utils/GCP/tracer.py | 22 ++++++++----- src/surquest/fastapi/utils/auth.py | 38 +++++++++++++++++++++++ 4 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 src/surquest/fastapi/utils/auth.py diff --git a/pyproject.toml b/pyproject.toml index b938a98..968a572 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "surquest-fastapi-utils" -version = "0.2.1" +version = "0.2.3" description = "This project provides collection of utilities for FastAPI framework as: Catcher, Middleware, etc." authors = [ {name= "Michal Švarc", email= "michal.svarc@surquest.com"} diff --git a/src/surquest/fastapi/utils/GCP/catcher.py b/src/surquest/fastapi/utils/GCP/catcher.py index 2fd432d..e839aba 100644 --- a/src/surquest/fastapi/utils/GCP/catcher.py +++ b/src/surquest/fastapi/utils/GCP/catcher.py @@ -124,6 +124,6 @@ async def catch_http_exception( ) return Response.set( - status_code=404, + status_code=getattr(exc, "status_code", 404), errors=[message] ) diff --git a/src/surquest/fastapi/utils/GCP/tracer.py b/src/surquest/fastapi/utils/GCP/tracer.py index d7ba174..8912b92 100644 --- a/src/surquest/fastapi/utils/GCP/tracer.py +++ b/src/surquest/fastapi/utils/GCP/tracer.py @@ -18,13 +18,21 @@ tracer_provider = TracerProvider(sampler=ALWAYS_ON) # always trace trace.set_tracer_provider(tracer_provider) -cloud_trace_exporter = CloudTraceSpanExporter() -tracer_provider.add_span_processor( - # BatchSpanProcessor buffers spans and sends them in batches in a - # background thread. The default parameters are sensible, but can be - # tweaked to optimize your performance - BatchSpanProcessor(cloud_trace_exporter) -) +# cloud_trace_exporter = CloudTraceSpanExporter() +try: + cloud_trace_exporter = CloudTraceSpanExporter() +except Exception as e: + print("CloudTraceSpanExporter not initialized") + print(e) + cloud_trace_exporter = None + +if cloud_trace_exporter is not None: + tracer_provider.add_span_processor( + # BatchSpanProcessor buffers spans and sends them in batches in a + # background thread. The default parameters are sensible, but can be + # tweaked to optimize your performance + BatchSpanProcessor(cloud_trace_exporter) + ) class Tracer: diff --git a/src/surquest/fastapi/utils/auth.py b/src/surquest/fastapi/utils/auth.py new file mode 100644 index 0000000..0fe9c12 --- /dev/null +++ b/src/surquest/fastapi/utils/auth.py @@ -0,0 +1,38 @@ +import typing +from fastapi.security.api_key import APIKeyQuery +from fastapi import HTTPException, Security, Request + + +api_key_query = APIKeyQuery(name="apiKey", auto_error=False) + +async def check_api_key( + request: Request, + api_key_query: str = Security(api_key_query), +): + + # check if app has defined API key + if "api_key" in request.app.extra: + + # check if passed API key is equal to app's API key + if api_key_query == request.app.extra.get("api_key"): + return api_key_query + else: + raise AUTHException() + + +class AUTHException(HTTPException): + def __init__( + self, + status_code: int = 401, # HTTP status code for Unauthorized + detail: typing.Optional[str] = "Unauthorized: Invalid API Key", + loc: typing.Optional[list] = ["query", "apiKey"], + type: typing.Optional[str] = "AUTH.ERROR" + ) -> None: + self.status_code = status_code + self.detail = detail + self.loc = loc + self.type = type + + def __repr__(self) -> str: + class_name = self.__class__.__name__ + return f"{class_name}(status_code={self.status_code!r}, detail={self.detail!r})" \ No newline at end of file