Skip to content

Commit 177b7f0

Browse files
authored
Merge pull request #2 from surquest/fix
removing hard dependency on GCP
2 parents cbb5ff9 + e2b003a commit 177b7f0

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "surquest-fastapi-utils"
7-
version = "0.2.1"
7+
version = "0.2.3"
88
description = "This project provides collection of utilities for FastAPI framework as: Catcher, Middleware, etc."
99
authors = [
1010
{name= "Michal Švarc", email= "michal.svarc@surquest.com"}

src/surquest/fastapi/utils/GCP/catcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,6 @@ async def catch_http_exception(
124124
)
125125

126126
return Response.set(
127-
status_code=404,
127+
status_code=getattr(exc, "status_code", 404),
128128
errors=[message]
129129
)

src/surquest/fastapi/utils/GCP/tracer.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,21 @@
1818

1919
tracer_provider = TracerProvider(sampler=ALWAYS_ON) # always trace
2020
trace.set_tracer_provider(tracer_provider)
21-
cloud_trace_exporter = CloudTraceSpanExporter()
22-
tracer_provider.add_span_processor(
23-
# BatchSpanProcessor buffers spans and sends them in batches in a
24-
# background thread. The default parameters are sensible, but can be
25-
# tweaked to optimize your performance
26-
BatchSpanProcessor(cloud_trace_exporter)
27-
)
21+
# cloud_trace_exporter = CloudTraceSpanExporter()
22+
try:
23+
cloud_trace_exporter = CloudTraceSpanExporter()
24+
except Exception as e:
25+
print("CloudTraceSpanExporter not initialized")
26+
print(e)
27+
cloud_trace_exporter = None
28+
29+
if cloud_trace_exporter is not None:
30+
tracer_provider.add_span_processor(
31+
# BatchSpanProcessor buffers spans and sends them in batches in a
32+
# background thread. The default parameters are sensible, but can be
33+
# tweaked to optimize your performance
34+
BatchSpanProcessor(cloud_trace_exporter)
35+
)
2836

2937
class Tracer:
3038

src/surquest/fastapi/utils/auth.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import typing
2+
from fastapi.security.api_key import APIKeyQuery
3+
from fastapi import HTTPException, Security, Request
4+
5+
6+
api_key_query = APIKeyQuery(name="apiKey", auto_error=False)
7+
8+
async def check_api_key(
9+
request: Request,
10+
api_key_query: str = Security(api_key_query),
11+
):
12+
13+
# check if app has defined API key
14+
if "api_key" in request.app.extra:
15+
16+
# check if passed API key is equal to app's API key
17+
if api_key_query == request.app.extra.get("api_key"):
18+
return api_key_query
19+
else:
20+
raise AUTHException()
21+
22+
23+
class AUTHException(HTTPException):
24+
def __init__(
25+
self,
26+
status_code: int = 401, # HTTP status code for Unauthorized
27+
detail: typing.Optional[str] = "Unauthorized: Invalid API Key",
28+
loc: typing.Optional[list] = ["query", "apiKey"],
29+
type: typing.Optional[str] = "AUTH.ERROR"
30+
) -> None:
31+
self.status_code = status_code
32+
self.detail = detail
33+
self.loc = loc
34+
self.type = type
35+
36+
def __repr__(self) -> str:
37+
class_name = self.__class__.__name__
38+
return f"{class_name}(status_code={self.status_code!r}, detail={self.detail!r})"

0 commit comments

Comments
 (0)