request

package
v0.1.15 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2025 License: Apache-2.0 Imports: 12 Imported by: 1

Documentation

Index

Constants

View Source
const (
	DefaultLimit   = 1000
	DefaultCursor  = ""
	ParamCursor    = "cursor"
	ParamLimit     = "limit"
	ParamSort      = "sort"
	ParamSortField = "sortField"
)
View Source
const (
	MaxGzipSize           = 1024 * 1025 * 5
	ContentTypeJSON       = "application/json"
	ContentTypeGzip       = "gzip"
	HeaderContentType     = "Content-Type"
	HeaderContentEncoding = "Content-Encoding"
)
View Source
const (
	MaxBodySize = 1048576 // 1MB
)

Variables

This section is empty.

Functions

func BuildLinkHeaders

func BuildLinkHeaders(r *http.Request, w http.ResponseWriter, serverURLWithProtocol, path string, cursor Cursor) error

BuildLinkHeaders adds pagination Link headers to the HTTP response.

func ErrInvalidRequest

func ErrInvalidRequest(err error) render.Renderer

ErrInvalidRequest creates a ResponseError for invalid requests.

func ErrRender

func ErrRender(err error) render.Renderer

ErrRender creates a ResponseError for rendering errors.

func GetBody

func GetBody(w http.ResponseWriter, r *http.Request, record interface{}) error

GetBody deserializes the request body into the provided record or returns an error.

func HasCode

func HasCode(err error, code int) bool

HasCode checks if the given error has the specified HTTP status code.

func NewHTTPError

func NewHTTPError(err error, code int) error

NewHTTPError creates a new ResponseError with the given error and HTTP status code.

func Param

func Param(r *http.Request, name string) string

Param returns the URL parameter from the request.

func QS

func QS(r *http.Request, name string) string

QS returns the query string parameter from the request.

func QSAll

func QSAll(r *http.Request, name string) []string

QSAll returns all values for the query string parameter from the request.

func QSDefault

func QSDefault(r *http.Request, name string, defaultValue string) string

QSDefault returns the query string parameter or a default value if not present.

func Reply

func Reply(r *http.Request, w http.ResponseWriter, data interface{}, statusCode int)

Reply sends a JSON response with the given data and status code.

func ReplyBytes

func ReplyBytes(r *http.Request, w http.ResponseWriter, data []byte, statusCode int, contentType string)

ReplyBytes sends a response with the given byte data and status code.

func ReplyBytesGzip

func ReplyBytesGzip(r *http.Request, w http.ResponseWriter, data []byte, statusCode int, contentType string)

ReplyBytesGzip sends a gzipped response with the given byte data and status code.

func ReplyErr

func ReplyErr(w http.ResponseWriter, r *http.Request, err error)

ReplyErr sends an error response with the given error.

func ReplyGzip

func ReplyGzip(r *http.Request, w http.ResponseWriter, data interface{}, statusCode int, pretty bool)

ReplyGzip sends a gzipped JSON response with the given data and status code.

func ReplyRaw

func ReplyRaw(r *http.Request, w http.ResponseWriter, src io.Reader, statusCode int, contentType string)

ReplyRaw sends a raw response with the given reader and status code.

func SetResponseHeaders

func SetResponseHeaders(w http.ResponseWriter, headers map[string]string)

SetResponseHeaders sets the given headers on the response.

func WrapRender

func WrapRender(w http.ResponseWriter, r *http.Request, v render.Renderer)

WrapRender wraps the render.Render function and handles errors.

Types

type Cursor

type Cursor struct {
	Prev *string `json:"prev"`
	Next *string `json:"next"`
}

Forward/Backward cursor

type HTTPErrorCoder

type HTTPErrorCoder interface {
	HTTPCode() int
}

type ListResponse

type ListResponse[DataType any] struct {
	Status Result     `json:"status"`
	Cursor Cursor     `json:"cursor"`
	Count  int        `json:"count"`
	Data   []DataType `json:"data"`
}

ListResponse simple class to make standard response objects for list of elements.

type PaginationOptions

type PaginationOptions struct {
	Limit      int
	Cursor     string
	Descending bool
	SortField  *string
}

func GetPagingOpts

func GetPagingOpts(r *http.Request) PaginationOptions

GetPagingOpts extracts pagination options from the HTTP request.

type ResponseError

type ResponseError struct {
	CallerInfo     string `json:"-"` // typically "$file:$line"
	Err            error  `json:"-"` // low-level runtime error
	HTTPStatusCode int    `json:"-"` // http response status code

	StatusText string `json:"status"`          // user-level status message
	AppCode    *int64 `json:"code,omitempty"`  // application-specific error code
	ErrorText  string `json:"error,omitempty"` // application-level error message, for debugging
}

func GetResponseError

func GetResponseError(err error) (re *ResponseError, ok bool)

GetResponseError extracts a ResponseError from a given error.

func NewErrNotFound

func NewErrNotFound() *ResponseError

NewErrNotFound creates a ResponseError for resource not found errors.

func NewErrServer

func NewErrServer() *ResponseError

NewErrServer creates a ResponseError for internal server errors.

func (*ResponseError) AsFields

func (e *ResponseError) AsFields() map[string]interface{}

AsFields returns the ResponseError as zerolog.Fields for structured logging.

func (*ResponseError) Error

func (e *ResponseError) Error() string

Error returns a string representation of the ResponseError.

func (*ResponseError) HTTPCode

func (e *ResponseError) HTTPCode() int

func (*ResponseError) Render

Render sets the HTTP status code for the response.

type Result

type Result struct {
	Success bool   `json:"success"`
	Error   string `json:"error,omitempty"`
}

func NewResult

func NewResult() Result

NewResult creates a new successful Result.

type SingleResponse

type SingleResponse[DataType any] struct {
	Status Result   `json:"status"`
	Data   DataType `json:"data"`
}

SingleResponse simple class to make standard response objects for single element gets

Jump to

Keyboard shortcuts

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