From a9f817b05ee7753be070147a987f65534a5ac1cd Mon Sep 17 00:00:00 2001 From: nsde Date: Sun, 13 Aug 2023 00:59:54 +0200 Subject: [PATCH] Removed unused proxy tests and fixed moderation proxy --- api/moderation.py | 2 +- api/proxies.py | 61 ----------------------------------------------- tests/__main__.py | 2 ++ 3 files changed, 3 insertions(+), 62 deletions(-) diff --git a/api/moderation.py b/api/moderation.py index bdc9ff3..d040e8f 100644 --- a/api/moderation.py +++ b/api/moderation.py @@ -36,7 +36,7 @@ Returns True if the message violates the policy, False otherwise. } ) - async with aiohttp.ClientSession(connector=proxies.default_proxy.connector) as session: + async with aiohttp.ClientSession(connector=proxies.get_proxy().connector) as session: try: async with session.request( method=req.get('method', 'POST'), diff --git a/api/proxies.py b/api/proxies.py index 930b5f9..a2da849 100644 --- a/api/proxies.py +++ b/api/proxies.py @@ -103,60 +103,6 @@ class ProxyLists: self.get_random = Proxy(url=random_proxy) self.connector = aiohttp_socks.ChainProxyConnector.from_urls(proxies_in_files) -# ================================================================================================================================ # - -# Proxy tests -# Can be useful if you want to troubleshoot your proxies - -def test_httpx_workaround(): - import httpx - - print(default_proxy.proxies) - - # this workaround solves the RNDS issue, but fails for Cloudflare protected websites - with httpx.Client(proxies=default_proxy.proxies, headers={'Host': 'checkip.amazonaws.com'}) as client: - return client.get( - f'http://{socket.gethostbyname("checkip.amazonaws.com")}/', - ).text.strip() - -def test_requests(): - import requests - - print(default_proxy.proxies) - - return requests.get( - timeout=5, - url='https://checkip.amazonaws.com/', - proxies=default_proxy.urls - ).text.strip() - -async def test_aiohttp_socks(): - async with aiohttp.ClientSession(connector=default_proxy.connector) as session: - async with session.get('https://checkip.amazonaws.com/') as response: - html = await response.text() - return html.strip() - -async def streaming_aiohttp_socks(): - async with aiohttp.ClientSession(connector=default_proxy.connector) as session: - async with session.get('https://httpbin.org/get', headers={ - 'Authorization': 'x' - }) as response: - json = await response.json() - return json - -async def text_httpx_socks(): - import httpx - from httpx_socks import AsyncProxyTransport - - print(default_proxy.url_ip) - - transport = AsyncProxyTransport.from_url(default_proxy.url_ip) - async with httpx.AsyncClient(transport=transport) as client: - res = await client.get('https://checkip.amazonaws.com') - return res.text - -# ================================================================================================================================ # - def get_proxy() -> Proxy: """Returns a Proxy object. The proxy is either from the proxy list or from the environment variables. """ @@ -170,10 +116,3 @@ def get_proxy() -> Proxy: username=os.getenv('PROXY_USER'), password=os.getenv('PROXY_PASS') ) - -if __name__ == '__main__': - # print(test_httpx()) - # print(test_requests()) - # print(asyncio.run(test_aiohttp_socks())) - print(asyncio.run(streaming_aiohttp_socks())) - # print(asyncio.run(text_httpx_socks())) diff --git a/tests/__main__.py b/tests/__main__.py index 02932b3..c4d440b 100644 --- a/tests/__main__.py +++ b/tests/__main__.py @@ -64,6 +64,8 @@ def test_library(): messages=MESSAGES ) + print(completion) + return completion['choices'][0]['message']['content'] def test_library_moderation():