aiohttp-try

This commit is contained in:
2023-11-29 10:23:41 +03:00
parent 36ab83d02f
commit 63eb952655
9 changed files with 77 additions and 68 deletions

View File

@@ -1,6 +1,6 @@
import asyncio
import json
import httpx
import aiohttp
from services.rediscache import redis
from orm.shout import Shout
@@ -20,13 +20,13 @@ class SearchService:
cached = await redis.execute("GET", text)
if not cached:
async with SearchService.lock:
# Use httpx to send a request to ElasticSearch
async with httpx.AsyncClient() as client:
# Use aiohttp to send a request to ElasticSearch
async with aiohttp.ClientSession() as session:
search_url = f"https://search.discours.io/search?q={text}"
response = await client.get(search_url)
if response.status_code == 200:
payload = response.json()
await redis.execute("SET", text, payload)
return json.loads(payload)
async with session.get(search_url) as response:
if response.status == 200:
payload = await response.json()
await redis.execute("SET", text, json.dumps(payload))
return payload
else:
return json.loads(cached)