secondaryRateLimited function

Response secondaryRateLimited({
  1. bool blocked = false,
  2. Duration? retryAfter,
})

A secondary rate-limit refusal — GitHub saying "too fast", not "not allowed".

Deliberately carries a healthy x-ratelimit-remaining: that is the whole difficulty, since the headers are then byte-for-byte an authorization failure and only the body says otherwise. blocked switches between the ordinary throttle (clears in about a minute) and the abuse-detection block (one to two minutes for a client that stops at the refusal, a quarter of an hour for one that keeps sending), which are recovered from differently.

retryAfter models the header GitHub sends on some secondary refusals and omits on others. Both cases are real and they are recovered from differently — a stated wait is the backend's own instruction, an absent one leaves the caller to choose — so the fake has to be able to produce either.

Implementation

http.Response secondaryRateLimited({
  bool blocked = false,
  Duration? retryAfter,
}) =>
    http.Response(
      jsonEncode({
        'message': blocked
            ? 'You have exceeded a secondary rate limit and have been '
                'temporarily blocked from content creation. Please retry your '
                'request again later.'
            : 'You have exceeded a secondary rate limit. Please wait a few '
                'minutes before trying again.',
        'documentation_url':
            'https://docs.github.com/rest/overview/rate-limits-for-the-rest-api',
      }),
      403,
      headers: {
        'content-type': 'application/json',
        'x-ratelimit-remaining': '4321',
        if (retryAfter != null) 'retry-after': '${retryAfter.inSeconds}',
      },
    );