jpopesculian / twitter-v2-rs

Rust bindings for Twitter API v2
https://docs.rs/twitter-v2/latest
102 stars 28 forks source link

how best can we make the rate limiting information that twitter puts into the response headers available to the user? #11

Open db48x opened 2 years ago

db48x commented 2 years ago

Twitter puts some information about your account’s rate limits in the response headers of every request. Most important is the time at which your rate limit is reset after a 429 response. See the documentation at https://developer.twitter.com/en/docs/twitter-api/rate-limits for all of the details.

I would like there to be some way to access this information, so that I can act on it. As a quick test, I tried sticking it on to the ApiError struct:

ApiError { title: "Too Many Requests", kind: "about:blank", status: 429, detail: "Too Many Requests", errors: [], reset: Some(1660051376) }

I added the simplest possible thing to api_error_for_status in api_result.rs:

let reset = self.headers().get("x-rate-limit-reset")
  .map(|v| v.to_str().unwrap().parse().unwrap());

and then added the resulting value to the ApiError that it creates.

This works, but it is not very elegant. It leaves two of the headers unavailable, and it would be rather nicer if it created a std::time::Duration for me, though that might be going a bit far. It also doesn’t do anything to make this information available on successful requests.

Do you prefer any particular way of implementing this?