-
Notifications
You must be signed in to change notification settings - Fork 2
/
proxy_session.py
43 lines (31 loc) · 1.18 KB
/
proxy_session.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
35
36
37
38
39
40
41
42
43
import json
from base64 import b64decode, b64encode
import requests
class ProxiedSession(requests.Session):
"""A session that sends requests through a proxy
WARNING: ONLY USE FOR LOCAL TESTING NOT FOR SERVICES EXPOSED TO THE WORLD!
See esteid-proxy/README.md for more information
"""
def send(self, request, **kwargs):
# Use a proxy for the request, so we create a new one and put all the params inside the body
PROXY_URL = "http://localhost:8001"
new_request = requests.Request(
method="POST",
url=PROXY_URL,
json={
"httpMethod": request.method,
"url": request.url,
"headers": dict(request.headers),
"body": b64encode(request.body).decode("utf-8") if request.body else None,
},
headers={
"proxy-token": "yolo",
"Content-Type": "application/json",
},
)
new_request = new_request.prepare()
result = super().send(new_request, **kwargs)
print("RESULT", result.status_code)
return result
def proxied_get_request_session():
return ProxiedSession()