File tree Expand file tree Collapse file tree 4 files changed +55
-9
lines changed
src/surquest/fastapi/utils Expand file tree Collapse file tree 4 files changed +55
-9
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44
55[project ]
66name = " surquest-fastapi-utils"
7- version = " 0.2.1 "
7+ version = " 0.2.3 "
88description = " This project provides collection of utilities for FastAPI framework as: Catcher, Middleware, etc."
99authors = [
1010 {name = " Michal Švarc" , email = " michal.svarc@surquest.com" }
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 1818
1919tracer_provider = TracerProvider (sampler = ALWAYS_ON ) # always trace
2020trace .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
2937class Tracer :
3038
Original file line number Diff line number Diff line change 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} )"
You can’t perform that action at this time.
0 commit comments