Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
0204c1a
Some base work for the API conversion
VottonDev May 9, 2022
38c3294
More ctx stuff
VottonDev May 9, 2022
05a5319
Remove awaits as not async functions
VottonDev May 23, 2022
0e95a28
Test file
VottonDev May 23, 2022
b60fd8d
Move cog functionality to announce_message
VottonDev May 23, 2022
2932f03
Base api.py for announce cog
VottonDev Jun 26, 2022
684b4a6
Fix indenting problems and add a test file
VottonDev Jul 3, 2022
bb228dc
Import commands
VottonDev Jul 3, 2022
b78ba2e
Add missing imports
VottonDev Jul 10, 2022
ebb2185
Add some API paths
VottonDev Jul 10, 2022
4639702
Add more API routes
VottonDev Jul 11, 2022
ac14066
Remove duplicate endpoint
VottonDev Jul 17, 2022
f721063
Merge branch 'master' into feature/announce_api_cog
VottonDev Sep 12, 2022
9b92096
Remove a unneeded file
VottonDev Sep 12, 2022
977cc00
Merge branch 'master' into feature/announce_api_cog
VottonDev Oct 15, 2022
7dadc18
Fix import formatting
VottonDev Oct 15, 2022
3f2032f
Try fix imports and add API paths
VottonDev Oct 17, 2022
9305ce0
Add a missing .dot for import
VottonDev Oct 17, 2022
4b501b9
Remove a unneeded import
VottonDev Oct 17, 2022
dec3496
Merge branch 'master' into feature/announce_api_cog
VottonDev Oct 17, 2022
ea320ec
Add import Announce to init
VottonDev Oct 17, 2022
6fda91c
Add missing setup to init
VottonDev Oct 17, 2022
8aa0dd3
Remove unneeded import
VottonDev Oct 17, 2022
edccd46
Reformat the cog to original
VottonDev Oct 17, 2022
44c916e
Try re-adding other API modules
VottonDev Oct 17, 2022
f3022bd
Fix API route by identation
VottonDev Oct 17, 2022
03c09df
Revert init file to see if it works
VottonDev Oct 17, 2022
2a5b644
Try add api as a import to init
VottonDev Oct 17, 2022
bd620aa
Improve the import for cog and api init
VottonDev Oct 17, 2022
0d2ef60
Add a conftest
VottonDev Oct 17, 2022
2bb83c6
Test removing setup import
VottonDev Oct 17, 2022
6611177
Remove load all cogs from test_core
VottonDev Oct 17, 2022
5e9b48b
Try testing announce_cog_enabled
VottonDev Oct 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions koala/cogs/announce/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from . import utils, db, log, models
from .cog import Announce, setup
from .announce_message import AnnounceMessage
from . import api, cog, db, log, models, utils
from .announce_message import AnnounceMessage
from .cog import Announce


def setup(bot):
cog.setup(bot)
api.setup(bot)
56 changes: 56 additions & 0 deletions koala/cogs/announce/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Futures
# Built-in/Generic Imports
# Libs
from http.client import BAD_REQUEST, CREATED, OK

import discord
from aiohttp import web
from discord.ext.commands import Bot

from koala.rest.api import build_response, parse_request
from koala.utils import convert_iso_datetime

# Own modules
from . import cog
from .log import logger

# Constants
ANNOUNCE_ENDPOINT = 'announce'

ANNOUNCE_STATUS= 'announce_status' # GET


class AnnounceEndpoint:
"""
The API endpoints for AnnounceCog
"""

def __init__(self, bot):
self._bot = bot

def register(self, app):
"""
Register the routes for the given application
todo: review aiohttp 'views' and see if they are a better idea
:param app: The aiohttp.web.Application (likely of the sub app)
:return: app
"""
app.add_routes([web.get('/{endpoint}'.format(endpoint=ANNOUNCE_STATUS), self.get_announce_is_enabled)])
return app


@parse_request
async def get_announce_is_enabled(guild):
return await cog.announce_is_enabled(guild)


def setup(bot: Bot):
"""
Load this cog to the KoalaBot.
:param bot: the bot client for KoalaBot
"""
sub_app = web.Application()
endpoint = AnnounceEndpoint(bot)
endpoint.register(sub_app)
getattr(bot, "koala_web_app").add_subapp('/{extension}'.format(extension=ANNOUNCE_ENDPOINT), sub_app)
logger.info("Announce API is ready.")
4 changes: 3 additions & 1 deletion koala/cogs/announce/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
from koala.colours import KOALA_GREEN
from koala.db import insert_extension
from koala.utils import extract_id, wait_for_message

from .announce_message import AnnounceMessage
from .db import AnnounceDBManager
from .log import logger
from .utils import ANNOUNCE_SEPARATION_DAYS, SECONDS_IN_A_DAY, MAX_MESSAGE_LENGTH
from .utils import (ANNOUNCE_SEPARATION_DAYS, MAX_MESSAGE_LENGTH,
SECONDS_IN_A_DAY)


def announce_is_enabled(ctx):
Expand Down
1 change: 1 addition & 0 deletions koala/cogs/announce/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Own modules
from koala.db import session_manager

from .models import GuildUsage

# Libs
Expand Down
2 changes: 1 addition & 1 deletion koala/cogs/announce/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy import Column, ForeignKey, Integer

from koala.db import setup
from koala.models import mapper_registry
Expand Down
13 changes: 13 additions & 0 deletions tests/cogs/announce/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest
from aiohttp import web
from discord.ext.commands import Bot

from koala.cogs.announce.api import AnnounceEndpoint


@pytest.fixture(autouse=True)
def setup_attributes(bot: Bot):
app = web.Application()
endpoint = AnnounceEndpoint(bot)
endpoint.register(app)
setattr(bot, "koala_web_app", app)
25 changes: 25 additions & 0 deletions tests/cogs/announce/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from http.client import BAD_REQUEST, CREATED, OK, UNPROCESSABLE_ENTITY

# Libs
import discord
import pytest
from aiohttp import web

import koalabot
from koala.cogs.announce.api import AnnounceEndpoint
from koala.db import get_all_available_guild_extensions
from koala.rest.api import parse_request


@pytest.fixture
def api_client(bot: discord.ext.commands.Bot, aiohttp_client, loop ):
app = web.Application()
endpoint = AnnounceEndpoint(bot)
app = endpoint.register(app)
return loop.run_until_complete(aiohttp_client(app))

# Check if announce cog is enabled
async def test_is_announce_cog_enabled(api_client):
resp = await api_client.get('/announce_status?guild=1')
assert resp.status == OK
assert await resp.json() == True
1 change: 0 additions & 1 deletion tests/cogs/base/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ async def test_list_enabled_extensions(bot: commands.Bot):
@mock.patch("koalabot.ENABLED_COGS", ["announce"])
@pytest.mark.asyncio
async def test_get_extensions(bot: commands.Bot):
koalabot.load_all_cogs()
guild: discord.Guild = dpytest.get_config().guilds[0]
resp = core.get_all_available_guild_extensions(guild.id)
print(resp)
Expand Down