httd

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2018 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"
)
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 = ""
)

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 GlobalRateLimitSafe

func GlobalRateLimitSafe(resp *http.Response, body *ratelimitBody) bool

func RateLimited

func RateLimited(resp *http.Response) bool

func SupportsDiscordAPIVersion

func SupportsDiscordAPIVersion(version int) bool

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

Types

type Bucket

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

type Client

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

func NewClient

func NewClient(conf *Config) *Client

func (*Client) Delete

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

func (*Client) Get

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

func (*Client) Patch

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

func (*Client) Post

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

func (*Client) Put

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

func (*Client) RateLimiter

func (c *Client) RateLimiter() RateLimiter

func (*Client) Request

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

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
}

type Deleter

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

type Details

type Details struct {
	Ratelimiter     string
	Endpoint        string // always as a suffix to Ratelimiter(!)
	ResponseStruct  interface{}
	SuccessHttpCode int
}

type Getter

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

type Patcher

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

type Poster

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

type Puter

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

type RateLimit

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

RateLimit TODO: a bucket is created for every request. Might want to delete them after a while. seriously. `/users/1` has the same ratelimiter as `/users/2` but any major endpoint prefix does not: `/channels/1` != `/channels/2`

func NewRateLimit

func NewRateLimit() *RateLimit

func (*RateLimit) Bucket

func (r *RateLimit) Bucket(key string) *Bucket

func (*RateLimit) RateLimitTimeout

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

func (*RateLimit) RateLimited

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

func (*RateLimit) UpdateRegisters

func (r *RateLimit) UpdateRegisters(key string, resp *http.Response, content []byte)

TODO: rewrite

func (*RateLimit) WaitTime

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

WaitTime check both the global and route specific rate limiter bucket before sending any REST requests

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:"-"`
}

func ExtractRateLimitInfo

func ExtractRateLimitInfo(resp *http.Response, body []byte) (info *RateLimitInfo, err error)

type RateLimiter

type RateLimiter interface {
	Bucket(key string) *Bucket
	RateLimitTimeout(key string) int64
	RateLimited(key string) bool
	UpdateRegisters(key string, res *http.Response, responseBody []byte)
	WaitTime(req *Request) time.Duration
}

type Request

type Request struct {
	Method      string
	Ratelimiter string
	Endpoint    string
	JSONParams  interface{}
}

type Requester

type Requester interface {
	Request(req *Request) (resp *http.Response, body []byte, err error)
	Get(req *Request) (resp *http.Response, body []byte, err error)
	Post(req *Request) (resp *http.Response, body []byte, err error)
	Put(req *Request) (resp *http.Response, body []byte, err error)
	Patch(req *Request) (resp *http.Response, body []byte, err error)
	Delete(req *Request) (resp *http.Response, body []byte, err error)
}

Jump to

Keyboard shortcuts

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