diff options
| author | 2022-02-07 21:46:10 +0100 | |
|---|---|---|
| committer | 2022-02-07 21:46:10 +0100 | |
| commit | 9cc204b0613a677bb3fec0b7bd357eb2968144ae (patch) | |
| tree | 1d322c5ff9585d8b225c756a16c5babcdf5f3693 | |
| parent | 0332136bfaf22bb1a6a95e8ff998ff0b68e7e2d1 (diff) | |
| download | client-1.2.1.tar.gz client-1.2.1.tar.bz2 client-1.2.1.zip | |
Headers are now a list of tuplesv1.2.1
| -rw-r--r-- | ttun/client.py | 11 | ||||
| -rw-r--r-- | ttun/types.py | 6 |
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 @@ | |||
| 1 | import asyncio | 1 | import asyncio |
| 2 | import json | 2 | import json |
| 3 | from base64 import b64encode, b64decode | 3 | from base64 import b64encode, b64decode |
| 4 | from time import perf_counter, process_time | 4 | from time import perf_counter |
| 5 | from typing import Optional, Callable, Coroutine, Awaitable | 5 | from typing import Optional, Callable, Coroutine, Awaitable |
| 6 | from uuid import uuid4 | 6 | from uuid import uuid4 |
| 7 | 7 | ||
| 8 | import websockets | 8 | import websockets |
| 9 | from aiohttp import ClientSession | 9 | from aiohttp import ClientSession, DummyCookieJar |
| 10 | from websockets import WebSocketClientProtocol | 10 | from websockets import WebSocketClientProtocol |
| 11 | from websockets.exceptions import ConnectionClosed | 11 | from websockets.exceptions import ConnectionClosed |
| 12 | 12 | ||
| 13 | from ttun.pubsub import PubSub | 13 | from ttun.pubsub import PubSub |
| 14 | from ttun.types import Config, RequestData, ResponseData | 14 | from ttun.types import Config, RequestData, ResponseData |
| 15 | from timeit import timeit | ||
| 16 | 15 | ||
| 17 | 16 | ||
| 18 | class Client: | 17 | class 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): | |||
| 8 | class RequestData(TypedDict): | 8 | class 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 | ||
| 16 | class ResponseData(TypedDict): | 15 | class 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 | ||
