Documentation
¶
Overview ¶
Package rest provides service-wide utils and convenience helpers to be used in cross-service requests
Index ¶
- func CopyHeader(dst *http.Header, src http.Header)
- func Decode(r io.Reader, v any) error
- func DecodeAsType[T any](r io.Reader) (T, error)
- func DecodeCustom(d Decoder, r io.Reader, v any) error
- func DecodeCustomAsType[T any](d Decoder, r io.Reader) (T, error)
- func Encode(w io.Writer, data any) error
- func EncodeCustom(e Encoder, w io.Writer, data any) error
- func JSONRequest(ctx context.Context, method string, url string, input any, output any, ...) (int, error)
- func JSONResponse(w http.ResponseWriter, resp *Response, code int)
- func JSONResponseRaw(w http.ResponseWriter, payload any, code int)
- func Marshal(v any) ([]byte, error)
- func MarshalCustom(m Marshaler, v any) ([]byte, error)
- func ServeBadRequest(w http.ResponseWriter, err string)
- func ServeBadRequestMalformedPayload(w http.ResponseWriter)
- func ServeBadRequestWithPayload(w http.ResponseWriter, err string, payload any)
- func ServeForbidden(w http.ResponseWriter, err string)
- func ServeInternalServerError(w http.ResponseWriter, err string)
- func ServeNotFound(w http.ResponseWriter, err string)
- func ServeOK(w http.ResponseWriter, res any)
- func ServeOKCached(w http.ResponseWriter, r *http.Request, res any)
- func ServeUnauthorized(w http.ResponseWriter, err string)
- func ServeValidationFailed(w http.ResponseWriter, err ValidationErrors)
- func SetDefaultSerializer(s Serializer)
- func Unmarshal(data []byte, v any) error
- func UnmarshalCustom(u Unmarshaler, data []byte, v any) error
- func UnmarshalCustomAsType[T any](u Unmarshaler, data []byte) (T, error)
- type Decoder
- type Encoder
- type ErrorCode
- type Marshaler
- type RawResponse
- type RequestFailedError
- func NewRequestCreationError(method string, url string, payload io.Reader, err error) *RequestFailedError
- func NewRequestExecutionError(method string, url string, payload io.Reader, err error) *RequestFailedError
- func NewRequestParsingError(method string, url string, requestPayload io.Reader, responsePayload io.Reader, ...) *RequestFailedError
- type RequestOption
- func WithAdditionalHeaders(h http.Header) RequestOption
- func WithCheckRetry(cr retryablehttp.CheckRetry) RequestOption
- func WithClient(c *http.Client) RequestOption
- func WithDecoder(d Decoder) RequestOption
- func WithEncoder(e Encoder) RequestOption
- func WithHeaders(h http.Header) RequestOption
- func WithRetries(r int) RequestOption
- func WithSerializer(s Serializer) RequestOption
- type Response
- type ResponseStatus
- type Serializer
- type Unmarshaler
- type ValidationError
- type ValidationErrors
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeCustomAsType ¶ added in v1.0.10
func JSONRequest ¶ added in v1.0.8
func JSONResponse ¶
func JSONResponse(w http.ResponseWriter, resp *Response, code int)
func JSONResponseRaw ¶
func JSONResponseRaw(w http.ResponseWriter, payload any, code int)
func ServeBadRequest ¶
func ServeBadRequest(w http.ResponseWriter, err string)
func ServeBadRequestMalformedPayload ¶
func ServeBadRequestMalformedPayload(w http.ResponseWriter)
func ServeBadRequestWithPayload ¶ added in v1.0.10
func ServeBadRequestWithPayload(w http.ResponseWriter, err string, payload any)
func ServeForbidden ¶ added in v1.0.10
func ServeForbidden(w http.ResponseWriter, err string)
func ServeInternalServerError ¶
func ServeInternalServerError(w http.ResponseWriter, err string)
func ServeNotFound ¶
func ServeNotFound(w http.ResponseWriter, err string)
func ServeOK ¶ added in v1.0.10
func ServeOK(w http.ResponseWriter, res any)
ServeOK serves http.StatusOK Response with provided payload.
func ServeOKCached ¶ added in v1.0.10
func ServeOKCached(w http.ResponseWriter, r *http.Request, res any)
ServeOKCached serves http.StatusOK Response with provided payload, optionally handling ETag match
Will serve http.StatusNotModified with no body if provided ETag is equal to computed, otherwise will serve http.StatusOK with body.
func ServeUnauthorized ¶ added in v1.0.10
func ServeUnauthorized(w http.ResponseWriter, err string)
func ServeValidationFailed ¶ added in v1.0.10
func ServeValidationFailed(w http.ResponseWriter, err ValidationErrors)
func SetDefaultSerializer ¶ added in v1.0.8
func SetDefaultSerializer(s Serializer)
func UnmarshalCustom ¶ added in v1.0.8
func UnmarshalCustom(u Unmarshaler, data []byte, v any) error
func UnmarshalCustomAsType ¶ added in v1.0.10
func UnmarshalCustomAsType[T any](u Unmarshaler, data []byte) (T, error)
Types ¶
type ErrorCode ¶
type ErrorCode string
const ( InternalServerErrorCode ErrorCode = "INTERNAL_SERVER_ERROR" BadRequestErrorCode ErrorCode = "BAD_REQUEST" NotFoundErrorCode ErrorCode = "NOT_FOUND" TimeoutErrorCode ErrorCode = "TIMEOUT" ForbiddenErrorCode ErrorCode = "FORBIDDEN" InvalidSessionErrorCode ErrorCode = "INVALID_SESSION" ConflictErrorCode ErrorCode = "CONFLICT" ValidationFailedErrorCode ErrorCode = "VALIDATION_FAILED" )
type RawResponse ¶
type RawResponse struct {
Status int
Body *bytes.Buffer
Header http.Header
// contains filtered or unexported fields
}
func (*RawResponse) Close ¶
func (r *RawResponse) Close()
type RequestFailedError ¶
type RequestFailedError struct {
// contains filtered or unexported fields
}
func NewRequestCreationError ¶
func NewRequestParsingError ¶
func (*RequestFailedError) Error ¶
func (e *RequestFailedError) Error() string
func (*RequestFailedError) Is ¶
func (e *RequestFailedError) Is(target error) bool
func (*RequestFailedError) LogValue ¶
func (e *RequestFailedError) LogValue() slog.Value
func (*RequestFailedError) Unwrap ¶
func (e *RequestFailedError) Unwrap() error
type RequestOption ¶ added in v1.0.8
type RequestOption func(*requestOptions)
func WithAdditionalHeaders ¶ added in v1.0.8
func WithAdditionalHeaders(h http.Header) RequestOption
func WithCheckRetry ¶ added in v1.0.8
func WithCheckRetry(cr retryablehttp.CheckRetry) RequestOption
func WithClient ¶ added in v1.0.8
func WithClient(c *http.Client) RequestOption
func WithDecoder ¶ added in v1.0.8
func WithDecoder(d Decoder) RequestOption
func WithEncoder ¶ added in v1.0.8
func WithEncoder(e Encoder) RequestOption
func WithHeaders ¶ added in v1.0.8
func WithHeaders(h http.Header) RequestOption
func WithRetries ¶ added in v1.0.8
func WithRetries(r int) RequestOption
func WithSerializer ¶ added in v1.0.8
func WithSerializer(s Serializer) RequestOption
type Response ¶
type Response struct {
Status ResponseStatus `json:"status"`
ErrorCode ErrorCode `json:"errorCode,omitempty"`
Error string `json:"error,omitempty"`
Payload any `json:"payload,omitempty"`
}
type ResponseStatus ¶
type ResponseStatus string
const ( APIResponseStatusOk ResponseStatus = "ok" APIResponseStatusError ResponseStatus = "error" )
type Serializer ¶ added in v1.0.8
type Serializer interface {
Marshaler
Unmarshaler
Encoder
Decoder
}
func DefaultSerializer ¶ added in v1.0.8
func DefaultSerializer() Serializer
type Unmarshaler ¶ added in v1.0.8
type ValidationError ¶ added in v1.0.10
type ValidationError struct {
Field string `json:"field"`
Reason string `json:"reason"`
Meta json.RawMessage `json:"meta,omitempty"`
}
func (*ValidationError) Error ¶ added in v1.0.10
func (e *ValidationError) Error() string
func (*ValidationError) LogValue ¶ added in v1.0.10
func (e *ValidationError) LogValue() slog.Value
type ValidationErrors ¶ added in v1.0.10
type ValidationErrors []ValidationError
func (ValidationErrors) Error ¶ added in v1.0.10
func (e ValidationErrors) Error() string
func (ValidationErrors) LogValue ¶ added in v1.0.10
func (e ValidationErrors) LogValue() slog.Value
func (ValidationErrors) Unwrap ¶ added in v1.0.10
func (e ValidationErrors) Unwrap() []error
Click to show internal directories.
Click to hide internal directories.