Skip to content

Commit e750f5b

Browse files
authored
Merge pull request #11 from tbobm/feat/add-http-api-dump
Add HTTP API dump
2 parents 8c39bcd + 8bb0ad4 commit e750f5b

File tree

5 files changed

+371
-0
lines changed

5 files changed

+371
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ $ curl localhost:8000/health
1919
OK
2020
```
2121

22+
#### http-dump
23+
24+
Path: [`./api/http/dump/`](./api/http/dump/)
25+
26+
Example usage:
27+
```console
28+
$ docker run -p 8000:8000 -d --rm ghcr.io/tbobm/apps:api-http-dump-latest
29+
$ curl localhost:8000/any/path -H "example-header: any-value"
30+
{"path":"any/path","method":"GET","headers":{"host":"localhost:8000","user-agent":"curl/7.81.0","accept":"*/*","example-header":"any-value"},"form":{}}
31+
```
32+
2233
## SQS
2334

2435
### Producer

api/http/dump/Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM python:3.12-slim as release
2+
3+
WORKDIR /app
4+
5+
EXPOSE 8000
6+
RUN pip install poetry
7+
# NOTE: could implement healthcheck
8+
9+
# install dependencies
10+
COPY . .
11+
12+
RUN poetry install
13+
14+
ENTRYPOINT [ "poetry", "run", "uvicorn", "dump.main:app", "--host", "0.0.0.0" ]

api/http/dump/dump/main.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import Union
2+
3+
from fastapi import FastAPI, Request
4+
5+
app = FastAPI()
6+
7+
8+
@app.api_route("/{full_path:path}", methods=["HEAD", "GET", "POST", "DELETE", "PUT"])
9+
async def read_root(request: Request, full_path: str):
10+
response = {
11+
"path": full_path,
12+
"method": request.method,
13+
}
14+
response['headers'] = request.headers
15+
_json = None
16+
try:
17+
_json = await request.json()
18+
if _json is not None:
19+
response['json'] = _json
20+
except Exception:
21+
# no json payload
22+
pass
23+
_form = await request.form()
24+
if _form is not None:
25+
response['form'] = _form
26+
return response

0 commit comments

Comments
 (0)