-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
34 lines (27 loc) · 1.13 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import requests
from typing import Optional
from fastapi import FastAPI, Request, Response
from fastapi.responses import Response as Res
import json
app = FastAPI()
@app.api_route("/proxer/{rest:path}", methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"])
def proxer(rest: str, response: Response, request: Request, payload: Optional[dict] = None):
h = dict(request.headers)
if h.get('host'):
del h['host']
call = getattr(requests, request.method.lower())
rest = rest + '?' + str(request.query_params)
print(h)
if payload:
# TODO: magari fare per metodo (anzi: sicuramente), comunque cosi ci funzia solo per json (magari conviene davvero farlo piu a basso livello, via socket?)
r = call(url=rest, data=json.dumps(payload),
headers=h, stream=True)
else:
r = call(url=rest, headers=h, stream=True)
h = r.headers
for key, value in h.items():
response.headers[key] = value
response.headers['Access-Control-Allow-Origin'] = '*'
r = Res(headers=response.headers, content=r.raw.data,
status_code=r.status_code)
return r