API token as environment variable
can be included in .env as API_TOKEN
This commit is contained in:
parent
8c35f42a0e
commit
c785e5eeed
@ -26,10 +26,10 @@ class API:
|
||||
RSS_FEED_ENDPOINT = API_ENDPOINT + "rssfeed/"
|
||||
FEED_CHANNEL_ENDPOINT = API_ENDPOINT + "feedchannel/"
|
||||
|
||||
def __init__(self, session: aiohttp.ClientSession):
|
||||
def __init__(self, api_token: str, session: aiohttp.ClientSession):
|
||||
log.debug("API session initialised")
|
||||
self.session = session
|
||||
self.token_headers = {"Authorization": f"Token 12bccad74fb8575b3242902014f8f3807016f4fe"}
|
||||
self.token_headers = {"Authorization": f"Token {api_token}"}
|
||||
|
||||
async def fetch_data(self, url: str, params=None):
|
||||
log.debug("api fetching from url: %s", url)
|
||||
|
@ -17,12 +17,13 @@ log = logging.getLogger(__name__)
|
||||
|
||||
class DiscordBot(commands.Bot):
|
||||
|
||||
def __init__(self, BASE_DIR: Path, developing: bool):
|
||||
def __init__(self, BASE_DIR: Path, developing: bool, api_token: str):
|
||||
activity = Game("Indev") if developing else None
|
||||
super().__init__(command_prefix="-", intents=Intents.all(), activity=activity)
|
||||
self.functions = Functions(self)
|
||||
self.functions = Functions(self, api_token)
|
||||
self.BASE_DIR = BASE_DIR
|
||||
self.developing = developing
|
||||
self.api_token = api_token
|
||||
|
||||
log.info("developing=%s", developing)
|
||||
|
||||
|
11
src/feed.py
11
src/feed.py
@ -221,8 +221,9 @@ class RSSFeed:
|
||||
|
||||
class Functions:
|
||||
|
||||
def __init__(self, bot):
|
||||
def __init__(self, bot, api_token: str):
|
||||
self.bot = bot
|
||||
self.api_token = api_token
|
||||
|
||||
async def validate_feed(self, nickname: str, url: str) -> FeedParserDict:
|
||||
"""Validates a feed based on the given nickname and url.
|
||||
@ -284,7 +285,9 @@ class Functions:
|
||||
source = Source.from_parsed(parsed_feed)
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
data = await API(session).create_new_rssfeed(name, url, source.icon_url, guild_id)
|
||||
data = await API(self.api_token, session).create_new_rssfeed(
|
||||
name, url, source.icon_url, guild_id
|
||||
)
|
||||
|
||||
return RSSFeed.from_dict(data)
|
||||
|
||||
@ -293,7 +296,7 @@ class Functions:
|
||||
log.info("Deleting Feed '%s'", uuid)
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
api = API(session)
|
||||
api = API(self.api_token, session)
|
||||
data = await api.get_rssfeed(uuid)
|
||||
await api.delete_rssfeed(uuid)
|
||||
|
||||
@ -310,7 +313,7 @@ class Functions:
|
||||
"""
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
data, count = await API(session).get_rssfeed_list(
|
||||
data, count = await API(self.api_token, session).get_rssfeed_list(
|
||||
discord_server_id=guild_id,
|
||||
page=page,
|
||||
page_size=pagesize
|
||||
|
20
src/main.py
20
src/main.py
@ -9,7 +9,7 @@ from os import getenv
|
||||
from pathlib import Path
|
||||
|
||||
# it's important to load environment variables before
|
||||
# importing the packages that depend on them.
|
||||
# importing the modules that depend on them.
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
|
||||
@ -26,12 +26,17 @@ async def main():
|
||||
|
||||
# Grab the token before anything else, because if there is no token
|
||||
# available then the bot cannot be started anyways.
|
||||
token = getenv("BOT_TOKEN")
|
||||
bot_token = getenv("BOT_TOKEN")
|
||||
if not bot_token:
|
||||
raise ValueError("Bot Token is empty")
|
||||
|
||||
if not token:
|
||||
raise ValueError("Token is empty")
|
||||
# ^ same story for the API token. Without it the API cannot be
|
||||
# interacted with, so grab it first.
|
||||
api_token = getenv("API_TOKEN")
|
||||
if not api_token:
|
||||
raise ValueError("API Token is empty")
|
||||
|
||||
developing = bool(getenv("DEVELOPING"))
|
||||
developing = bool(getenv("DEVELOPING", False))
|
||||
|
||||
# Setup logging settings and mute spammy loggers
|
||||
logsetup = LogSetup(BASE_DIR)
|
||||
@ -41,9 +46,10 @@ async def main():
|
||||
level=logging.WARNING
|
||||
)
|
||||
|
||||
async with DiscordBot(BASE_DIR, developing=developing) as bot:
|
||||
|
||||
async with DiscordBot(BASE_DIR, developing=developing, api_token=api_token) as bot:
|
||||
await bot.load_extensions()
|
||||
await bot.start(token, reconnect=True)
|
||||
await bot.start(bot_token, reconnect=True)
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
Loading…
x
Reference in New Issue
Block a user