request

package
v0.18.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2024 License: MIT Imports: 31 Imported by: 18

Documentation

Index

Examples

Constants

View Source
const (
	ErrStatusBadRequest                   = StatusError(http.StatusBadRequest)
	ErrStatusUnauthorized                 = StatusError(http.StatusUnauthorized)
	ErrStatusPaymentRequired              = StatusError(http.StatusPaymentRequired)
	ErrStatusForbidden                    = StatusError(http.StatusForbidden)
	ErrStatusNotFound                     = StatusError(http.StatusNotFound)
	ErrStatusMethodNotAllowed             = StatusError(http.StatusMethodNotAllowed)
	ErrStatusNotAcceptable                = StatusError(http.StatusNotAcceptable)
	ErrStatusProxyAuthRequired            = StatusError(http.StatusProxyAuthRequired)
	ErrStatusRequestTimeout               = StatusError(http.StatusRequestTimeout)
	ErrStatusConflict                     = StatusError(http.StatusConflict)
	ErrStatusGone                         = StatusError(http.StatusGone)
	ErrStatusLengthRequired               = StatusError(http.StatusLengthRequired)
	ErrStatusPreconditionFailed           = StatusError(http.StatusPreconditionFailed)
	ErrStatusRequestEntityTooLarge        = StatusError(http.StatusRequestEntityTooLarge)
	ErrStatusRequestURITooLong            = StatusError(http.StatusRequestURITooLong)
	ErrStatusUnsupportedMediaType         = StatusError(http.StatusUnsupportedMediaType)
	ErrStatusRequestedRangeNotSatisfiable = StatusError(http.StatusRequestedRangeNotSatisfiable)
	ErrStatusExpectationFailed            = StatusError(http.StatusExpectationFailed)
	ErrStatusTeapot                       = StatusError(http.StatusTeapot)
	ErrStatusMisdirectedRequest           = StatusError(http.StatusMisdirectedRequest)
	ErrStatusUnprocessableEntity          = StatusError(http.StatusUnprocessableEntity)
	ErrStatusLocked                       = StatusError(http.StatusLocked)
	ErrStatusFailedDependency             = StatusError(http.StatusFailedDependency)
	ErrStatusTooEarly                     = StatusError(http.StatusTooEarly)
	ErrStatusUpgradeRequired              = StatusError(http.StatusUpgradeRequired)
	ErrStatusPreconditionRequired         = StatusError(http.StatusPreconditionRequired)
	ErrStatusTooManyRequests              = StatusError(http.StatusTooManyRequests)
	ErrStatusRequestHeaderFieldsTooLarge  = StatusError(http.StatusRequestHeaderFieldsTooLarge)
	ErrStatusUnavailableForLegalReasons   = StatusError(http.StatusUnavailableForLegalReasons)
)
View Source
const (
	KB = 1000
	MB = 1000 * KB
	GB = 1000 * MB
	TB = 1000 * GB
	PB = 1000 * TB

	KiB = 1024
	MiB = 1024 * KiB
	GiB = 1024 * MiB
	TiB = 1024 * GiB
	PiB = 1024 * TiB
)

Variables

This section is empty.

Functions

func DIMiddleware added in v0.13.1

func DIMiddleware() router.MiddlewareFunc

func ErrorHandler added in v0.13.1

func ErrorHandler(err error) http.Handler

func HandleErrors

func HandleErrors(handlers ...func(ctx context.Context, err error) http.Handler) router.MiddlewareFunc

func Register added in v0.8.0

func Register(ctx context.Context) error

func Respond added in v0.15.0

func Respond(w http.ResponseWriter, r *http.Request, resp any)

func RespondError added in v0.15.0

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

func Run

func Run(requestHttp *http.Request, requestStruct any) error

func Validate

func Validate(request *http.Request, v any) error

Types

type Converter added in v0.10.3

type Converter func(string) reflect.Value

type File added in v0.11.2

type File struct {
	// contains filtered or unexported fields
}

func (*File) Close added in v0.11.2

func (f *File) Close() error

Close implements fs.File.

func (*File) Read added in v0.11.2

func (f *File) Read(b []byte) (int, error)

Read implements fs.File.

func (*File) Stat added in v0.11.2

func (f *File) Stat() (fs.FileInfo, error)

Stat implements fs.File.

type FileInfo added in v0.11.2

type FileInfo struct {
	// contains filtered or unexported fields
}

func (*FileInfo) IsDir added in v0.11.2

func (f *FileInfo) IsDir() bool

IsDir implements fs.FileInfo.

func (*FileInfo) ModTime added in v0.11.2

func (f *FileInfo) ModTime() time.Time

ModTime implements fs.FileInfo.

func (*FileInfo) Mode added in v0.11.2

func (f *FileInfo) Mode() fs.FileMode

Mode implements fs.FileInfo.

func (*FileInfo) Name added in v0.11.2

func (f *FileInfo) Name() string

Name implements fs.FileInfo.

func (*FileInfo) Size added in v0.11.2

func (f *FileInfo) Size() int64

Size implements fs.FileInfo.

func (*FileInfo) Sys added in v0.11.2

func (f *FileInfo) Sys() any

Sys implements fs.FileInfo.

type HTMLError added in v0.8.0

type HTMLError interface {
	HTMLError() string
}

type HTMLResponse

type HTMLResponse struct {
	ResponseBuilder
}

func NewHTMLResponse

func NewHTMLResponse(data []byte) *HTMLResponse

type HTTPError

type HTTPError struct {
	// contains filtered or unexported fields
}

func NewHTTPError

func NewHTTPError(err error, status int) *HTTPError

func (*HTTPError) Error

func (e *HTTPError) Error() string

func (*HTTPError) Respond

func (e *HTTPError) Respond(w http.ResponseWriter, r *http.Request) error

func (*HTTPError) Status added in v0.13.1

func (e *HTTPError) Status() int

func (*HTTPError) Unwrap added in v0.8.0

func (e *HTTPError) Unwrap() error

func (*HTTPError) WithStack added in v0.11.2

func (e *HTTPError) WithStack()

type JSONResponse

type JSONResponse struct {
	// contains filtered or unexported fields
}

func NewJSONResponse

func NewJSONResponse(data any) *JSONResponse

func (*JSONResponse) AddHeader

func (r *JSONResponse) AddHeader(key, value string) *JSONResponse

func (*JSONResponse) Respond

func (r *JSONResponse) Respond(w http.ResponseWriter, _ *http.Request) error

func (*JSONResponse) SetStatus

func (r *JSONResponse) SetStatus(status int) *JSONResponse

type Message

type Message struct {
	Array   string `json:"array"`
	String  string `json:"string"`
	Numeric string `json:"numeric"`
}

func (*Message) UnmarshalJSON

func (m *Message) UnmarshalJSON(b []byte) error

type MessageOptions

type MessageOptions struct {
	Attribute string
	Value     any
	Arguments []string
	Field     reflect.StructField
}

type RequestHandler

type RequestHandler[TRequest, TResponse any] struct {
	// contains filtered or unexported fields
}

func Handler

func Handler[TRequest, TResponse any](callback func(r *TRequest) (TResponse, error)) *RequestHandler[TRequest, TResponse]
Example (Error)
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/abibby/salusa/request"
)

func main() {
	type ExampleRequest struct {
		A int `query:"a" validate:"min:1"`
	}
	type ExampleResponse struct {
	}

	h := request.Handler(func(r *ExampleRequest) (*ExampleResponse, error) {
		return &ExampleResponse{}, nil
	})

	rw := httptest.NewRecorder()

	h.ServeHTTP(
		rw,
		httptest.NewRequest("GET", "/?a=-1", http.NoBody),
	)

	fmt.Println(rw.Result().StatusCode)
}
Output:

422
Example (Input)
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/abibby/salusa/request"
)

func main() {
	type ExampleRequest struct {
		A int `query:"a"`
		B int `query:"b"`
	}
	type ExampleResponse struct {
		Sum int `json:"sum"`
	}
	h := request.Handler(func(r *ExampleRequest) (*ExampleResponse, error) {
		return &ExampleResponse{
			Sum: r.A + r.B,
		}, nil
	})

	rw := httptest.NewRecorder()

	h.ServeHTTP(
		rw,
		httptest.NewRequest("GET", "/?a=10&b=5", http.NoBody),
	)

	fmt.Println(rw.Body)
}
Output:

{
    "sum": 15
}
Example (PathParams)
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/abibby/salusa/request"
	"github.com/gorilla/mux"
)

func main() {
	type ExampleRequest struct {
		A string `path:"a"`
		B string `path:"b"`
	}
	r := mux.NewRouter()
	r.Handle("/{a}/{b}", request.Handler(func(r *ExampleRequest) (*ExampleRequest, error) {
		return r, nil
	}))

	rw := httptest.NewRecorder()

	r.ServeHTTP(
		rw,
		httptest.NewRequest("GET", "/path_param_a/path_param_b", http.NoBody),
	)

	fmt.Println(rw.Body)
}
Output:

{
    "A": "path_param_a",
    "B": "path_param_b"
}

func (*RequestHandler[TRequest, TResponse]) Docs added in v0.13.1

func (h *RequestHandler[TRequest, TResponse]) Docs(op *spec.OperationProps) *RequestHandler[TRequest, TResponse]

func (*RequestHandler[TRequest, TResponse]) Operation added in v0.13.1

func (h *RequestHandler[TRequest, TResponse]) Operation(ctx context.Context) (*spec.Operation, error)

func (*RequestHandler[TRequest, TResponse]) Run added in v0.8.0

func (h *RequestHandler[TRequest, TResponse]) Run(r *TRequest) (TResponse, error)

func (*RequestHandler[TRequest, TResponse]) ServeHTTP

func (h *RequestHandler[TRequest, TResponse]) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*RequestHandler[TRequest, TResponse]) Validate added in v0.11.0

func (r *RequestHandler[TRequest, TResponse]) Validate(ctx context.Context) error

type Responder

type Responder interface {
	Respond(w http.ResponseWriter, r *http.Request) error
}

type ResponseBuilder added in v0.16.0

type ResponseBuilder struct {
	*http.Response
}

func NewResponse

func NewResponse(body io.Reader) *ResponseBuilder

func (*ResponseBuilder) AddHeader added in v0.16.0

func (r *ResponseBuilder) AddHeader(key, value string) *ResponseBuilder

func (*ResponseBuilder) Headers added in v0.16.0

func (r *ResponseBuilder) Headers() http.Header

func (*ResponseBuilder) Respond added in v0.16.0

func (*ResponseBuilder) SetStatus added in v0.16.0

func (r *ResponseBuilder) SetStatus(status int) *ResponseBuilder

func (*ResponseBuilder) Status added in v0.16.0

func (r *ResponseBuilder) Status() int

type StackFrame added in v0.11.2

type StackFrame struct {
	Call  string `json:"call"`
	File  string `json:"file"`
	Line  int    `json:"line"`
	Extra int    `json:"-"`
}

type StackTrace added in v0.11.2

type StackTrace struct {
	GoRoutine string        `json:"go_routine"`
	Stack     []*StackFrame `json:"stack"`
}

type StatusError added in v0.15.0

type StatusError int

func (StatusError) Error added in v0.15.0

func (e StatusError) Error() string

func (StatusError) Respond added in v0.15.0

func (e StatusError) Respond(w http.ResponseWriter, r *http.Request) error

type ValidationError

type ValidationError map[string][]string

func (ValidationError) AddError

func (e ValidationError) AddError(key string, message string)

func (ValidationError) Error

func (e ValidationError) Error() string

func (ValidationError) HTMLError added in v0.8.0

func (e ValidationError) HTMLError() string

func (ValidationError) HasErrors

func (e ValidationError) HasErrors() bool

func (ValidationError) Merge

func (e ValidationError) Merge(vErr ValidationError)

type Validator

type Validator interface {
	Valid() error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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