rest

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HeedyContext is the context entry used to get the auth context from the request context
	HeedyContext notABasicType = iota
)

Variables

View Source
var ErrNotFound = errors.New("not_found: The given endpoint is not available")
View Source
var QueryDecoder = schema.NewDecoder()

Functions

func APIHeaders added in v0.5.1

func APIHeaders(w http.ResponseWriter)

APIHeaders writes headers that need to be present in all API requests

func NotFoundHandler

func NotFoundHandler(w http.ResponseWriter, r *http.Request)

func RequestLogger

func RequestLogger(r *http.Request) *logrus.Entry

RequestLogger generates a basic logger that holds relevant request info

func URLParam

func URLParam(r *http.Request, param string, err error) (string, error)

func UnmarshalRequest

func UnmarshalRequest(request *http.Request, unmarshalTo interface{}) error

UnmarshalRequest unmarshals the input data to the given interface

func WriteCompress

func WriteCompress(w http.ResponseWriter, r *http.Request, towrite io.Reader, status int) error

WriteCompress compresses a response Reader object if it has an accepted encoding. While it can be a security risk is some cases, it is very useful when the response can be enormous (like timeseries data).

func WriteCompressAsync

func WriteCompressAsync(w http.ResponseWriter, r *http.Request, towrite io.Reader, status int) error

WriteCompressAsync is identical to WriteCompress, but it does compression in another thread, since gzip is cpu-consuming

func WriteGzipJSON

func WriteGzipJSON(w http.ResponseWriter, r *http.Request, data interface{}, err error)

func WriteJSON

func WriteJSON(w http.ResponseWriter, r *http.Request, data interface{}, err error)

WriteJSON writes response as JSON, or writes the error if such is given

func WriteJSONError

func WriteJSONError(w http.ResponseWriter, r *http.Request, status int, err error)

WriteJSONError writes an error message as json. It is assumed that the resulting status code is not StatusOK, but rather 4xx

func WriteJSONStatus

func WriteJSONStatus(w http.ResponseWriter, r *http.Request, data interface{}, status int)

WriteJSONStatus writes json with the given status code

func WriteResult

func WriteResult(w http.ResponseWriter, r *http.Request, err error)

WriteResult writes empty object if the command succeeded, and outputs an error if it didn't

Types

type AsyncWriter

type AsyncWriter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewAsyncWriter

func NewAsyncWriter(w io.Writer) *AsyncWriter

func (*AsyncWriter) Close

func (aw *AsyncWriter) Close() error

func (*AsyncWriter) Write

func (aw *AsyncWriter) Write(p []byte) (n int, err error)

type Context

type Context struct {
	Requester

	Log       *logrus.Entry  // The request's logger
	DB        database.DB    // The authenticated database object
	Events    events.Handler // The event handler (must be set here for plugins)
	RequestID string         // The ID of the original query to the API

	ID     string // The ID sent to plugins in X-Heedy-ID header, and is used for all internal requests
	Plugin string // The plugin that sent the request
}

A Context is generated for all requests, and holds all the info necessary for completing it. This object can be extracted from a request with the CTX function.

func CTX

func CTX(r *http.Request) *Context

CTX gets the heedy request context from an http.Request

type ErrorResponse

type ErrorResponse struct {
	ErrorName        string `json:"error"`
	ErrorDescription string `json:"error_description"`
	ID               string `json:"id,omitempty"`
}

ErrorResponse is the response given by the server upon an error

func NewErrorResponse

func NewErrorResponse(err error) ErrorResponse

func (*ErrorResponse) Error

func (er *ErrorResponse) Error() string

type Requester

type Requester interface {
	Request(c *Context, method, path string, body interface{}, header map[string]string) (io.Reader, error)
}

Jump to

Keyboard shortcuts

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