request

package
v0.2.69 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package request implements reflection-based net/http request decoder.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrJSONExpected        = errors.New("request with application/json content type expected")
	ErrMissingRequestBody  = errors.New("missing request body")
	ErrMissingRequiredFile = errors.New("missing required file")
)

These errors may be returned on request decoding failure.

Functions

func DecoderMiddleware

func DecoderMiddleware(factory DecoderMaker) func(http.Handler) http.Handler

DecoderMiddleware sets up request decoder in suitable handlers.

func ValidatorMiddleware

func ValidatorMiddleware(factory rest.RequestValidatorFactory) func(http.Handler) http.Handler

ValidatorMiddleware sets up request validator in suitable handlers.

Types

type DecoderFactory

type DecoderFactory struct {
	// ApplyDefaults enables default value assignment for fields missing explicit value in request.
	// Default value is retrieved from `default` field tag.
	ApplyDefaults bool

	// JSONReader allows custom JSON decoder for request body.
	// If not set encoding/json.Decoder is used.
	JSONReader func(rd io.Reader, v interface{}) error

	// JSONSchemaReflector is optional, it is called to infer "default" values.
	JSONSchemaReflector *jsonschema.Reflector
	// contains filtered or unexported fields
}

DecoderFactory decodes http requests.

Please use NewDecoderFactory to create instance.

func NewDecoderFactory

func NewDecoderFactory() *DecoderFactory

NewDecoderFactory creates request decoder factory.

func (*DecoderFactory) MakeDecoder

func (df *DecoderFactory) MakeDecoder(
	method string,
	input interface{},
	customMapping rest.RequestMapping,
) nethttp.RequestDecoder

MakeDecoder creates request.RequestDecoder for a http method and request structure.

Input is checked for `json`, `file` tags only for methods with body semantics (POST, PUT, PATCH) or if input implements openapi.RequestBodyEnforcer.

CustomMapping can be nil, otherwise it is used instead of field tags to match decoded fields with struct.

func (*DecoderFactory) RegisterFunc

func (df *DecoderFactory) RegisterFunc(fn form.DecodeFunc, types ...interface{})

RegisterFunc adds custom type handling.

func (*DecoderFactory) SetDecoderFunc

func (df *DecoderFactory) SetDecoderFunc(tagName rest.ParamIn, d func(r *http.Request) (url.Values, error))

SetDecoderFunc adds custom decoder function for values of particular field tag name.

type DecoderFunc

type DecoderFunc func(r *http.Request, input interface{}, validator rest.Validator) error

DecoderFunc implements RequestDecoder with a func.

func (DecoderFunc) Decode

func (df DecoderFunc) Decode(r *http.Request, input interface{}, validator rest.Validator) error

Decode implements RequestDecoder.

type DecoderMaker

type DecoderMaker interface {
	MakeDecoder(method string, input interface{}, customMapping rest.RequestMapping) nethttp.RequestDecoder
}

DecoderMaker creates request decoder for particular structured Go input value.

type EmbeddedSetter

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

EmbeddedSetter can capture *http.Resuest in your input structure.

func (*EmbeddedSetter) Request

func (e *EmbeddedSetter) Request() *http.Request

Request is an accessor.

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/dikac/swaggest-rest/request"
)

func main() {
	type MyRequest struct {
		request.EmbeddedSetter

		Foo int    `header:"X-Foo"`
		Bar string `formData:"bar"`
		Baz bool   `query:"baz"`
	}

	// A decoder for particular structure, can be reused for multiple HTTP requests.
	myDecoder := request.NewDecoderFactory().MakeDecoder(http.MethodPost, new(MyRequest), nil)

	// Request and response writer from ServeHTTP.
	var (
		rw  http.ResponseWriter
		req *http.Request
	)

	// This code would presumably live in ServeHTTP.
	var myReq MyRequest

	if err := myDecoder.Decode(req, &myReq, nil); err != nil {
		http.Error(rw, err.Error(), http.StatusBadRequest)
	}

	// Access data from raw request.
	fmt.Println("Remote Addr:", myReq.Request().RemoteAddr)
}
Output:

func (*EmbeddedSetter) SetRequest

func (e *EmbeddedSetter) SetRequest(r *http.Request)

SetRequest implements Setter.

type Loader

type Loader interface {
	LoadFromHTTPRequest(r *http.Request) error
}

Loader loads data from http.Request.

Implement this interface on a pointer to your input structure to disable automatic request mapping.

type Setter

type Setter interface {
	SetRequest(r *http.Request)
}

Setter captures original http.Request.

Implement this interface on a pointer to your input structure to get access to http.Request.

Jump to

Keyboard shortcuts

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