aiohttp-try
This commit is contained in:
@@ -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)
|
||||
|
Reference in New Issue
Block a user