Documentation ¶
Index ¶
- Constants
- func GlobalRateLimit(resp *http.Response) bool
- func HeaderToTime(header *http.Header) (t time.Time, err error)
- func JSONEncode(w io.WriteCloser, v interface{}) error
- func Marshal(v interface{}) (data []byte, err error)
- func RateLimited(resp *http.Response) bool
- func SupportsDiscordAPIVersion(version int) bool
- func Unmarshal(data []byte, v interface{}) error
- type Bucket
- type Client
- func (c *Client) Delete(req *Request) (resp *http.Response, body []byte, err error)
- func (c *Client) Get(req *Request) (resp *http.Response, body []byte, err error)
- func (c *Client) Patch(req *Request) (resp *http.Response, body []byte, err error)
- func (c *Client) Post(req *Request) (resp *http.Response, body []byte, err error)
- func (c *Client) Put(req *Request) (resp *http.Response, body []byte, err error)
- func (c *Client) RateLimiter() RateLimiter
- func (c *Client) Request(r *Request) (resp *http.Response, body []byte, err error)
- type Config
- type Deleter
- type Details
- type ErrREST
- type Getter
- type Patcher
- type Poster
- type Puter
- type RateLimit
- func (r *RateLimit) Bucket(key string) *Bucket
- func (r *RateLimit) RateLimitTimeout(key string) int64
- func (r *RateLimit) RateLimited(key string) bool
- func (r *RateLimit) RequestPermit(key string) (timeout time.Duration, err error)
- func (r *RateLimit) UpdateRegisters(key string, adjuster RateLimitAdjuster, resp *http.Response, content []byte)
- func (r *RateLimit) WaitTime(req *Request) time.Duration
- type RateLimitAdjuster
- type RateLimitInfo
- type RateLimiter
- type Request
- type Requester
Constants ¶
const ( BaseURL = "https://discordapp.com/api" // Header AuthorizationFormat = "Bot %s" UserAgentFormat = "DiscordBot (%s, %s) %s" HTTPCodeRateLimit int = 429 ContentEncoding = "Content-Encoding" ContentType = "Content-Type" ContentTypeJSON = "application/json" GZIPCompression = "gzip" )
defaults and string format's for Discord interaction
const ( XRateLimitLimit = "X-RateLimit-Limit" XRateLimitRemaining = "X-RateLimit-Remaining" XRateLimitReset = "X-RateLimit-Reset" // is converted from seconds to milliseconds! XRateLimitGlobal = "X-RateLimit-Global" RateLimitRetryAfter = "Retry-After" GlobalRateLimiterKey = "" )
http rate limit identifiers
Variables ¶
This section is empty.
Functions ¶
func GlobalRateLimit ¶
GlobalRateLimit assumes that there will always be a header entry when a global rate limit kicks in
func HeaderToTime ¶ added in v0.7.0
HeaderToTime takes the response header from Discord and extracts the timestamp. Useful for detecting time desync between discord and client
func JSONEncode ¶ added in v0.8.0
func JSONEncode(w io.WriteCloser, v interface{}) error
func Marshal ¶ added in v0.8.0
Marshal is the json marshaler implementation for jsoniter, depending on the build tags.
func RateLimited ¶
RateLimited check if a response was rate limited
func SupportsDiscordAPIVersion ¶
SupportsDiscordAPIVersion check if a given discord api version is supported by this package.
Types ¶
type Bucket ¶
type Bucket struct {
// contains filtered or unexported fields
}
Bucket holds the rate limit info for a given key or endpoint
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the httd client for handling Discord requests
func (*Client) RateLimiter ¶
func (c *Client) RateLimiter() RateLimiter
RateLimiter get the rate limit manager
type Config ¶
type Config struct { APIVersion int BotToken string HTTPClient *http.Client CancelRequestWhenRateLimited bool // Header field: `User-Agent: DiscordBot ({Source}, {Version}) {Extra}` UserAgentVersion string UserAgentSourceURL string UserAgentExtra string }
Config is the configuration options for the httd.Client structure. Essentially the behaviour of all requests sent to Discord.
type Deleter ¶
Deleter interface which holds the Delete method for sending delete requests to Discord
type Details ¶
type Details struct { Ratelimiter string Endpoint string // always as a suffix to Ratelimiter(!) ResponseStruct interface{} SuccessHTTPCode int }
Details ...
type ErrREST ¶ added in v0.8.0
type RateLimit ¶
type RateLimit struct {
// contains filtered or unexported fields
}
RateLimit ...
func (*RateLimit) Bucket ¶
Bucket returns a bucket given the key (or ID) for a rate limit bucket. If no bucket exists for the key, one will be created.
func (*RateLimit) RateLimitTimeout ¶
RateLimitTimeout returns the time left before the rate limit for a given key is reset. This takes the global rate limit into account.
func (*RateLimit) RateLimited ¶
RateLimited checks if the given key is rate limited. This takes the global rate limiter into account.
func (*RateLimit) RequestPermit ¶ added in v0.9.0
func (*RateLimit) UpdateRegisters ¶
func (r *RateLimit) UpdateRegisters(key string, adjuster RateLimitAdjuster, resp *http.Response, content []byte)
UpdateRegisters updates the relevant buckets and time desync between the client and the Discord servers.
type RateLimitAdjuster ¶ added in v0.8.7
RateLimitAdjuster acts as a middleware when receiving ratelimits from Discord.
type RateLimitInfo ¶
type RateLimitInfo struct { Message string `json:"message"` RetryAfter int64 `json:"retry_after"` Global bool `json:"global"` Limit int `json:"-"` Remaining int `json:"-"` Reset int64 `json:"-"` Empty bool `json:"-"` }
RateLimitInfo is populated by Discord http responses in order to obtain rate limits
func ExtractRateLimitInfo ¶
func ExtractRateLimitInfo(resp *http.Response, body []byte) (info *RateLimitInfo, err error)
ExtractRateLimitInfo uses the RateLimitInfo struct to obtain rate limits from the Discord response
type RateLimiter ¶
type RateLimiter interface { Bucket(key string) *Bucket RateLimitTimeout(key string) int64 RateLimited(key string) bool UpdateRegisters(key string, adjuster RateLimitAdjuster, res *http.Response, responseBody []byte) WaitTime(req *Request) time.Duration RequestPermit(key string) (timeout time.Duration, err error) }
RateLimiter is the interface for the ratelimit manager
type Request ¶
type Request struct { Method string Ratelimiter string RateLimitAdjuster RateLimitAdjuster Endpoint string Body interface{} // will automatically marshal to JSON if the ContentType is httd.ContentTypeJSON ContentType string }
Request is populated before executing a Discord request to correctly generate a http request