dbl/http.py
async def request(self, method, url, **kwargs):
        """Handles requests to the API"""
        rate_limiter = RateLimiter(max_calls=59, period=60, callback=limited)
        # handles ratelimits. max_calls is set to 59 because current implementation will retry in 60s after 60 calls is reached. DBL has a 1h block so obviously this doesn't work well, as it will get a 429 when 60 is reached.

        async with rate_limiter:  # this works but doesn't 'save' over restart. need a better implementation.

            if not self.token:
                raise UnauthorizedDetected('UnauthorizedDetected (status code: 401): No TOKEN provided')

            headers = {
                'User-Agent': self.user_agent,
                'Content-Type': 'application/json'
            }

            if 'json' in kwargs:
                kwargs['data'] = to_json(kwargs.pop('json'))

            kwargs['headers'] = headers
            headers['Authorization'] = self.token


            for tries in range(5):
                async with self.session.request(method, url, **kwargs) as resp:
                    log.debug('%s %s with %s has returned %s', method,
                              url, kwargs.get('data'), resp.status)

                    data = await json_or_text(resp)


                    if 300 > resp.status >= 200:
                        return data


                    if resp.status == 429:  # we are being ratelimited
                        fmt = 'We are being rate limited. Retrying in %.2f seconds (%.3f minutes).'

                        # sleep a bit
                        retry_after = json.loads(resp.headers.get('Retry-After'))
                        mins = retry_after / 60
                        log.warning(fmt, retry_after, mins)

                        # check if it's a global rate limit (True as only 1 ratelimit atm - /api/bots)
                        is_global = True  # is_global = data.get('global', False)
                        if is_global:
                            self._global_over.clear()

                        await asyncio.sleep(retry_after, loop=self.loop)
                        log.debug('Done sleeping for the rate limit. Retrying...')

                        # release the global lock now that the
                        # global rate limit has passed
                        if is_global:
                            self._global_over.set()
                            log.debug('Global rate limit is now over.')

                        continue


                    if resp.status == 400:
                        raise HTTPException(resp, data)
                    elif resp.status == 401:
                        raise Unauthorized(resp, data)
                    elif resp.status == 403:
                        raise Forbidden(resp, data)
                    elif resp.status == 404:
                        raise NotFound(resp, data)
                    else:
                        raise HTTPException(resp, data)
            # We've run out of retries, raise.
            raise HTTPException(resp, data)
async def request(self, method, url, **kwargs):
        rate_limiter = RateLimiter(max_calls=59, period=60, callback=limited)
        # handles ratelimits. max_calls is set to 59 because current implementation will retry in 60s after 60 calls is reached. DBL has a 1h block so obviously this doesn't work well, as it will get a 429 when 60 is reached.

        async with rate_limiter:  # this works but doesn't 'save' over restart. need a better implementation.

            if not self.token:
                raise UnauthorizedDetected('UnauthorizedDetected (status code: 401): No TOKEN provided')

            headers = {
                'User-Agent': self.user_agent,
                'Content-Type': 'application/json'
            }

            if 'json' in kwargs:
                kwargs['data'] = to_json(kwargs.pop('json'))

            kwargs['headers'] = headers
            headers['Authorization'] = self.token


            for tries in range(5):
                async with self.session.request(method, url, **kwargs) as resp:
                    log.debug('%s %s with %s has returned %s', method,
                              url, kwargs.get('data'), resp.status)

                    data = await json_or_text(resp)


                    if 300 > resp.status >= 200:
                        return data


                    if resp.status == 429:  # we are being ratelimited
                        fmt = 'We are being rate limited. Retrying in %.2f seconds (%.3f minutes).'

                        # sleep a bit
                        retry_after = json.loads(resp.headers.get('Retry-After'))
                        mins = retry_after / 60
                        log.warning(fmt, retry_after, mins)

                        # check if it's a global rate limit (True as only 1 ratelimit atm - /api/bots)
                        is_global = True  # is_global = data.get('global', False)
                        if is_global:
                            self._global_over.clear()

                        await asyncio.sleep(retry_after, loop=self.loop)
                        log.debug('Done sleeping for the rate limit. Retrying...')

                        # release the global lock now that the
                        # global rate limit has passed
                        if is_global:
                            self._global_over.set()
                            log.debug('Global rate limit is now over.')

                        continue


                    if resp.status == 400:
                        raise HTTPException(resp, data)
                    elif resp.status == 401:
                        raise Unauthorized(resp, data)
                    elif resp.status == 403:
                        raise Forbidden(resp, data)
                    elif resp.status == 404:
                        raise NotFound(resp, data)
                    else:
                        raise HTTPException(resp, data)
            # We've run out of retries, raise.
            raise HTTPException(resp, data)
async def request ( self , method , url , * * kwargs ) : rate_limiter = RateLimiter ( max_calls = 59 , period = 60 , callback = limited ) # handles ratelimits. max_calls is set to 59 because current implementation will retry in 60s after 60 calls is reached. DBL has a 1h block so obviously this doesn't work well, as it will get a 429 when 60 is reached. async with rate_limiter : # this works but doesn't 'save' over restart. need a better implementation. if not self . token : raise UnauthorizedDetected ( 'UnauthorizedDetected (status code: 401): No TOKEN provided' ) headers = { 'User-Agent' : self . user_agent , 'Content-Type' : 'application/json' } if 'json' in kwargs : kwargs [ 'data' ] = to_json ( kwargs . pop ( 'json' ) ) kwargs [ 'headers' ] = headers headers [ 'Authorization' ] = self . token for tries in range ( 5 ) : async with self . session . request ( method , url , * * kwargs ) as resp : log . debug ( '%s %s with %s has returned %s' , method , url , kwargs . get ( 'data' ) , resp . status ) data = await json_or_text ( resp ) if 300 > resp . status >= 200 : return data if resp . status == 429 : # we are being ratelimited fmt = 'We are being rate limited. Retrying in %.2f seconds (%.3f minutes).' # sleep a bit retry_after = json . loads ( resp . headers . get ( 'Retry-After' ) ) mins = retry_after / 60 log . warning ( fmt , retry_after , mins ) # check if it's a global rate limit (True as only 1 ratelimit atm - /api/bots) is_global = True # is_global = data.get('global', False) if is_global : self . _global_over . clear ( ) await asyncio . sleep ( retry_after , loop = self . loop ) log . debug ( 'Done sleeping for the rate limit. Retrying...' ) # release the global lock now that the # global rate limit has passed if is_global : self . _global_over . set ( ) log . debug ( 'Global rate limit is now over.' ) continue if resp . status == 400 : raise HTTPException ( resp , data ) elif resp . status == 401 : raise Unauthorized ( resp , data ) elif resp . status == 403 : raise Forbidden ( resp , data ) elif resp . status == 404 : raise NotFound ( resp , data ) else : raise HTTPException ( resp , data ) # We've run out of retries, raise. raise HTTPException ( resp , data )
Handles requests to the API
Handles requests to the API