@@ -327,8 +327,8 @@ async def close_proxy_event(_: FastAPI) -> AsyncIterator[None]: # (1)!
327327 app = FastAPI(lifespan=close_proxy_event)
328328
329329 @app.get("/{path:path}") # (2)!
330- async def _(request: Request, path: str = "" ):
331- return await proxy.proxy(request=request, path=path ) # (3)!
330+ async def _(request: Request):
331+ return await proxy.proxy(request=request) # (3)!
332332
333333 # Then run shell: `uvicorn <your.py>:app --host http://127.0.0.1:8000 --port 8000`
334334 # visit the app: `http://127.0.0.1:8000/`
@@ -339,10 +339,6 @@ async def _(request: Request, path: str = ""):
339339 2. `{path:path}` is the key.<br>
340340 It allows the app to accept all path parameters.<br>
341341 visit <https://www.starlette.io/routing/#path-parameters> for more info.
342- 3. !!! info
343- In fact, you only need to pass the `request: Request` argument.<br>
344- `fastapi_proxy_lib` can automatically get the `path` from `request`.<br>
345- Explicitly pointing it out here is just to remind you not to forget to specify `{path:path}`.
346342 '''
347343
348344 client : httpx .AsyncClient
@@ -376,25 +372,20 @@ def __init__(
376372
377373 @override
378374 async def proxy ( # pyright: ignore [reportIncompatibleMethodOverride]
379- self , * , request : StarletteRequest , path : Optional [ str ] = None
375+ self , * , request : StarletteRequest
380376 ) -> StarletteResponse :
381377 """Send request to target server.
382378
383379 Args:
384380 request: `starlette.requests.Request`
385- path: The path params of request, which means the path params of base url.<br>
386- If None, will get it from `request.path_params`.<br>
387- **Usually, you don't need to pass this argument**.
388381
389382 Returns:
390383 The response from target server.
391384 """
392385 base_url = self .base_url
393386
394387 # 只取第一个路径参数。注意,我们允许没有路径参数,这代表直接请求
395- path_param : str = (
396- path if path is not None else next (iter (request .path_params .values ()), "" )
397- )
388+ path_param : str = next (iter (request .path_params .values ()), "" )
398389
399390 # 将路径参数拼接到目标url上
400391 # e.g: "https://www.example.com/p0/" + "p1"
@@ -462,8 +453,8 @@ async def close_proxy_event(_: FastAPI) -> AsyncIterator[None]:
462453 app = FastAPI(lifespan=close_proxy_event)
463454
464455 @app.get("/{path:path}")
465- async def _(request: Request, path: str = "" ):
466- return await proxy.proxy(request=request, path=path )
456+ async def _(request: Request):
457+ return await proxy.proxy(request=request)
467458
468459 # Then run shell: `uvicorn <your.py>:app --host http://127.0.0.1:8000 --port 8000`
469460 # visit the app: `http://127.0.0.1:8000/http://www.example.com`
@@ -502,25 +493,20 @@ async def proxy( # pyright: ignore [reportIncompatibleMethodOverride]
502493 self ,
503494 * ,
504495 request : StarletteRequest ,
505- path : Optional [str ] = None ,
506496 ) -> StarletteResponse :
507497 """Send request to target server.
508498
509499 Args:
510500 request: `starlette.requests.Request`
511- path: The path params of request, which means the full url of target server.<br>
512- If None, will get it from `request.path_params`.<br>
513- **Usually, you don't need to pass this argument**.
514501
515502 Returns:
516503 The response from target server.
517504 """
518505 proxy_filter = self .proxy_filter
519506
520507 # 只取第一个路径参数
521- path_param : str = (
522- next (iter (request .path_params .values ()), "" ) if path is None else path
523- )
508+ path_param : str = next (iter (request .path_params .values ()), "" )
509+
524510 # 如果没有路径参数,即在正向代理中未指定目标url,则返回400
525511 if path_param == "" :
526512 error = _BadTargetUrlError ("Must provide target url." )
0 commit comments