33import logging
44import time
55from dataclasses import dataclass
6- from typing import Optional , Annotated
6+ from typing import Annotated , Optional
77
8- from cql2 import Expr
98import httpx
10- from fastapi import Request , Depends
9+ from cql2 import Expr
10+ from fastapi import Depends , Request
1111from starlette .background import BackgroundTask
1212from starlette .datastructures import MutableHeaders
1313from starlette .responses import StreamingResponse
1414
15- from ..utils import update_qs
15+ from .. import utils
1616
1717logger = logging .getLogger (__name__ )
1818
@@ -33,18 +33,18 @@ def __post_init__(self):
3333 timeout = httpx .Timeout (timeout = 15.0 ),
3434 )
3535
36- self . proxy_request . __annotations__ [ "collections_filter" ] = Annotated [
37- Optional [ Expr ], Depends ( self .collections_filter . dependency )
38- ]
39- self . stream . __annotations__ [ "collections_filter" ] = Annotated [
40- Optional [ Expr ], Depends (self .collections_filter . dependency )
41- ]
36+ # Update annotations to support FastAPI's dependency injection
37+ for endpoint in [ self . proxy_request , self .stream ]:
38+ endpoint . __annotations__ [ "collections_filter" ] = Annotated [
39+ Optional [ Expr ],
40+ Depends (getattr ( self .collections_filter , " dependency" , lambda : None )),
41+ ]
4242
4343 async def proxy_request (
4444 self ,
4545 request : Request ,
4646 * ,
47- collections_filter : Annotated [Optional [Expr ], Depends (...)],
47+ collections_filter : Annotated [Optional [Expr ], Depends (...)] = None ,
4848 stream = False ,
4949 ) -> httpx .Response :
5050 """Proxy a request to the upstream STAC API."""
@@ -53,24 +53,22 @@ async def proxy_request(
5353 headers .setdefault ("X-Forwarded-Host" , request .url .hostname )
5454
5555 path = request .url .path
56- query = request .url .query . encode ( "utf-8" )
56+ query = request .url .query
5757
58- # https://github.com/fastapi/fastapi/discussions/7382#discussioncomment-5136466
59- # TODO: Examine filters
60- if collections_filter :
58+ if utils .is_collection_endpoint (path ) and collections_filter :
6159 if request .method == "GET" and path == "/collections" :
62- query += b"&" + update_qs (
63- request .query_params , filter = collections_filter .to_text ()
64- )
65-
66- url = httpx .URL (
67- path = path ,
68- query = query ,
69- )
60+ query = utils .insert_filter (qs = query , filter = collections_filter )
61+ elif utils .is_item_endpoint (path ) and self .items_filter :
62+ if request .method == "GET" :
63+ query = utils .insert_filter (qs = query , filter = self .items_filter )
7064
65+ # https://github.com/fastapi/fastapi/discussions/7382#discussioncomment-5136466
7166 rp_req = self .client .build_request (
7267 request .method ,
73- url = url ,
68+ url = httpx .URL (
69+ path = path ,
70+ query = query .encode ("utf-8" ),
71+ ),
7472 headers = headers ,
7573 content = request .stream (),
7674 )
0 commit comments