Skip to content

Commit 9150cd8

Browse files
authored
Merge pull request #398 from KoalaBotUK/fix/base-cog-api
fix: base cog api now works
2 parents b7822ad + 21aa650 commit 9150cd8

File tree

6 files changed

+75
-72
lines changed

6 files changed

+75
-72
lines changed

koala/cogs/base/api.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,14 @@ async def get_version(self):
130130
return core.get_version()
131131

132132
@parse_request
133-
async def post_load_cog(self, extension, package):
133+
async def post_load_cog(self, extension):
134134
"""
135135
Loads a cog from the cogs folder
136136
:param extension: name of the cog
137-
:param package: package of the cogs
138137
:return:
139138
"""
140139
try:
141-
await core.load_cog(self._bot, extension, package)
140+
await core.load_cog(self._bot, extension)
142141
except BaseException as e:
143142
error = 'Error loading cog: {}'.format(handleActivityError(e))
144143
logger.error(error)
@@ -147,15 +146,14 @@ async def post_load_cog(self, extension, package):
147146
return {'message': 'Cog loaded'}
148147

149148
@parse_request
150-
async def post_unload_cog(self, extension, package):
149+
async def post_unload_cog(self, extension):
151150
"""
152151
Unloads a cog from the cogs folder
153152
:param extension: name of the cog
154-
:param package: package of the cogs
155153
:return:
156154
"""
157155
try:
158-
await core.unload_cog(self._bot, extension, package)
156+
await core.unload_cog(self._bot, extension)
159157
except BaseException as e:
160158
error = 'Error unloading cog: {}'.format(handleActivityError(e))
161159
logger.error(error)

koala/cogs/base/cog.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ async def load_cog(self, ctx, extension):
188188
:param ctx: Context of the command
189189
:param extension: The name of the cog
190190
"""
191-
await ctx.send(await core.load_cog(self.bot, extension, koalabot.COGS_PACKAGE))
191+
await ctx.send(await core.load_cog(self.bot, extension))
192192

193193
@commands.command(name="unloadCog", aliases=["unload_cog"])
194194
@commands.check(koalabot.is_owner)
@@ -198,7 +198,7 @@ async def unload_cog(self, ctx, extension):
198198
:param ctx: Context of the command
199199
:param extension: The name of the cog
200200
"""
201-
await ctx.send(await core.unload_cog(self.bot, extension, koalabot.COGS_PACKAGE))
201+
await ctx.send(await core.unload_cog(self.bot, extension))
202202

203203
@commands.command(name="enableExt", aliases=["enable_koala_ext"])
204204
@commands.check(koalabot.is_admin)

koala/cogs/base/core.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,18 @@ async def purge(bot: Bot, channel_id, amount):
127127
return await channel.purge(limit=amount+1)
128128

129129

130-
async def load_cog(bot: Bot, extension, package):
130+
async def load_cog(bot: Bot, extension):
131131
"""
132132
Loads a cog from the cogs folder
133133
:param extension:
134134
:param package:
135135
:return:
136136
"""
137-
await bot.load_extension("."+extension, package=package)
137+
await bot.load_extension("."+extension, package=koalabot.COGS_PACKAGE)
138138
return f'{extension} Cog Loaded'
139139

140140

141-
async def unload_cog(bot: Bot, extension, package):
141+
async def unload_cog(bot: Bot, extension):
142142
"""
143143
Unloads a cog from the cogs folder
144144
:param extension:
@@ -148,7 +148,7 @@ async def unload_cog(bot: Bot, extension, package):
148148
if extension == "base" or extension == "BaseCog":
149149
raise discord.ext.commands.errors.ExtensionError(message=f"Sorry, you can't unload the base cog", name=extension)
150150
else:
151-
await bot.unload_extension("."+extension, package=package)
151+
await bot.unload_extension("."+extension, package=koalabot.COGS_PACKAGE)
152152
return f'{extension} Cog Unloaded'
153153

154154

koala/rest/api.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,13 @@ def build_response(status_code, data):
4040
:param data:
4141
:return:
4242
"""
43+
if data is not None:
44+
body = json.dumps(data, cls=EnhancedJSONEncoder)
45+
else:
46+
body = None
47+
4348
return aiohttp.web.Response(status=status_code,
44-
body=json.dumps(data, cls=EnhancedJSONEncoder),
49+
body=body,
4550
content_type='application/json')
4651

4752

@@ -92,7 +97,7 @@ async def wrapper(*args, **kwargs):
9297
available_args = {}
9398

9499
if (request.method == "POST" or request.method == "PUT") and request.has_body:
95-
body = await request.post()
100+
body = await request.json()
96101
for arg in wanted_args:
97102
if arg in body:
98103
available_args[arg] = body[arg]

tests/cogs/base/test_api.py

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ async def test_get_activities_missing_param(api_client):
5353
'''
5454

5555
async def test_put_schedule_activity(api_client):
56-
resp = await api_client.put('/scheduled-activity', data=(
56+
resp = await api_client.put('/scheduled-activity', json=(
5757
{
5858
'activity_type': 'playing',
5959
'message': 'test',
@@ -67,7 +67,7 @@ async def test_put_schedule_activity(api_client):
6767

6868

6969
async def test_put_schedule_activity_missing_param(api_client):
70-
resp = await api_client.put('/scheduled-activity', data=(
70+
resp = await api_client.put('/scheduled-activity', json=(
7171
{
7272
'activity_type': 'playing',
7373
'message': 'test',
@@ -80,7 +80,7 @@ async def test_put_schedule_activity_missing_param(api_client):
8080

8181

8282
async def test_put_schedule_activity_bad_activity(api_client):
83-
resp = await api_client.put('/scheduled-activity', data=(
83+
resp = await api_client.put('/scheduled-activity', json=(
8484
{
8585
'activity_type': 'invalidActivity',
8686
'message': 'test',
@@ -93,7 +93,7 @@ async def test_put_schedule_activity_bad_activity(api_client):
9393

9494

9595
async def test_put_schedule_activity_bad_start_time(api_client):
96-
resp = await api_client.put('/scheduled-activity', data=(
96+
resp = await api_client.put('/scheduled-activity', json=(
9797
{
9898
'activity_type': 'playing',
9999
'message': 'test',
@@ -106,7 +106,7 @@ async def test_put_schedule_activity_bad_start_time(api_client):
106106

107107

108108
async def test_put_schedule_activity_bad_end_time(api_client):
109-
resp = await api_client.put('/scheduled-activity', data=(
109+
resp = await api_client.put('/scheduled-activity', json=(
110110
{
111111
'activity_type': 'invalidActivity',
112112
'message': 'test',
@@ -125,7 +125,7 @@ async def test_put_schedule_activity_bad_end_time(api_client):
125125

126126

127127
async def test_put_set_activity(api_client):
128-
resp = await api_client.put('/activity', data=(
128+
resp = await api_client.put('/activity', json=(
129129
{
130130
'activity_type': 'playing',
131131
'name': 'test',
@@ -138,7 +138,7 @@ async def test_put_set_activity(api_client):
138138

139139

140140
async def test_put_set_activity_bad_req(api_client):
141-
resp = await api_client.put('/activity', data=(
141+
resp = await api_client.put('/activity', json=(
142142
{
143143
'activity_type': 'invalidActivity',
144144
'name': 'test',
@@ -149,7 +149,7 @@ async def test_put_set_activity_bad_req(api_client):
149149

150150

151151
async def test_put_set_activity_missing_param(api_client):
152-
resp = await api_client.put('/activity', data=(
152+
resp = await api_client.put('/activity', json=(
153153
{
154154
'activity_type': 'invalidActivity',
155155
'url': 'test.com'
@@ -203,53 +203,53 @@ async def test_get_support_link(api_client):
203203
'''
204204

205205
async def test_post_load_cog(api_client):
206-
resp = await api_client.post('/load-cog', data=(
206+
resp = await api_client.post('/load-cog', json=(
207207
{
208208
'extension': 'announce',
209-
'package': koalabot.COGS_PACKAGE
209+
# 'package': koalabot.COGS_PACKAGE
210210
}))
211211
assert resp.status == OK
212212
text = await resp.text()
213213
assert text == '{"message": "Cog loaded"}'
214214

215215
async def test_post_load_base_cog(api_client):
216-
resp = await api_client.post('/load-cog', data=(
216+
resp = await api_client.post('/load-cog', json=(
217217
{
218218
'extension': 'base',
219-
'package': koalabot.COGS_PACKAGE
219+
# 'package': koalabot.COGS_PACKAGE
220220
}))
221221
assert resp.status == OK
222222
text = await resp.text()
223223
assert text == '{"message": "Cog loaded"}'
224224

225225
async def test_post_load_cog_bad_req(api_client):
226-
resp = await api_client.post('/load-cog', data=(
226+
resp = await api_client.post('/load-cog', json=(
227227
{
228228
'extension': 'invalidCog',
229-
'package': koalabot.COGS_PACKAGE
229+
# 'package': koalabot.COGS_PACKAGE
230230
}))
231231
assert resp.status == UNPROCESSABLE_ENTITY
232232
assert await resp.text() == '422: Error loading cog: Invalid extension'
233233

234-
async def test_post_load_cog_missing_param(api_client):
235-
resp = await api_client.post('/load-cog', data=(
236-
{
237-
'extension': 'invalidCog'
238-
}))
239-
assert resp.status == BAD_REQUEST
240-
assert await resp.text() == "400: Unsatisfied Arguments: {'package'}"
234+
# async def test_post_load_cog_missing_param(api_client):
235+
# resp = await api_client.post('/load-cog', json=(
236+
# {
237+
# 'extension': 'invalidCog'
238+
# }))
239+
# assert resp.status == BAD_REQUEST
240+
# assert await resp.text() == "400: Unsatisfied Arguments: {'package'}"
241241

242242
async def test_post_load_cog_already_loaded(api_client):
243-
await api_client.post('/load-cog', data=(
243+
await api_client.post('/load-cog', json=(
244244
{
245245
'extension': 'announce',
246-
'package': koalabot.COGS_PACKAGE
246+
# 'package': koalabot.COGS_PACKAGE
247247
}))
248248

249-
resp = await api_client.post('/load-cog', data=(
249+
resp = await api_client.post('/load-cog', json=(
250250
{
251251
'extension': 'announce',
252-
'package': koalabot.COGS_PACKAGE
252+
# 'package': koalabot.COGS_PACKAGE
253253
}))
254254
assert resp.status == UNPROCESSABLE_ENTITY
255255
assert await resp.text() == '422: Error loading cog: Already loaded'
@@ -261,43 +261,43 @@ async def test_post_load_cog_already_loaded(api_client):
261261
'''
262262

263263
async def test_post_unload_cog(api_client):
264-
await api_client.post('/load-cog', data=(
264+
await api_client.post('/load-cog', json=(
265265
{
266266
'extension': 'announce',
267-
'package': koalabot.COGS_PACKAGE
267+
# 'package': koalabot.COGS_PACKAGE
268268
}))
269269

270-
resp = await api_client.post('/unload-cog', data=(
270+
resp = await api_client.post('/unload-cog', json=(
271271
{
272272
'extension': 'announce',
273-
'package': koalabot.COGS_PACKAGE
273+
# 'package': koalabot.COGS_PACKAGE
274274
}))
275275
assert resp.status == OK
276276
text = await resp.text()
277277
assert text == '{"message": "Cog unloaded"}'
278278

279279
async def test_post_unload_cog_not_loaded(api_client):
280-
resp = await api_client.post('/unload-cog', data=(
280+
resp = await api_client.post('/unload-cog', json=(
281281
{
282282
'extension': 'announce',
283-
'package': koalabot.COGS_PACKAGE
283+
# 'package': koalabot.COGS_PACKAGE
284284
}))
285285
assert resp.status == UNPROCESSABLE_ENTITY
286286
assert await resp.text() == '422: Error unloading cog: Extension not loaded'
287287

288-
async def test_post_unload_cog_missing_param(api_client):
289-
resp = await api_client.post('/unload-cog', data=(
290-
{
291-
'extension': 'invalidCog'
292-
}))
293-
assert resp.status == BAD_REQUEST
294-
assert await resp.text() == "400: Unsatisfied Arguments: {'package'}"
288+
# async def test_post_unload_cog_missing_param(api_client):
289+
# resp = await api_client.post('/unload-cog', json=(
290+
# {
291+
# 'extension': 'invalidCog'
292+
# }))
293+
# assert resp.status == BAD_REQUEST
294+
# assert await resp.text() == "400: Unsatisfied Arguments: {'package'}"
295295

296296
async def test_post_unload_base_cog(api_client):
297-
resp = await api_client.post('/unload-cog', data=(
297+
resp = await api_client.post('/unload-cog', json=(
298298
{
299299
'extension': 'BaseCog',
300-
'package': koalabot.COGS_PACKAGE
300+
# 'package': koalabot.COGS_PACKAGE
301301
}))
302302
assert resp.status == UNPROCESSABLE_ENTITY
303303
text = await resp.text()
@@ -313,7 +313,7 @@ async def test_post_unload_base_cog(api_client):
313313
async def test_post_enable_extension(api_client, bot):
314314
await koalabot.load_all_cogs(bot)
315315
guild: discord.Guild = dpytest.get_config().guilds[0]
316-
resp = await api_client.post('/enable-extension', data=({
316+
resp = await api_client.post('/enable-extension', json=({
317317
'guild_id': guild.id,
318318
'koala_ext': 'Announce'
319319
}))
@@ -325,7 +325,7 @@ async def test_post_enable_extension(api_client, bot):
325325
async def test_post_enable_extension_bad_req(api_client):
326326
guild: discord.Guild = dpytest.get_config().guilds[0]
327327

328-
resp = await api_client.post('/enable-extension', data=(
328+
resp = await api_client.post('/enable-extension', json=(
329329
{
330330
'guild_id': guild.id,
331331
'koala_ext': 'Invalid Extension'
@@ -336,7 +336,7 @@ async def test_post_enable_extension_bad_req(api_client):
336336

337337
async def test_post_enable_extension_missing_param(api_client):
338338
guild: discord.Guild = dpytest.get_config().guilds[0]
339-
resp = await api_client.post('/enable-extension', data=(
339+
resp = await api_client.post('/enable-extension', json=(
340340
{
341341
'guild_id': guild.id
342342
}))
@@ -354,13 +354,13 @@ async def test_post_enable_extension_missing_param(api_client):
354354
async def test_post_disable_extension(api_client, bot):
355355
await koalabot.load_all_cogs(bot)
356356
guild: discord.Guild = dpytest.get_config().guilds[0]
357-
setup = await api_client.post('/enable-extension', data=({
357+
setup = await api_client.post('/enable-extension', json=({
358358
'guild_id': guild.id,
359359
'koala_ext': 'Announce'
360360
}))
361361
assert setup.status == OK
362362

363-
resp = await api_client.post('/disable-extension', data=({
363+
resp = await api_client.post('/disable-extension', json=({
364364
'guild_id': guild.id,
365365
'koala_ext': 'Announce'
366366
}))
@@ -370,7 +370,7 @@ async def test_post_disable_extension(api_client, bot):
370370

371371
async def test_post_disable_extension_not_enabled(api_client):
372372
guild: discord.Guild = dpytest.get_config().guilds[0]
373-
resp = await api_client.post('/disable-extension', data=({
373+
resp = await api_client.post('/disable-extension', json=({
374374
'guild_id': guild.id,
375375
'koala_ext': 'Announce'
376376
}))
@@ -380,7 +380,7 @@ async def test_post_disable_extension_not_enabled(api_client):
380380

381381
async def test_post_disable_extension_missing_param(api_client):
382382
guild: discord.Guild = dpytest.get_config().guilds[0]
383-
resp = await api_client.post('/disable-extension', data=({
383+
resp = await api_client.post('/disable-extension', json=({
384384
'guild_id': guild.id
385385
}))
386386
assert resp.status == BAD_REQUEST
@@ -390,7 +390,7 @@ async def test_post_disable_extension_missing_param(api_client):
390390
async def test_post_disable_extension_bad_req(api_client):
391391
guild: discord.Guild = dpytest.get_config().guilds[0]
392392

393-
resp = await api_client.post('/disable-extension', data=(
393+
resp = await api_client.post('/disable-extension', json=(
394394
{
395395
'guild_id': guild.id,
396396
'koala_ext': 'Invalid Extension'

0 commit comments

Comments
 (0)