fromResponse static method

HttpBucket? fromResponse(
  1. HttpHandler handler,
  2. BaseResponse response
)

Create a HttpBucket from a response from the API.

If the response does not have rate limit headers, this method returns null.

Every response from Discord's API contains headers to handle rate limiting. This class keeps track of these headers in a single rate limit bucket (identified by the xRateLimitBucket header) and allows the client to anticipate rate limits.

Every HttpHandler stores a map of HttpRoute.rateLimitId to HttpBucket and implicitly checks each request before sending it, waiting if a rate limit would be exceeded.

External references:

Implementation

static HttpBucket? fromResponse(HttpHandler handler, BaseResponse response) {
  final limit = response.headers[xRateLimitLimit];
  final remaining = response.headers[xRateLimitRemaining];
  final resetAfter = response.headers[xRateLimitResetAfter];
  final id = response.headers[xRateLimitBucket];

  if (limit == null || remaining == null || resetAfter == null || id == null) {
    return null;
  }

  final resetAfterDuration = Duration(milliseconds: (double.parse(resetAfter) * 1000).ceil());
  final resetAtTime = DateTime.now().add(resetAfterDuration);

  return HttpBucket(
    handler,
    id: id,
    remaining: int.parse(remaining),
    resetAt: resetAtTime,
  );
}