summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <tom@vanderlee.io>2022-02-07 21:46:10 +0100
committerGravatar Tom van der Lee <tom@vanderlee.io>2022-02-07 21:46:10 +0100
commit9cc204b0613a677bb3fec0b7bd357eb2968144ae (patch)
tree1d322c5ff9585d8b225c756a16c5babcdf5f3693
parent0332136bfaf22bb1a6a95e8ff998ff0b68e7e2d1 (diff)
downloadclient-9cc204b0613a677bb3fec0b7bd357eb2968144ae.tar.gz
client-9cc204b0613a677bb3fec0b7bd357eb2968144ae.tar.bz2
client-9cc204b0613a677bb3fec0b7bd357eb2968144ae.zip
Headers are now a list of tuplesv1.2.1
-rw-r--r--ttun/client.py11
-rw-r--r--ttun/types.py6
2 files changed, 6 insertions, 11 deletions
diff --git a/ttun/client.py b/ttun/client.py
index eac40da..b4be4e7 100644
--- a/ttun/client.py
+++ b/ttun/client.py
@@ -1,18 +1,17 @@
1import asyncio 1import asyncio
2import json 2import json
3from base64 import b64encode, b64decode 3from base64 import b64encode, b64decode
4from time import perf_counter, process_time 4from time import perf_counter
5from typing import Optional, Callable, Coroutine, Awaitable 5from typing import Optional, Callable, Coroutine, Awaitable
6from uuid import uuid4 6from uuid import uuid4
7 7
8import websockets 8import websockets
9from aiohttp import ClientSession 9from aiohttp import ClientSession, DummyCookieJar
10from websockets import WebSocketClientProtocol 10from websockets import WebSocketClientProtocol
11from websockets.exceptions import ConnectionClosed 11from websockets.exceptions import ConnectionClosed
12 12
13from ttun.pubsub import PubSub 13from ttun.pubsub import PubSub
14from ttun.types import Config, RequestData, ResponseData 14from ttun.types import Config, RequestData, ResponseData
15from timeit import timeit
16 15
17 16
18class Client: 17class Client:
@@ -69,7 +68,7 @@ class Client:
69 break 68 break
70 69
71 async def proxyRequest(self, request: RequestData, on_response: Callable[[ResponseData], Awaitable] = None): 70 async def proxyRequest(self, request: RequestData, on_response: Callable[[ResponseData], Awaitable] = None):
72 async with ClientSession() as session: 71 async with ClientSession(cookie_jar=DummyCookieJar()) as session:
73 request_id = uuid4() 72 request_id = uuid4()
74 await PubSub.publish({ 73 await PubSub.publish({
75 "type": "request", 74 "type": "request",
@@ -84,7 +83,6 @@ class Client:
84 method=request['method'], 83 method=request['method'],
85 url=f'http://localhost:{self.port}{request["path"]}', 84 url=f'http://localhost:{self.port}{request["path"]}',
86 headers=request['headers'], 85 headers=request['headers'],
87 cookies=request['cookies'],
88 data=b64decode(request['body'].encode()), 86 data=b64decode(request['body'].encode()),
89 allow_redirects=False 87 allow_redirects=False
90 ) 88 )
@@ -92,8 +90,7 @@ class Client:
92 90
93 response_data = ResponseData( 91 response_data = ResponseData(
94 status=response.status, 92 status=response.status,
95 headers=dict(response.headers), 93 headers=list(response.headers.items()),
96 cookies=dict(response.cookies),
97 body=b64encode(await response.read()).decode() 94 body=b64encode(await response.read()).decode()
98 ) 95 )
99 96
diff --git a/ttun/types.py b/ttun/types.py
index cf94b9e..640ac5d 100644
--- a/ttun/types.py
+++ b/ttun/types.py
@@ -8,15 +8,13 @@ class Message(TypedDict):
8class RequestData(TypedDict): 8class RequestData(TypedDict):
9 method: str 9 method: str
10 path: str 10 path: str
11 headers: dict 11 headers: list[tuple[str, str]]
12 cookies: dict
13 body: Optional[str] 12 body: Optional[str]
14 13
15 14
16class ResponseData(TypedDict): 15class ResponseData(TypedDict):
17 status: int 16 status: int
18 headers: dict 17 headers: list[tuple[str, str]]
19 cookies: dict
20 body: Optional[str] 18 body: Optional[str]
21 19
22 20