httputil

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2020 License: Apache-2.0 Imports: 15 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultErrorMapper = &ErrorMapper{
	errors.ErrInternal: ErrInternalError,

	codec.ErrMarshal:             ErrInternalError,
	codec.ErrUnmarshalDocument:   ErrInvalidJSONDocument,
	codec.ErrCodec:               ErrInvalidInput,
	codec.ErrUnmarshal:           ErrInvalidInput,
	codec.ErrUnmarshalFieldValue: ErrInvalidJSONFieldValue,
	codec.ErrUnmarshalFieldName:  ErrInvalidJSONFieldName,
	query.ErrViolation:           ErrInvalidJSONFieldValue,

	filter.ErrFilter:           ErrInvalidQueryParameter,
	filter.ErrFilterCollection: ErrInvalidResourceName,

	query.ErrFieldValue:         ErrInvalidJSONFieldValue,
	query.ErrInvalidParameter:   ErrInvalidQueryParameter,
	query.ErrInvalidSort:        ErrInvalidQueryParameter,
	query.ErrInvalidFieldSet:    ErrInvalidQueryParameter,
	query.ErrNoFieldsInFieldSet: ErrInvalidQueryParameter,
	query.ErrViolationUnique:    ErrResourceAlreadyExists,
	query.ErrViolationCheck:     ErrInvalidJSONFieldValue,
	query.ErrViolationNotNull:   ErrInvalidInput,

	mapping.ErrModel:                ErrInternalError,
	mapping.ErrRelation:             ErrInternalError,
	mapping.ErrFieldValue:           ErrInvalidJSONFieldValue,
	mapping.ErrFieldNotParser:       ErrInvalidQueryParameter,
	mapping.ErrFieldNotNullable:     ErrForbiddenOperation,
	mapping.ErrModelNotFound:        ErrResourceNotFound,
	mapping.ErrInvalidRelationField: ErrInvalidQueryParameter,

	server.ErrHeaderNotAcceptable:   ErrNotAcceptable,
	server.ErrUnsupportedHeader:     ErrUnsupportedHeader,
	server.ErrMissingRequiredHeader: ErrMissingRequiredHeader,
	server.ErrHeaderValue:           ErrInvalidHeaderValue,

	auth.ErrAccountNotFound:      ErrInvalidAuthenticationInfo,
	auth.ErrAccountAlreadyExists: ErrAccountAlreadyExists,
	auth.ErrInvalidUsername:      ErrInvalidAuthenticationInfo,
	auth.ErrInvalidSecret:        ErrInvalidAuthenticationInfo,
	auth.ErrInvalidPassword:      ErrInvalidAuthenticationInfo,
	auth.ErrTokenRevoked:         ErrInvalidAuthenticationInfo,
	auth.ErrTokenNotValidYet:     ErrInvalidAuthenticationInfo,
	auth.ErrTokenExpired:         ErrInvalidAuthenticationInfo,
	auth.ErrForbidden:            ErrForbiddenAuthorize,
	auth.ErrInvalidRole:          ErrForbiddenAuthorize,
	auth.ErrAuthorizationHeader:  ErrInvalidAuthorizationHeader,
}

DefaultErrorMapper is the default error classification mapper.

View Source
var IDKey = &idKeyStruct{}

IDKey is the context key that should match the value of 'ID' for the handlers requests.

Functions

func CtxGetEndpoint

func CtxGetEndpoint(ctx context.Context) (*server.Endpoint, bool)

CtxGetEndpoint gets the endpoint stored in the given 'ctx' context.

func CtxMustGetID

func CtxMustGetID(ctx context.Context) string

CtxMustGetID gets ID from the context or panics if no id is found there. ID should be in the raw string form.

func CtxSetID

func CtxSetID(ctx context.Context, id string) context.Context

CtxSetID sets the 'id' value in the given 'ctx' and returns it.

func ErrAccountAlreadyExists

func ErrAccountAlreadyExists() *codec.Error

ErrAccountAlreadyExists creating account failed - user already exists.

func ErrAccountDisabled

func ErrAccountDisabled() *codec.Error

ErrAccountDisabled provided account is disabled.

func ErrBadRequest

func ErrBadRequest() *codec.Error

ErrBadRequest is the API error thrown on bad request

func ErrEndpointForbidden

func ErrEndpointForbidden() *codec.Error

ErrEndpointForbidden forbidden access above given API endpoint.

func ErrForbiddenAuthorize

func ErrForbiddenAuthorize() *codec.Error

ErrForbidden server understood the request but refuses to authorize it.

func ErrForbiddenOperation

func ErrForbiddenOperation() *codec.Error

ErrForbiddenOperation server understood the request but current operation is not allowed.

func ErrHashMismatch

func ErrHashMismatch() *codec.Error

ErrHashMismatch returns when the hash value specified in the request didn't match the one stored/computed by the server.

func ErrHeadersNotSupported

func ErrHeadersNotSupported() *codec.Error

ErrHeadersNotSupported errors thrown when the provided HTTP headers are not supported by the server

func ErrIDConflict

func ErrIDConflict() *codec.Error

ErrIDConflict defines an error when the primary field value doesn't match endpoint's defined id.

func ErrInputOutOfRange

func ErrInputOutOfRange() *codec.Error

ErrInputOutOfRange one of the request inputs were out of range.

func ErrInsufficientAccountPermissions

func ErrInsufficientAccountPermissions() *codec.Error

ErrInsufficientAccountPermissions provided account has insufficient permissions for given request.

func ErrInternalError

func ErrInternalError() *codec.Error

ErrInternalError server encountered internal error.

func ErrInvalidAuthenticationInfo

func ErrInvalidAuthenticationInfo() *codec.Error

ErrInvalidAuthenticationInfo defines the error when the authentication fails.

func ErrInvalidAuthorizationHeader

func ErrInvalidAuthorizationHeader() *codec.Error

ErrInvalidAuthorizationHeader server failed to authenticate the request due to invalid authorization header.

func ErrInvalidCredentials

func ErrInvalidCredentials() *codec.Error

ErrInvalidCredentials provided invalid account credentials - access denied.

func ErrInvalidHeaderValue

func ErrInvalidHeaderValue() *codec.Error

ErrInvalidHeaderValue responded when some HTTP header was not in a valid format.

func ErrInvalidInput

func ErrInvalidInput() *codec.Error

ErrInvalidInput returned when provided request input is not valid.

func ErrInvalidJSONDocument

func ErrInvalidJSONDocument() *codec.Error

ErrInvalidJSONDocument error returned when the specified JSON structure is not syntactically valid.

func ErrInvalidJSONFieldName

func ErrInvalidJSONFieldName() *codec.Error

ErrInvalidJSONFieldName error returned when one or more of the specified JSON fields was not in a correct format.

func ErrInvalidJSONFieldValue

func ErrInvalidJSONFieldValue() *codec.Error

ErrInvalidJSONFieldValue error returned when one or more of the specified JSON fields was not in a correct format.

func ErrInvalidQueryParameter

func ErrInvalidQueryParameter() *codec.Error

ErrInvalidQueryParameter one of the query parameters has invalid value or format.

func ErrInvalidResourceName

func ErrInvalidResourceName() *codec.Error

ErrInvalidResourceName defines an error when the specified resource name is not valid.

func ErrInvalidURI

func ErrInvalidURI() *codec.Error

ErrInvalidURI error returned when the URI is not recognized by the server.

func ErrLanguageHeaderNotAcceptable

func ErrLanguageHeaderNotAcceptable() *codec.Error

ErrLanguageHeaderNotAcceptable provided request headers contains not supported language.

func ErrLanguageNotAcceptable

func ErrLanguageNotAcceptable() *codec.Error

ErrLanguageNotAcceptable languages provided in the request are not supported.

func ErrMetadataTooLarge

func ErrMetadataTooLarge() *codec.Error

ErrMetadataTooLarge the size of the specified metadata exceeds the limits.

func ErrMethodNotAllowed

func ErrMethodNotAllowed() *codec.Error

ErrMethodNotAllowed given method is not allowed for the specified URI

func ErrMissingRequiredHeader

func ErrMissingRequiredHeader() *codec.Error

ErrMissingRequiredHeader one of the required HTTP headers were not specified in the request.

func ErrMissingRequiredModelField

func ErrMissingRequiredModelField() *codec.Error

ErrMissingRequiredModelField one of the required model fields were not specified in the request body

func ErrMissingRequiredQueryParameter

func ErrMissingRequiredQueryParameter() *codec.Error

ErrMissingRequiredQueryParameter returns when one or more of the query parameter is missing for the request.

func ErrNotAcceptable

func ErrNotAcceptable() *codec.Error

ErrNotAcceptable one of the header contains values that are not possible to get the response by the server.

func ErrOperationTimedOut

func ErrOperationTimedOut() *codec.Error

ErrOperationTimedOut the operation could not be completed within the permitted time.

func ErrQueryParameterValueOutOfRange

func ErrQueryParameterValueOutOfRange() *codec.Error

ErrQueryParameterValueOutOfRange one of the specified query parameters in the request URI is outside the permissible range.

func ErrRequestBodyTooLarge

func ErrRequestBodyTooLarge() *codec.Error

ErrRequestBodyTooLarge the size of the request body exceeds the maximum permitted size.

func ErrResourceAlreadyExists

func ErrResourceAlreadyExists() *codec.Error

ErrResourceAlreadyExists the specified resource already exists.

func ErrResourceNotFound

func ErrResourceNotFound() *codec.Error

ErrResourceNotFound provided resource doesn't exists.

func ErrServiceUnavailable

func ErrServiceUnavailable() *codec.Error

ErrServiceUnavailable the server is currently unable to receive requests.

func ErrTooManyOperationsPerAccount

func ErrTooManyOperationsPerAccount() *codec.Error

ErrTooManyOperationsPerAccount too many requests for given account.

func ErrTypeConflict

func ErrTypeConflict() *codec.Error

ErrTypeConflict defines an error when the data 'type' doesn't match endpoint's defined 'type'.

func ErrUnsupportedField

func ErrUnsupportedField() *codec.Error

ErrUnsupportedField one of the fields specified in the request is not supported.

func ErrUnsupportedFilterOperator

func ErrUnsupportedFilterOperator() *codec.Error

ErrUnsupportedFilterOperator one of the filter operators is not supported.

func ErrUnsupportedHeader

func ErrUnsupportedHeader() *codec.Error

ErrUnsupportedHeader one of the HTTP headers specified in the request is not supported.

func ErrUnsupportedQueryParameter

func ErrUnsupportedQueryParameter() *codec.Error

ErrUnsupportedQueryParameter one of the query parameters in the request URI is not supported.

func ErrWarningNotification

func ErrWarningNotification() *codec.Error

ErrWarningNotification warns on response with some value.

func GetCodec

func GetCodec(ctx context.Context) (codec.Codec, bool)

GetCodec gets the codec from the context.

func MapError

func MapError(err error) []*codec.Error

MapError maps the 'err' input error into slice of 'Error'. The function uses DefaultErrorMapper for error mapping. The logic is the same as for DefaultErrorMapper.Errors method.

func MidStoreEndpoint

func MidStoreEndpoint(endpoint *server.Endpoint) server.Middleware

MidStoreEndpoint is a middleware that stores the endpoint in the request context.

func SetCodec

func SetCodec(ctx context.Context, c codec.Codec) context.Context

SetCodec sets the codec in the context.

func Wrap

func Wrap(next http.Handler) httprouter.Handle

Wrap stores the httprouter params in the context.Context and handles std http handler.

Types

type ErrFunc

type ErrFunc func() *codec.Error

ErrFunc is the function used to create new Error instance.

type ErrorMapper

type ErrorMapper map[error]ErrFunc

ErrorMapper is the neuron errors classification mapper. It creates the 'Error' from the provided error.

func (*ErrorMapper) Errors

func (c *ErrorMapper) Errors(err error) []*codec.Error

Errors gets the slice of 'Error' from the provided 'err' error. The mapping is based on the 'most specific classification first' method. If the error is 'errors.ClassError' the function gets it's class. The function checks classification occurrence based on the priority:

  • Class
  • Minor
  • Major

If no mapping is provided for given classification - an internal error is returned.

type QualityValue

type QualityValue struct {
	Value   string
	Quality float64
}

QualityValue is the structure that contains quality - value pair.

func ParseAcceptEncodingHeader

func ParseAcceptEncodingHeader(header http.Header) []QualityValue

ParseAcceptEncodingHeader parses 'Accept-Encoding' header values sorted by it's quality.

func ParseAcceptHeader

func ParseAcceptHeader(header http.Header) []QualityValue

ParseAcceptHeader parses 'Accept' header values sorted by it's quality.

type ResponseWriter

type ResponseWriter struct {
	Status         int
	TempWriter     io.Writer
	Buffer         *bytes.Buffer
	ResponseWriter http.ResponseWriter
}

ResponseWriter is a wrapper implementation for the http.ResponseWriter. It allows to have buffered writer and status. It needs to be used in a middleware so that it could manipulate the output with some compressor or react differently on panic. Look at middleware.ResponseWriter how it is used.

func (*ResponseWriter) Header

func (r *ResponseWriter) Header() http.Header

Header implements http.ResponseWriter.

func (*ResponseWriter) Write

func (r *ResponseWriter) Write(bytes []byte) (int, error)

Write implements io.Writer.

func (*ResponseWriter) WriteHeader

func (r *ResponseWriter) WriteHeader(statusCode int)

WriteHeader implements http.ResponseWriter.

Jump to

Keyboard shortcuts

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