Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add example of Modify (redefine) response only to particular endpoint #37

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- [#37](https://github.com/WSH032/fastapi-proxy-lib/pull/37) - docs: add example of `Modify (redefine) response only to particular endpoint`. Thanks [@pavelsr](https://github.com/pavelsr)!

### Changed

- [#30](https://github.com/WSH032/fastapi-proxy-lib/pull/30) - fix(internal): use `websocket` in favor of `websocket_route`. Thanks [@WSH032](https://github.com/WSH032)!
Expand Down
32 changes: 32 additions & 0 deletions docs/Usage/Advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,35 @@ async def _(request: Request, path: str = ""):
```

visit `/`, you will notice that the response body is printed to the console.


## Modify (redefine) response only to particular endpoint

```python
from fastapi import FastAPI
from starlette.requests import Request
from fastapi_proxy_lib.core.http import ReverseHttpProxy

app = FastAPI()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps adding lifespan would be better?

app = FastAPI(lifespan=close_proxy_event)

proxy = ReverseHttpProxy(base_url="https://httpbin.org/")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


@app.get("/{path:path}")
async def _(request: Request, path: str = ""):
if path == "ip" and request.method == "GET":
Comment on lines +132 to +135
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add @app.post("/{path:path}") here? Because I see below there's a check for request.method == "GET".

return { "msg": "Method is redefined"}
else:
proxy_response = await proxy.proxy(request=request, path=path)
return proxy_response

if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
```

In this example all requests except `GET /ip` will be passed to `httpbin.org`:

```python
python3 test.py # Uvicorn running on http://0.0.0.0:8000
curl http://127.0.0.0:8000/user-agent # { "user-agent": "curl/7.81.0" }
curl http://127.0.0.0:8000/ip # {"msg":"Method is redefined"}
```