Documentation ¶
Overview ¶
Package rest provides http handler for use case interactor to implement REST API.
Index ¶
- Constants
- func HTTPStatusFromCanonicalCode(c status.Code) int
- func OutputHasNoContent(output interface{}) bool
- type ETagged
- type ErrResponse
- type ErrWithAppCode
- type ErrWithCanonicalStatus
- type ErrWithFields
- type ErrWithHTTPStatus
- type HTTPCodeAsError
- type HandlerTrait
- type HandlerWithRoute
- type HandlerWithUseCase
- type JSONSchemaValidator
- type JSONWriterTo
- type OutputWithHTTPStatus
- type ParamIn
- type RequestErrors
- type RequestJSONSchemaProvider
- type RequestMapping
- type RequestValidatorFactory
- type ResponseJSONSchemaProvider
- type ResponseValidatorFactory
- type ValidationErrors
- type Validator
- type ValidatorFunc
Constants ¶
const ( // ParamInPath indicates path parameters, such as `/users/{id}`. ParamInPath = ParamIn("path") // ParamInQuery indicates query parameters, such as `/users?page=10`. ParamInQuery = ParamIn("query") // ParamInBody indicates body value, such as `{"id": 10}`. ParamInBody = ParamIn("body") // ParamInFormData indicates body form parameters. ParamInFormData = ParamIn("formData") // ParamInCookie indicates cookie parameters, which are passed ParamIn the `Cookie` header, // such as `Cookie: debug=0; gdpr=2`. ParamInCookie = ParamIn("cookie") // ParamInHeader indicates header parameters, such as `X-Header: value`. ParamInHeader = ParamIn("header") )
Variables ¶
This section is empty.
Functions ¶
func HTTPStatusFromCanonicalCode ¶
HTTPStatusFromCanonicalCode returns http status accordingly to use case status code.
func OutputHasNoContent ¶
func OutputHasNoContent(output interface{}) bool
OutputHasNoContent indicates if output does not seem to have any content body to render in response.
Types ¶
type ETagged ¶
type ETagged interface {
ETag() string
}
ETagged exposes specific version of resource.
type ErrResponse ¶
type ErrResponse struct { StatusText string `json:"status,omitempty" description:"Status text."` AppCode int `json:"code,omitempty" description:"Application-specific error code."` ErrorText string `json:"error,omitempty" description:"Error message."` Context map[string]interface{} `json:"context,omitempty" description:"Application context."` // contains filtered or unexported fields }
ErrResponse is HTTP error response body.
func Err ¶
func Err(err error) (int, ErrResponse)
Err creates HTTP status code and ErrResponse for error.
You can use it with use case status code:
rest.Err(status.NotFound)
type ErrWithAppCode ¶
ErrWithAppCode exposes application error code.
type ErrWithCanonicalStatus ¶
ErrWithCanonicalStatus exposes canonical status code.
type ErrWithFields ¶
ErrWithFields exposes structured context of error.
type ErrWithHTTPStatus ¶
ErrWithHTTPStatus exposes HTTP status code.
type HTTPCodeAsError ¶
type HTTPCodeAsError int
HTTPCodeAsError exposes HTTP status code as use case error that can be translated to response status.
func (HTTPCodeAsError) Error ¶
func (c HTTPCodeAsError) Error() string
Error return HTTP status text.
func (HTTPCodeAsError) HTTPStatus ¶
func (c HTTPCodeAsError) HTTPStatus() int
HTTPStatus returns HTTP status code.
type HandlerTrait ¶
type HandlerTrait struct { // SuccessStatus is an HTTP status code to set on successful use case interaction. // // Default is 200 (OK) or 204 (No Content). SuccessStatus int // SuccessContentType is a Content-Type of successful response, default application/json. SuccessContentType string // MakeErrResp overrides error response builder instead of default Err, // returned values are HTTP status code and error structure to be marshaled. MakeErrResp func(ctx context.Context, err error) (int, interface{}) // ReqMapping controls request decoding into use case input. // Optional, if not set field tags are used as mapping. ReqMapping RequestMapping RespHeaderMapping map[string]string RespCookieMapping map[string]http.Cookie // ReqValidator validates decoded request data. ReqValidator Validator // RespValidator validates decoded response data. RespValidator Validator // OperationAnnotations are called after operation setup and before adding operation to documentation. // // Deprecated: use OpenAPIAnnotations. OperationAnnotations []func(op *openapi3.Operation) error // OpenAPIAnnotations are called after operation setup and before adding operation to documentation. OpenAPIAnnotations []func(oc openapi.OperationContext) error }
HandlerTrait controls basic behavior of rest handler.
func (*HandlerTrait) RequestMapping ¶
func (h *HandlerTrait) RequestMapping() RequestMapping
RequestMapping returns custom mapping for request decoder.
func (*HandlerTrait) RestHandler ¶
func (h *HandlerTrait) RestHandler() *HandlerTrait
RestHandler is an accessor.
type HandlerWithRoute ¶
type HandlerWithRoute interface { // RouteMethod returns http method of action. RouteMethod() string // RoutePattern returns http path pattern of action. RoutePattern() string }
HandlerWithRoute is a http.Handler with routing information.
type HandlerWithUseCase ¶
type HandlerWithUseCase interface {
UseCase() usecase.Interactor
}
HandlerWithUseCase exposes usecase.
type JSONSchemaValidator ¶
type JSONSchemaValidator interface { Validator // AddSchema accepts JSON schema for a request parameter or response value. AddSchema(in ParamIn, name string, schemaData []byte, required bool) error }
JSONSchemaValidator defines JSON schema validator.
type JSONWriterTo ¶
JSONWriterTo writes JSON payload.
type OutputWithHTTPStatus ¶
OutputWithHTTPStatus exposes HTTP status code(s) for output.
type RequestErrors ¶
RequestErrors is a list of validation or decoding errors.
Key is field position (e.g. "path:id" or "body"), value is a list of issues with the field.
func (RequestErrors) Fields ¶
func (re RequestErrors) Fields() map[string]interface{}
Fields returns request errors by field location and name.
type RequestJSONSchemaProvider ¶
type RequestJSONSchemaProvider interface { ProvideRequestJSONSchemas( method string, input interface{}, mapping RequestMapping, validator JSONSchemaValidator, ) error }
RequestJSONSchemaProvider provides request JSON Schemas.
type RequestMapping ¶
RequestMapping describes how decoded request should be applied to container struct.
It is defined as a map by parameter location. Each item is a map with struct field name as key and decoded field name as value.
Example:
map[rest.ParamIn]map[string]string{rest.ParamInQuery:map[string]string{"ID": "id", "FirstName": "first-name"}}
type RequestValidatorFactory ¶
type RequestValidatorFactory interface {
MakeRequestValidator(method string, input interface{}, mapping RequestMapping) Validator
}
RequestValidatorFactory creates request validator for particular structured Go input value.
type ResponseJSONSchemaProvider ¶
type ResponseJSONSchemaProvider interface { ProvideResponseJSONSchemas( statusCode int, contentType string, output interface{}, headerMapping map[string]string, validator JSONSchemaValidator, ) error }
ResponseJSONSchemaProvider provides response JSON Schemas.
type ResponseValidatorFactory ¶
type ResponseValidatorFactory interface { MakeResponseValidator( statusCode int, contentType string, output interface{}, headerMapping map[string]string, ) Validator }
ResponseValidatorFactory creates response validator for particular structured Go output value.
type ValidationErrors ¶
ValidationErrors is a list of validation errors.
Key is field position (e.g. "path:id" or "body"), value is a list of issues with the field.
func (ValidationErrors) Error ¶
func (re ValidationErrors) Error() string
Error returns error message.
func (ValidationErrors) Fields ¶
func (re ValidationErrors) Fields() map[string]interface{}
Fields returns request errors by field location and name.
type Validator ¶
type Validator interface { // ValidateData validates decoded request/response data and returns error in case of invalid data. ValidateData(in ParamIn, namedData map[string]interface{}) error // ValidateJSONBody validates JSON encoded body and returns error in case of invalid data. ValidateJSONBody(jsonBody []byte) error // HasConstraints indicates if there are validation rules for parameter location. HasConstraints(in ParamIn) bool }
Validator validates a map of decoded data.
type ValidatorFunc ¶
ValidatorFunc implements Validator with a func.
func (ValidatorFunc) HasConstraints ¶
func (v ValidatorFunc) HasConstraints(_ ParamIn) bool
HasConstraints indicates if there are validation rules for parameter location.
func (ValidatorFunc) ValidateData ¶
func (v ValidatorFunc) ValidateData(in ParamIn, namedData map[string]interface{}) error
ValidateData implements Validator.
func (ValidatorFunc) ValidateJSONBody ¶
func (v ValidatorFunc) ValidateJSONBody(body []byte) error
ValidateJSONBody implements Validator.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package chirouter provides instrumentation for chi.Router.
|
Package chirouter provides instrumentation for chi.Router. |
Package gorillamux provides OpenAPI docs collector for gorilla/mux web services.
|
Package gorillamux provides OpenAPI docs collector for gorilla/mux web services. |
Package gzip provides pre-compressed data container.
|
Package gzip provides pre-compressed data container. |
Package jsonschema implements request validator with github.com/santhosh-tekuri/jsonschema/v2.
|
Package jsonschema implements request validator with github.com/santhosh-tekuri/jsonschema/v2. |
Package nethttp provides instrumentation for net/http.
|
Package nethttp provides instrumentation for net/http. |
Package openapi provides documentation collector.
|
Package openapi provides documentation collector. |
Package request implements reflection-based net/http request decoder.
|
Package request implements reflection-based net/http request decoder. |
Package response implements reflection-based net/http response encoder.
|
Package response implements reflection-based net/http response encoder. |
gzip
Package gzip provides http compression support.
|
Package gzip provides http compression support. |
Package resttest provides utilities to test REST API.
|
Package resttest provides utilities to test REST API. |
Package web provides default facades for web service bootstrap.
|
Package web provides default facades for web service bootstrap. |