Documentation
¶
Overview ¶
httpio handles encoding and decoding for http i/o. This package is used to standardize request and response handling.
Index ¶
- func Param[T any](r *http.Request, param ParamType) (val T)
- func WithParams(next http.Handler) http.Handler
- type Decoder
- type Encoder
- func (e *Encoder) BadRequest(err error) error
- func (e *Encoder) BadRequestWithMessage(err error, message string) error
- func (e *Encoder) Conflict(err error) error
- func (e *Encoder) ConflictWithMessage(err error, message string) error
- func (e *Encoder) Forbidden(err error) error
- func (e *Encoder) ForbiddenWithMessage(err error, message string) error
- func (e *Encoder) InternalServerError(err error) error
- func (e *Encoder) InternalServerErrorWithMessage(err error, message string) error
- func (e *Encoder) NotFound(err error) error
- func (e *Encoder) NotFoundWithMessage(err error, message string) error
- func (e *Encoder) Ok(body interface{}) error
- func (e *Encoder) ServiceUnavailable(err error) error
- func (e *Encoder) ServiceUnavailableWithMessage(err error, message string) error
- func (e *Encoder) StatusCode(statusCode int, err error) error
- func (e *Encoder) StatusCodeWithMessage(statusCode int, err error, message string) error
- func (e *Encoder) Unauthorized(err error) error
- func (e *Encoder) UnauthorizedWithMessage(err error, message string) error
- type HTTPEncoder
- type MessageResponse
- type ParamType
- type ValidatorFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder is a struct that can be used for decoding http requests and validating those requests
func NewDecoder ¶
func NewDecoder(req *http.Request, validator ValidatorFunc) *Decoder
NewDecoder returns a pointer to a new Decoder struct
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder is a struct that is used for encoding http responses
func NewEncoder ¶
func NewEncoder(w http.ResponseWriter) *Encoder
NewEncoder returns a new Encoder to write to the ResponseWriter This encoder will write to the ResponseWriter using a json encoder.
func (*Encoder) BadRequest ¶
BadRequest writes a http 400 status to the response header and returns the original error
func (*Encoder) BadRequestWithMessage ¶
BadRequestWithMessage writes a bad request with a message body in the response
func (*Encoder) Conflict ¶
Conflict writes a 409 status to the response header and returns the original error
func (*Encoder) ConflictWithMessage ¶
ConflictWithMessage writes a 409 status to the response header and encodes a message in the response body
func (*Encoder) Forbidden ¶
Forbidden writes a 403 status to the response header and returns the original error
func (*Encoder) ForbiddenWithMessage ¶
ForbiddenWithMessage returns a forbidden response with an error message
func (*Encoder) InternalServerError ¶
InternalServerError writes a 500 status to the response header and returns the original error
func (*Encoder) InternalServerErrorWithMessage ¶
InternalServerErrorWithMessage writes a 500 status to the response header and encodes a message in the response body
func (*Encoder) NotFound ¶
NotFound writes a 404 status to the response header and returns the original error
func (*Encoder) NotFoundWithMessage ¶
NotFoundWithMessage writes a 404 status to the response header and encodes a message in the response body
func (*Encoder) ServiceUnavailable ¶
ServiceUnavailable writes a 503 status to the response header and returns the original error
func (*Encoder) ServiceUnavailableWithMessage ¶
ServiceUnavailableWithMessage writes a 503 status to the response header and encodes a message in the response body
func (*Encoder) StatusCode ¶
StatusCode writes a statusCode to the response header and returns the original error
func (*Encoder) StatusCodeWithMessage ¶
StatusCodeWithMessage writes a statusCode and message to the response header and returns the original error
func (*Encoder) Unauthorized ¶
Unauthorized writes a 401 status to the response header and returns the original error
type HTTPEncoder ¶
type HTTPEncoder interface {
// Encode is the call that is made to encode data into a response body and returns an error if it fails
Encode(v interface{}) error
}
HTTPEncoder is an interface that is accepted when encoding http responses
type MessageResponse ¶
type MessageResponse struct {
Message string `json:"message,omitempty"`
}
MessageResponse holds a standard structure for http responses that carry a single message
type ParamType ¶ added in v0.0.2
type ParamType string
ParamType defines the type used to describe url Params
type ValidatorFunc ¶
type ValidatorFunc func(s interface{}) error
ValidatorFunc is a function that validates s It returns an error if the validation fails