Skip to content

Request consumer

PConsumer

consumer class that consumes the requests and returns the responses of the ClashOfClans API

:ivar queue: the request_queue where the requests are enqueued :type queue: Queue :ivar r_p_s: allowed number of requests that can be done with one consumer in one second :type r_p_s: int :ivar url: the base URL for the requests :type url: str

Source code in pyclasher/client/request_consumer.py
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
class PConsumer:
    def __init__(self, queue, token, requests_per_s, request_timeout, url):
        self.queue = queue
        self.header = {
            'Authorization': f'Bearer {token}'
        }
        self.r_p_s = requests_per_s
        self.timeout = request_timeout
        self.wait = 1 / self.r_p_s
        self.url = url
        self.session = ClientSession(
            base_url=url,
            headers=self.header,
            timeout=ClientTimeout(total=self.timeout)
        )
        return

    async def _request(self, future, url, method, body, status, error):
        try:
            async with self.session.request(
                    method=method,
                    url=url,
                    data=None if body is None else dumps(body)
            ) as response:
                response_json = await response.json()

                if response.status == 200:
                    error.set_result(None)
                else:
                    error.set_result(ApiExceptions.from_api_code(
                        response.status, ClientError(response_json)
                    ))

                future.set_result(response_json)
                status.set_result(response.status)
                return

        except aTimeoutError:
            future.set_result(MISSING)
            status.set_result(None)
            error.set_result(RequestTimeout(self.timeout))
        except Exception as exception:
            future.set_result(MISSING)
            status.set_result(None)
            error.set_result(exception)
            raise exception


    async def consume(self):
        while True:
            future, url, method, body, status, error = await self.queue.get()

            async with ExecutionTimer(self.wait):
                create_task(
                    self._request(
                        future, url, method.value, body, status, error
                    )
                )

                self.queue.task_done()

    async def close(self):
        await self.session.close()
        return

__init__(queue, token, requests_per_s, request_timeout, url)

initialisation of the request consumer

:param queue: the request_queue where the requests are enqueued :type queue: Queue :param token: one ClashOfClans API token :type token: str :param requests_per_s: allowed number of requests that can be done with one consumer in one second :type requests_per_s: int :param request_timeout: seconds until the request is cancelled due to a timeout :type request_timeout: float :param url: the base URL for the requests :type url: str :return: None :rtype: None

Source code in pyclasher/client/request_consumer.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def __init__(self, queue, token, requests_per_s, request_timeout, url):
    self.queue = queue
    self.header = {
        'Authorization': f'Bearer {token}'
    }
    self.r_p_s = requests_per_s
    self.timeout = request_timeout
    self.wait = 1 / self.r_p_s
    self.url = url
    self.session = ClientSession(
        base_url=url,
        headers=self.header,
        timeout=ClientTimeout(total=self.timeout)
    )
    return

close() async

asynchronous method that closed the consumer

:return: None :rtype: None

Source code in pyclasher/client/request_consumer.py
72
73
74
async def close(self):
    await self.session.close()
    return

consume() async

asynchronous method that is used as a consuming task that consumes requests forever until stopped

:return: None :rtype: None .. note:: uses an infinite while loop, only run it as an asyncio task

Source code in pyclasher/client/request_consumer.py
59
60
61
62
63
64
65
66
67
68
69
70
async def consume(self):
    while True:
        future, url, method, body, status, error = await self.queue.get()

        async with ExecutionTimer(self.wait):
            create_task(
                self._request(
                    future, url, method.value, body, status, error
                )
            )

            self.queue.task_done()