2020-07-29 02:19:05 +03:00
|
|
|
"""Giphy client module."""
|
|
|
|
|
|
|
|
from aiohttp import ClientSession, ClientTimeout
|
|
|
|
|
|
|
|
|
|
|
|
class GiphyClient:
|
|
|
|
|
2021-10-01 01:56:29 +03:00
|
|
|
API_URL = "https://api.giphy.com/v1"
|
2020-07-29 02:19:05 +03:00
|
|
|
|
|
|
|
def __init__(self, api_key, timeout):
|
|
|
|
self._api_key = api_key
|
|
|
|
self._timeout = ClientTimeout(timeout)
|
|
|
|
|
|
|
|
async def search(self, query, limit):
|
|
|
|
"""Make search API call and return result."""
|
2021-10-01 01:56:29 +03:00
|
|
|
url = f"{self.API_URL}/gifs/search"
|
2020-07-29 02:19:05 +03:00
|
|
|
params = {
|
2021-10-01 01:56:29 +03:00
|
|
|
"q": query,
|
|
|
|
"api_key": self._api_key,
|
|
|
|
"limit": limit,
|
2020-07-29 02:19:05 +03:00
|
|
|
}
|
|
|
|
async with ClientSession(timeout=self._timeout) as session:
|
|
|
|
async with session.get(url, params=params) as response:
|
|
|
|
if response.status != 200:
|
|
|
|
response.raise_for_status()
|
|
|
|
return await response.json()
|