diff options
| author | 2020-02-29 13:04:39 +0200 | |
|---|---|---|
| committer | 2020-02-29 13:04:39 +0200 | |
| commit | 1b3f2746724f0f2c30c531080b0d8ee5ae85d633 (patch) | |
| tree | 7d4f3e057cea18bd972079a5e8603fb57adbc7c8 | |
| parent | (Space Cog): Added NASA API base URL and .apod command HTTP request default p... (diff) | |
(Space Cog): Added fetch_from_nasa function to space.py, what do request to NASA API and support arguments endpoint and params.
| -rw-r--r-- | bot/seasons/evergreen/space.py | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/bot/seasons/evergreen/space.py b/bot/seasons/evergreen/space.py index a4f60041..7d471d47 100644 --- a/bot/seasons/evergreen/space.py +++ b/bot/seasons/evergreen/space.py @@ -1,4 +1,6 @@ import logging +from typing import Any, Dict +from urllib.parse import urlencode from discord.ext.commands import Cog @@ -25,6 +27,12 @@ class Space(Cog): self.bot = bot self.http_session = bot.http_session + async def fetch_from_nasa(self, endpoint: str, params: Dict[str, Any]) -> Dict[str, Any]: + """Fetch information from NASA API, return result.""" + # Generate request URL from base URL, endpoint and parsed params + async with self.http_session.get(url=f"{BASE_URL}{endpoint}?{urlencode(params)}") as resp: + return await resp.json() + def setup(bot: SeasonalBot) -> None: """Load Space Cog.""" |