diff --git a/reddit.go b/reddit.go index 93089eb..f388b0d 100644 --- a/reddit.go +++ b/reddit.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "net/url" + "strconv" "strings" "github.com/thecsw/mira/v4/models" @@ -38,6 +39,24 @@ func (c *Reddit) MiraRequest(method string, target string, payload map[string]st return nil, err } defer response.Body.Close() + + // Reddit returns integers for X-Ratelimit-Used and X-Ratelimit-Reset, but not for X-Ratelimit-Remaining + if rateLimitUsed := response.Header.Get("X-Ratelimit-Used"); rateLimitUsed != "" { + if used, err := strconv.Atoi(rateLimitUsed); err == nil { + c.RateLimitUsed = used + } + } + if rateLimitRemaining := response.Header.Get("X-Ratelimit-Remaining"); rateLimitRemaining != "" { + if remaining, err := strconv.ParseFloat(rateLimitRemaining, 64); err == nil { + c.RateLimitRemaining = remaining + } + } + if rateLimitReset := response.Header.Get("X-Ratelimit-Reset"); rateLimitReset != "" { + if reset, err := strconv.Atoi(rateLimitReset); err == nil { + c.RateLimitReset = reset + } + } + buf := new(bytes.Buffer) buf.ReadFrom(response.Body) data := buf.Bytes() diff --git a/reddit_struct.go b/reddit_struct.go index 9754b94..e0ad48d 100644 --- a/reddit_struct.go +++ b/reddit_struct.go @@ -8,13 +8,16 @@ import ( // Reddit is the main mira struct that practically // does everything type Reddit struct { - Token string `json:"access_token"` - Duration float64 `json:"expires_in"` - Creds Credentials - Chain chan *ChainVals - Stream Streaming - Values RedditVals - Client *http.Client + Token string `json:"access_token"` + Duration float64 `json:"expires_in"` + Creds Credentials + Chain chan *ChainVals + Stream Streaming + Values RedditVals + Client *http.Client + RateLimitUsed int // The number of requests used in the current rate limit window + RateLimitRemaining float64 // The number of requests left to use in the current rate limit window + RateLimitReset int // The number of seconds left in the current rate limit window } // Streaming is used for some durations on how frequently