rest

package
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package rest provides service-wide utils and convenience helpers to be used in cross-service requests

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyHeader

func CopyHeader(dst *http.Header, src http.Header)

func Decode

func Decode(r io.Reader, v any) error

func DecodeAsType added in v1.0.10

func DecodeAsType[T any](r io.Reader) (T, error)

func DecodeCustom added in v1.0.8

func DecodeCustom(d Decoder, r io.Reader, v any) error

func DecodeCustomAsType added in v1.0.10

func DecodeCustomAsType[T any](d Decoder, r io.Reader) (T, error)

func Encode

func Encode(w io.Writer, data any) error

func EncodeCustom added in v1.0.8

func EncodeCustom(e Encoder, w io.Writer, data any) error

func JSONRequest added in v1.0.8

func JSONRequest(
	ctx context.Context,
	method string,
	url string,
	input any,
	output any,
	errorResp any,
	opts ...RequestOption,
) (int, error)

func JSONResponse

func JSONResponse(w http.ResponseWriter, resp *Response, code int)

func JSONResponseRaw

func JSONResponseRaw(w http.ResponseWriter, payload any, code int)

func Marshal

func Marshal(v any) ([]byte, error)

func MarshalCustom added in v1.0.8

func MarshalCustom(m Marshaler, v any) ([]byte, error)

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 Unmarshal

func Unmarshal(data []byte, v any) error

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 Decoder added in v1.0.8

type Decoder interface {
	Decode(r io.Reader, dst any) error
}

type Encoder added in v1.0.8

type Encoder interface {
	Encode(w io.Writer, src any) error
}

type ErrorCode

type ErrorCode string
const (
	InternalServerErrorCode   ErrorCode = "INTERNAL_SERVER_ERROR"
	BadRequestErrorCode       ErrorCode = "BAD_REQUEST"
	NotFoundErrorCode         ErrorCode = "NOT_FOUND"
	TimeoutErrorCode          ErrorCode = "TIMEOUT"
	UnauthorizedErrorCode     ErrorCode = "UNAUTHORIZED"
	ForbiddenErrorCode        ErrorCode = "FORBIDDEN"
	InvalidSessionErrorCode   ErrorCode = "INVALID_SESSION"
	ConflictErrorCode         ErrorCode = "CONFLICT"
	ValidationFailedErrorCode ErrorCode = "VALIDATION_FAILED"
)

type Marshaler added in v1.0.8

type Marshaler interface {
	Marshal(data any) ([]byte, error)
}

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 NewRequestCreationError(
	method string,
	url string,
	payload io.Reader,
	err error,
) *RequestFailedError

func NewRequestExecutionError

func NewRequestExecutionError(
	method string,
	url string,
	payload io.Reader,
	err error,
) *RequestFailedError

func NewRequestParsingError

func NewRequestParsingError(
	method string,
	url string,
	requestPayload io.Reader,
	responsePayload io.Reader,
	err error,
) *RequestFailedError

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 Unmarshaler interface {
	Unmarshal(data []byte, dst any) error
}

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

Jump to

Keyboard shortcuts

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