http

package
v1.11.5 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2024 License: MIT Imports: 20 Imported by: 3

Documentation

Index

Constants

View Source
const PlatformErrorCodeHeader = "X-Platform-Error-Code"

PlatformErrorCodeHeader shows the error code of platform error.

Variables

This section is empty.

Functions

func CheckError

func CheckError(resp *http.Response) (err error)

CheckError reads the http.Response and returns an error if one exists. It will automatically recognize the errors returned by Influx services and decode the error into an internal error type. If the error cannot be determined in that way, it will create a generic error message.

If there is no error, then this returns nil.

func CheckErrorStatus

func CheckErrorStatus(code int, res *http.Response) error

CheckErrorStatus for status and any error in the response.

func ErrorCodeToStatusCode

func ErrorCodeToStatusCode(ctx context.Context, code string) int

ErrorCodeToStatusCode maps an influxdb error code string to a http status code integer.

func OrgIDFromContext

func OrgIDFromContext(ctx context.Context) *platform.ID

OrgIDFromContext ....

func SetCORS

func SetCORS(next http.Handler) http.Handler

func SkipOptions

func SkipOptions(next http.Handler) http.Handler

func StatusCodeToErrorCode

func StatusCodeToErrorCode(statusCode int) string

StatusCodeToErrorCode maps a http status code integer to an influxdb error code string.

func UserAgent

func UserAgent(r *http.Request) string

func WriteErrorResponse

func WriteErrorResponse(ctx context.Context, w http.ResponseWriter, code string, msg string)

Types

type API

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

API provides a consolidated means for handling API interface concerns. Concerns such as decoding/encoding request and response bodies as well as adding headers for content type and content encoding.

func NewAPI

func NewAPI(opts ...APIOptFn) *API

NewAPI creates a new API type.

func (*API) DecodeGob

func (a *API) DecodeGob(r io.Reader, v interface{}) error

DecodeGob decodes reader with gob.

func (*API) DecodeJSON

func (a *API) DecodeJSON(r io.Reader, v interface{}) error

DecodeJSON decodes reader with json.

func (*API) Err

func (a *API) Err(w http.ResponseWriter, r *http.Request, err error)

Err is used for writing an error to the response.

func (*API) Respond

func (a *API) Respond(w http.ResponseWriter, r *http.Request, status int, v interface{})

Respond writes to the response writer, handling all errors in writing.

func (*API) Write

func (a *API) Write(w http.ResponseWriter, status int, b []byte)

Write allows the user to write raw bytes to the response writer. This operation does not have a fail case, all failures here will be logged.

type APIOptFn

type APIOptFn func(*API)

APIOptFn is a functional option for setting fields on the API type.

func WithEncodeGZIP

func WithEncodeGZIP() APIOptFn

WithEncodeGZIP sets the encoder to gzip contents.

func WithErrFn

func WithErrFn(fn func(ctx context.Context, err error) (interface{}, int, error)) APIOptFn

WithErrFn sets the err handling func for issues when writing to the response body.

func WithLog

func WithLog(logger *zap.Logger) APIOptFn

WithLog sets the logger.

func WithOKErrFn

func WithOKErrFn(fn func(err error) error) APIOptFn

WithOKErrFn is an error handler for failing validation for request bodies.

func WithPrettyJSON

func WithPrettyJSON(b bool) APIOptFn

WithPrettyJSON sets the json encoder to marshal indent or not.

func WithUnmarshalErrFn

func WithUnmarshalErrFn(fn func(encoding string, err error) error) APIOptFn

WithUnmarshalErrFn sets the error handler for errors that occur when unmarshalling the request body.

type ErrBody

type ErrBody struct {
	Code string `json:"code"`
	Msg  string `json:"message"`
}

ErrBody is an err response body.

type ErrorHandler

type ErrorHandler int

ErrorHandler is the error handler in http package.

func (ErrorHandler) HandleHTTPError

func (h ErrorHandler) HandleHTTPError(ctx context.Context, err error, w http.ResponseWriter)

HandleHTTPError encodes err with the appropriate status code and format, sets the X-Platform-Error-Code headers on the response. We're no longer using X-Influx-Error and X-Influx-Reference. and sets the response status to the corresponding status code.

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware constructor.

func Metrics

func Metrics(name string, reqMetric *prometheus.CounterVec, durMetric *prometheus.HistogramVec) Middleware

func Trace

func Trace(name string) Middleware

func ValidResource

func ValidResource(api *API, lookupOrgByResourceID func(context.Context, platform.ID) (platform.ID, error)) Middleware

ValidResource make sure a resource exists when a sub system needs to be mounted to an api

type OrgContext

type OrgContext string
const CtxOrgKey OrgContext = "orgID"

type ResourceHandler

type ResourceHandler interface {
	Prefix() string
	http.Handler
}

ResourceHandler is an HTTP handler for a resource. The prefix describes the url path prefix that relates to the handler endpoints.

type StatusResponseWriter

type StatusResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func NewStatusResponseWriter

func NewStatusResponseWriter(w http.ResponseWriter) *StatusResponseWriter

func (*StatusResponseWriter) Code

func (w *StatusResponseWriter) Code() int

func (*StatusResponseWriter) ResponseBytes

func (w *StatusResponseWriter) ResponseBytes() int

func (*StatusResponseWriter) StatusCodeClass

func (w *StatusResponseWriter) StatusCodeClass() string

func (*StatusResponseWriter) Write

func (w *StatusResponseWriter) Write(b []byte) (int, error)

func (*StatusResponseWriter) WriteHeader

func (w *StatusResponseWriter) WriteHeader(statusCode int)

WriteHeader writes the header and captures the status code.

Jump to

Keyboard shortcuts

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