httd

package
v0.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2019 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
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

func GlobalRateLimit(resp *http.Response) bool

GlobalRateLimit assumes that there will always be a header entry when a global rate limit kicks in

func HeaderToTime added in v0.7.0

func HeaderToTime(header *http.Header) (t time.Time, err error)

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

func Marshal(v interface{}) (data []byte, err error)

Marshal is the json marshaler implementation for jsoniter, depending on the build tags.

func RateLimited

func RateLimited(resp *http.Response) bool

RateLimited check if a response was rate limited

func SupportsDiscordAPIVersion

func SupportsDiscordAPIVersion(version int) bool

SupportsDiscordAPIVersion check if a given discord api version is supported by this package.

func Unmarshal added in v0.8.0

func Unmarshal(data []byte, v interface{}) error

Unmarshal is the json unmarshaler implementation that is defined by the used build tags.

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 NewClient

func NewClient(conf *Config) (*Client, error)

NewClient ...

func (*Client) Delete

func (c *Client) Delete(req *Request) (resp *http.Response, body []byte, err error)

Delete handles Discord delete requests

func (*Client) Get

func (c *Client) Get(req *Request) (resp *http.Response, body []byte, err error)

Get handles Discord get requests

func (*Client) Patch

func (c *Client) Patch(req *Request) (resp *http.Response, body []byte, err error)

Patch handles Discord patch requests

func (*Client) Post

func (c *Client) Post(req *Request) (resp *http.Response, body []byte, err error)

Post handles Discord post requests

func (*Client) Put

func (c *Client) Put(req *Request) (resp *http.Response, body []byte, err error)

Put handles Discord put requests

func (*Client) RateLimiter

func (c *Client) RateLimiter() RateLimiter

RateLimiter get the rate limit manager

func (*Client) Request

func (c *Client) Request(r *Request) (resp *http.Response, body []byte, err error)

Request execute a Discord request

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

type Deleter interface {
	Delete(req *Request) (resp *http.Response, body []byte, err error)
}

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 ErrREST struct {
	Code       int    `json:"code"`
	Msg        string `json:"message"`
	Suggestion string `json:"-"`
	HTTPCode   int    `json:"-"`
}

func (*ErrREST) Error added in v0.8.0

func (e *ErrREST) Error() string

type Getter

type Getter interface {
	Get(req *Request) (resp *http.Response, body []byte, err error)
}

Getter interface which holds the Get method for sending get requests to Discord

type Patcher

type Patcher interface {
	Patch(req *Request) (resp *http.Response, body []byte, err error)
}

Patcher interface which holds the Patch method for sending patch requests to Discord

type Poster

type Poster interface {
	Post(req *Request) (resp *http.Response, body []byte, err error)
}

Poster interface which holds the Post method for sending post requests to Discord

type Puter

type Puter interface {
	Put(req *Request) (resp *http.Response, body []byte, err error)
}

Puter interface which holds the Put method for sending put requests to Discord

type RateLimit

type RateLimit struct {
	// contains filtered or unexported fields
}

RateLimit ...

func NewRateLimit

func NewRateLimit() *RateLimit

NewRateLimit creates a new rate limit manager

func (*RateLimit) Bucket

func (r *RateLimit) Bucket(key string) *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

func (r *RateLimit) RateLimitTimeout(key string) int64

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

func (r *RateLimit) RateLimited(key string) bool

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 (r *RateLimit) RequestPermit(key string) (timeout time.Duration, err error)

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.

func (*RateLimit) WaitTime

func (r *RateLimit) WaitTime(req *Request) time.Duration

WaitTime get's the remaining time before another request can be made. returns a time.Duration of milliseconds.

type RateLimitAdjuster added in v0.8.7

type RateLimitAdjuster func(timeout time.Duration) time.Duration

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

type Requester

type Requester interface {
	Request(req *Request) (resp *http.Response, body []byte, err error)
	Getter
	Poster
	Puter
	Patcher
	Deleter
}

Requester holds all the sub-request interface for Discord interaction

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL