rest

package
v2.0.0-...-70fcb0c Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// We do not need the attachments endpoint as that is just upload "attachment"
	// Upload
	UploadFile = NewEndpoint(Effis, http.MethodPost, "/<bucket>/")
	// Fetch
	GetFile = NewEndpoint(Effis, http.MethodGet, "/<bucket>/<id>")
	// Fetch Data
	GetFileData = NewEndpoint(Effis, http.MethodGet, "/<bucket>/<id>/data")
	// Download is not needed as that is just Fetch but `Content-Disposition: attachment`.
	// Static Files
	GetStaticFile = NewEndpoint(Effis, http.MethodGet, "/static/<name>")
)

Files

View Source
var (
	// Get Instance Info
	InstanceInfo = NewEndpoint(Oprish, http.MethodGet, "/")
)

Instance

View Source
var (
	// Create Message
	SendMessage = NewEndpoint(Oprish, http.MethodPost, "/messages/")
)

Messaging

Functions

This section is empty.

Types

type CompiledEndpoint

type CompiledEndpoint struct {
	Endpoint *Endpoint
	Url      string
	Path     string
	Type     RequestType
}

CompiledEndpoint represents a compiled endpoint. It contains the endpoint, the constructed URL, the path, and the type.

type Config

type Config struct {
	HttpClient      *http.Client
	RateLimiter     RateLimiter
	RateLimiterOpts []RateLimiterOpt
	ApiUrl          string
	FileUrl         string
}

func DefaultConfig

func DefaultConfig(apiUrl, fileUrl string) *Config

func (*Config) Apply

func (c *Config) Apply(opts []ConfigOpt)

type ConfigOpt

type ConfigOpt func(config *Config)

func WithApiUrl

func WithApiUrl(apiUrl string) ConfigOpt

func WithFileUrl

func WithFileUrl(fileUrl string) ConfigOpt

func WithHttpClient

func WithHttpClient(httpClient *http.Client) ConfigOpt

func WithRateLimiter

func WithRateLimiter(rateLimiter RateLimiter) ConfigOpt

func WithRateLimiterOpts

func WithRateLimiterOpts(opts ...RateLimiterOpt) ConfigOpt

type Endpoint

type Endpoint struct {
	Method string
	Route  string
	Type   RequestType
}

Endpoint represents a single endpoint. It contains all the information needed to construct a request from input.

func NewEndpoint

func NewEndpoint(kind RequestType, method string, path string) *Endpoint

NewEndpoint creates a new endpoint from the given values.

func (*Endpoint) Compile

func (e *Endpoint) Compile(values QueryValues, params ...any) *CompiledEndpoint

Compile the endpoint with the given values and parameters.

type Form

type Form map[string]io.Reader

type QueryValues

type QueryValues map[string]any

func (QueryValues) Encode

func (q QueryValues) Encode() string

Encode the query values into a string via `url.Values`.

type RateLimiter

type RateLimiter interface {
	// MaxRetries is how many times should a request be retried
	// if it fails due to rate limiting?
	MaxRetries() int
	// Reset the rate limiter.
	Reset()
	// WaitBucket waits for the bucket to be available.
	// Returns true if this is the first time the bucket is used,
	// and so the lock must be held over the request.
	WaitBucket(context.Context, *CompiledEndpoint) (bool, error)
	// UnlockBucket unlocks the bucket after a request has been sent.
	// Takes in the boolean returned by WaitBucket.
	UnlockBucket(context.Context, *CompiledEndpoint, *http.Response, bool) error
}

RateLimiter is an interface for handling rate limiting.

func NewRateLimiter

func NewRateLimiter(opts ...RateLimiterOpt) RateLimiter

NewRateLimiter creates a new default rate limiter.

type RateLimiterConfig

type RateLimiterConfig struct {
	// MaxRetries is the maximum number of retries that can be performed
	// before giving up.
	MaxRetries int
	// CleanupInterval is the interval at which the rate limiter will
	// clean up old buckets.
	CleanupInterval time.Duration
}

RateLimiterConfig is the configuration for the rate limiter.

func DefaultRateLimiterConfig

func DefaultRateLimiterConfig() *RateLimiterConfig

DefaultRateLimiterConfig returns a default rate limiter configuration.

func (*RateLimiterConfig) Apply

func (c *RateLimiterConfig) Apply(opts []RateLimiterOpt)

Apply applies the given options to the configuration.

type RateLimiterOpt

type RateLimiterOpt func(config *RateLimiterConfig)

RateLimiterOpt is an option for the rate limiter.

func WithCleanupInterval

func WithCleanupInterval(interval time.Duration) RateLimiterOpt

WithCleanupInterval sets the interval at which the rate limiter will clean up old buckets.

func WithMaxRetries

func WithMaxRetries(maxRetries int) RateLimiterOpt

WithMaxRetries sets the maximum number of retries that can be performed before giving up.

type RequestConfig

type RequestConfig struct {
	Ctx context.Context
}

func DefaultRequestConfig

func DefaultRequestConfig() *RequestConfig

func (*RequestConfig) Apply

func (c *RequestConfig) Apply(opts []RequestOpt)

type RequestOpt

type RequestOpt func(config *RequestConfig)

func WithCtx

func WithCtx(ctx context.Context) RequestOpt

type RequestType

type RequestType int

RequestType represents the destination for the request - oprish or effis.

const (
	Oprish RequestType = iota
	Effis
)

type Rest

type Rest interface {
	Request(endpoint *CompiledEndpoint, data any, response any, opts ...RequestOpt) (*http.Response, error)
	HttpClient() *http.Client
	RateLimiter() RateLimiter
	// Files
	UploadAttachment(file io.Reader, spoiler bool) (models.FileData, error)
	UploadFile(bucket string, fileData io.Reader, spoiler bool) (file models.FileData, err error)
	GetAttachment(id string) (io.ReadCloser, error)
	GetFile(bucket, id string) (io.ReadCloser, error)
	GetAttachmentData(id string) (models.FileData, error)
	GetFileData(bucket, id string) (fileData models.FileData, err error)
	GetStaticFile(name string) (io.ReadCloser, error)
	// Instance
	GetInstanceInfo() (info models.InstanceInfo, err error)
	// Messaging
	CreateMessage(messageCreate types.MessageCreate) (message models.Message, err error)
}

func New

func New(apiUrl, fileUrl string, opts ...ConfigOpt) Rest

Jump to

Keyboard shortcuts

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