Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions reddit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"

"github.com/thecsw/mira/v4/models"
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you link the same link from the PR? So whenever someone looks at the code in the future, they can see where these values are documented and look back on them—say when/if they change in the future.

if rateLimitUsed := response.Header.Get("X-Ratelimit-Used"); rateLimitUsed != "" {
if used, err := strconv.Atoi(rateLimitUsed); err == nil {
c.RateLimitUsed = used

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to give these some default values other than 0? i.e., is 0 a valid value in regular runtime and could we have something else to signify its "uninitialized" and/or "unknown" (say, whenever err != nil)? Since all of these are signed, -1?

}
}
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()
Expand Down
17 changes: 10 additions & 7 deletions reddit_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the godoc and LSPs to work nicely, can you put the inlined comments above the declaration in godoc format of // RateLimitUsed indicates the number of...

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
Expand Down