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 DecodeCustom(d Decoder, r io.Reader, v any) 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 ServeInternalServerError(w http.ResponseWriter, err string)
- func ServeNotFound(w http.ResponseWriter, err string)
- func ServeOKResponseJSON(w http.ResponseWriter, r *http.Request, res any)
- func SetDefaultSerializer(s Serializer)
- func Unmarshal(data []byte, v any) error
- func UnmarshalCustom(u Unmarshaler, data []byte, v any) 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
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ServeInternalServerError ¶
func ServeInternalServerError(w http.ResponseWriter, err string)
func ServeNotFound ¶
func ServeNotFound(w http.ResponseWriter, err string)
func ServeOKResponseJSON ¶
func ServeOKResponseJSON(w http.ResponseWriter, r *http.Request, res any)
ServeOKResponseJSON 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 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
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" )
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
Click to show internal directories.
Click to hide internal directories.