about summary refs log tree commit diff stats
path: root/discord/utils/rest.py
blob: f7f6ab828acdf85fd84b802a61224ce9ac5b692c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import aiohttp
import asyncio

from discord.utils.exceptions import APIException


async def get(token, url):
    async with aiohttp.ClientSession(headers={
        "Authorization": f"Bot {token}",
        "User-Agent": f"DiscordBot (https://github.com/mounderfod/discobra 0.0.1)"
    }) as session:
        async with session.get(url='https://discord.com/api/v10' + url) as r:
            data = await r.json()
            match r.status:
                case 200:
                    return data
                case other:
                    raise APIException(data['message'])


async def post(token, url, data):
    async with aiohttp.ClientSession(headers={
        "Authorization": f"Bot {token}",
        "User-Agent": f"DiscordBot (https://github.com/mounderfod/discobra 0.0.1)"
    }) as session:
        async with session.post(url='https://discord.com/api/v10' + url, data=data) as r:
            data = await r.json()
            match r.status:
                case 200 | 204:
                    return data
                case other:
                    raise APIException(data['message'])


async def patch(token, url, data):
    async with aiohttp.ClientSession(headers={
        "Authorization": f"Bot {token}",
        "User-Agent": f"DiscordBot (https://github.com/mounderfod/discobra 0.0.1)"
    }) as session:
        async with session.patch(url='https://discord.com/api/v10' + url, data=data) as r:
            data = await r.json()
            match r.status:
                case 200 | 204:
                    return data
                case other:
                    raise APIException(data['message'])


async def delete(token, url):
    async with aiohttp.ClientSession(headers={
        "Authorization": f"Bot {token}",
        "User-Agent": f"DiscordBot (https://github.com/mounderfod/discobra 0.0.1)"
    }) as session:
        async with session.delete(url='https://discord.com/api/v10' + url) as r:
            data = await r.json()
            match r.status:
                case 200:
                    return data
                case other:
                    raise APIException(data['message'])