Fix http helper

This commit is contained in:
Yasir Aris M 2024-08-08 10:30:13 +07:00 committed by GitHub
parent efe8905ef1
commit 8837cdeed0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,14 +1,9 @@
from asyncio import gather from asyncio import gather
from aiohttp import ClientSession
from httpx import AsyncClient, Timeout from httpx import AsyncClient, Timeout
# Aiohttp Async Client
# session = ClientSession()
# HTTPx Async Client # HTTPx Async Client
fetch = AsyncClient( fetch = AsyncClient(
http2=True, https2=True
verify=False, verify=False,
headers={ headers={
"Accept-Language": "en-US,en;q=0.9,id-ID;q=0.8,id;q=0.7", "Accept-Language": "en-US,en;q=0.9,id-ID;q=0.8,id;q=0.7",
@ -19,8 +14,8 @@ fetch = AsyncClient(
async def get(url: str, *args, **kwargs): async def get(url: str, *args, **kwargs):
async with session.get(url, *args, **kwargs) as resp:
try: try:
resp = await fetch.get(url, *args, **kwargs)
data = await resp.json() data = await resp.json()
except Exception: except Exception:
data = await resp.text() data = await resp.text()
@ -28,8 +23,8 @@ async def get(url: str, *args, **kwargs):
async def head(url: str, *args, **kwargs): async def head(url: str, *args, **kwargs):
async with session.head(url, *args, **kwargs) as resp:
try: try:
resp = await fetch.head(url, *args, **kwargs)
data = await resp.json() data = await resp.json()
except Exception: except Exception:
data = await resp.text() data = await resp.text()
@ -37,8 +32,8 @@ async def head(url: str, *args, **kwargs):
async def post(url: str, *args, **kwargs): async def post(url: str, *args, **kwargs):
async with session.post(url, *args, **kwargs) as resp:
try: try:
resp = await fetch.post(url, *args, **kwargs)
data = await resp.json() data = await resp.json()
except Exception: except Exception:
data = await resp.text() data = await resp.text()
@ -58,8 +53,8 @@ async def multipost(url: str, times: int, *args, **kwargs):
async def resp_get(url: str, *args, **kwargs): async def resp_get(url: str, *args, **kwargs):
return await session.get(url, *args, **kwargs) return await fetch.get(url, *args, **kwargs)
async def resp_post(url: str, *args, **kwargs): async def resp_post(url: str, *args, **kwargs):
return await session.post(url, *args, **kwargs) return await fetch.post(url, *args, **kwargs)