nova-api/api/netclient.py

35 lines
851 B
Python
Raw Normal View History

2023-07-25 02:42:53 +02:00
import os
import httpx
2023-07-25 19:45:21 +02:00
import proxies
2023-07-25 02:42:53 +02:00
from dotenv import load_dotenv
2023-08-01 02:38:55 +02:00
from helpers.requesting import Request
2023-07-25 02:42:53 +02:00
load_dotenv()
2023-07-25 19:45:21 +02:00
async def stream_closedai_request(request: Request):
async with httpx.AsyncClient(
# proxies=proxies.default_proxy.urls_httpx,
timeout=httpx.Timeout(request.timeout)
) as client:
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {os.getenv("CLOSEDAI_KEY")}'
}
response = await client.request(
2023-07-25 19:45:21 +02:00
method=request.method,
url=request.url,
json=request.payload,
headers=headers
)
response.raise_for_status()
async for chunk in response.aiter_bytes():
chunk = f'{chunk.decode("utf8")}\n\n'
yield chunk
2023-07-25 19:45:21 +02:00
if __name__ == '__main__':
pass