about summary refs log tree commit diff stats
path: root/discord/utils/rest.py
blob: fc3f729f9bf1be28df78c46d08407d6ac05bb8bd (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import aiohttp

from discord.utils.exceptions import APIException


class RESTClient:
    """
    Utility class to make it easier to make HTTP requests to Discord's API. This should not be used manually,
    as it only works with Discord's API and the library should cover anything that can be requested from it. Any
    requests to other APIs should use `aiohttp`.
    """
    def __init__(self, token: str, session: aiohttp.ClientSession):
        self.token = token
        self._session = session

    @property
    def session(self) -> aiohttp.ClientSession:
        """
        Returns the _session used by the client.
        """
        return self._session

    async def get(self, url: str):
        """
        Makes a GET request to Discord's API.

        **Parameters:**
        - url: The part of the request URL that goes after `https://discord.com/api/v10`
        """
        async with self._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(self, url: str, data):
        """
        Makes a POST request to Discord's API.

        **Parameters:**
        - url: The part of the request URL that goes after `https://discord.com/api/v10`
        - data: The data to post.
        """
        async with self._session.post(url='https://discord.com/api/v10' + url, data=data) as r:
            data = await r.json()
            match r.status:
                case 200 | 204 | 201:
                    return data
                case other:
                    raise APIException(data['message'])

    async def patch(self, url, data):
        """
        Makes a PATCH request to Discord's API.

        **Parameters:**
        - url: The part of the request URL that goes after `https://discord.com/api/v10`
        - data: The data to patch.
        """
        async with self._session.patch(url='https://discord.com/api/v10' + url, data=data) as res:
            data = await res.json()
            match res.status:
                case 200 | 204:
                    return data
                case other:
                    raise APIException(data['message'])

    async def delete(self, url):
        """
        Makes a POST request to Discord's API.

        **Parameters:**
        - url: The part of the request URL that goes after `https://discord.com/api/v10`
        """
        async with self._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'])