rest

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2019 License: BSD-2-Clause Imports: 3 Imported by: 0

README

rest

Go Rest Helper

The lib provided easy methods for generate JSON response from custom forms

Use OkResponse for send 200 OK client data

Use ErrorResponse for providing error to client

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadGatewayResponse

func BadGatewayResponse(writer http.ResponseWriter, message string)

Bad Gateway 502

func BadRequestResponse

func BadRequestResponse(writer http.ResponseWriter, message string)

Bad Request 400

func ConflictResponse

func ConflictResponse(writer http.ResponseWriter, message string)

Conflict 409

func ErrorResponse

func ErrorResponse(w http.ResponseWriter, e IError)

Response with error

func ExpectationFailedResponse

func ExpectationFailedResponse(writer http.ResponseWriter, message string)

Expectation Failed 417

func FailedDependencyResponse

func FailedDependencyResponse(writer http.ResponseWriter, message string)

Failed Dependency 424

func ForbiddenResponse

func ForbiddenResponse(writer http.ResponseWriter, message string)

Forbidden 403

func GatewayTimeoutResponse

func GatewayTimeoutResponse(writer http.ResponseWriter, message string)

Gateway Timeout 504

func GoneResponse

func GoneResponse(writer http.ResponseWriter, message string)

Gone 410

func HTTPVersionNotSupportedResponse

func HTTPVersionNotSupportedResponse(writer http.ResponseWriter, message string)

HTTP Version Not Supported 505

func InsufficientStorageResponse

func InsufficientStorageResponse(writer http.ResponseWriter, message string)

Insufficient Storage 507

func InternalServerErrorResponse

func InternalServerErrorResponse(writer http.ResponseWriter, message string)

Internal Server Error 500

func LengthRequiredResponse

func LengthRequiredResponse(writer http.ResponseWriter, message string)

Length Required 411

func LockedResponse

func LockedResponse(writer http.ResponseWriter, message string)

Locked 423

func LoopDetectedResponse

func LoopDetectedResponse(writer http.ResponseWriter, message string)

Loop Detected 508

func MethodNotAllowedResponse

func MethodNotAllowedResponse(writer http.ResponseWriter, message string)

Method Not Allowed 405

func MisdirectedRequestResponse

func MisdirectedRequestResponse(writer http.ResponseWriter, message string)

Misdirected Request 421

func NetworkAuthenticationRequiredResponse

func NetworkAuthenticationRequiredResponse(writer http.ResponseWriter, message string)

Network Authentication Required 511

func NotAcceptableResponse

func NotAcceptableResponse(writer http.ResponseWriter, message string)

Not Acceptable 406

func NotExtendedResponse

func NotExtendedResponse(writer http.ResponseWriter, message string)

Not Extended 510

func NotFoundResponse

func NotFoundResponse(writer http.ResponseWriter, message string)

Not Found 404

func NotImplementedResponse

func NotImplementedResponse(writer http.ResponseWriter, message string)

Not Implemented 501

func OkResponse

func OkResponse(writer http.ResponseWriter, message string, data interface{}, meta interface{})

Ok 200

func PaymentRequiredResponse

func PaymentRequiredResponse(writer http.ResponseWriter, message string)

Payment Required 402

func PreconditionFailedResponse

func PreconditionFailedResponse(writer http.ResponseWriter, message string)

Precondition Failed 412

func PreconditionRequiredResponse

func PreconditionRequiredResponse(writer http.ResponseWriter, message string)

Precondition Required 428

func ProxyAuthRequiredResponse

func ProxyAuthRequiredResponse(writer http.ResponseWriter, message string)

Proxy Auth Required 407

func RequestEntityTooLargeResponse

func RequestEntityTooLargeResponse(writer http.ResponseWriter, message string)

Request Entity Too Large 413

func RequestHeaderFieldsTooLargeResponse

func RequestHeaderFieldsTooLargeResponse(writer http.ResponseWriter, message string)

Request Header Fields Too Large 431

func RequestTimeoutResponse

func RequestTimeoutResponse(writer http.ResponseWriter, message string)

Request Timeout 408

func RequestURITooLongResponse

func RequestURITooLongResponse(writer http.ResponseWriter, message string)

Request URI Too Long 414

func RequestedRangeNotSatisfiableResponse

func RequestedRangeNotSatisfiableResponse(writer http.ResponseWriter, message string)

Requested Range Not Satisfiable 416

func SendJson

func SendJson(writer http.ResponseWriter, httpCode int, data interface{})

Sent json into http writer

func ServiceUnavailableResponse

func ServiceUnavailableResponse(writer http.ResponseWriter, message string)

Service Unavailable 503

func TeapotResponse

func TeapotResponse(writer http.ResponseWriter, message string)

Teapot 418

func TooEarlyResponse

func TooEarlyResponse(writer http.ResponseWriter, message string)

Too Early 425

func TooManyRequestsResponse

func TooManyRequestsResponse(writer http.ResponseWriter, message string)

Too Many Requests 429

func UnauthorizedResponse

func UnauthorizedResponse(writer http.ResponseWriter, message string)

Unauthorized 401

func UnavailableForLegalReasonsResponse

func UnavailableForLegalReasonsResponse(writer http.ResponseWriter, message string)

Unavailable For Legal Reasons 451

func UnprocessableEntityResponse

func UnprocessableEntityResponse(writer http.ResponseWriter, message string)

Unprocessable Entity 421

func UnsupportedMediaTypeResponse

func UnsupportedMediaTypeResponse(writer http.ResponseWriter, message string)

Unsupported Media Type 415

func UpgradeRequiredResponse

func UpgradeRequiredResponse(writer http.ResponseWriter, message string)

Upgrade Required 426

func VariantAlsoNegotiatesResponse

func VariantAlsoNegotiatesResponse(writer http.ResponseWriter, message string)

Variant Also Negotiates 506

Types

type Error

type Error struct {
	// Error code
	Code *int `json:"code,omitempty"`
	// Error details
	Errors *ErrorDetails `json:"errors,omitempty"`
	// Message
	Message string `json:"message"`
	// contains filtered or unexported fields
}

Rest error struct

func (*Error) AppendDetail

func (e *Error) AppendDetail(message string, key *string, code *int) IError

Append detail

func (*Error) Error

func (e *Error) Error() string

Default error interface method

func (*Error) GetCode

func (e *Error) GetCode() *int

Get code

func (*Error) GetDetails

func (e *Error) GetDetails() *ErrorDetails

Get error details

func (*Error) GetHttpCode

func (e *Error) GetHttpCode() int

Get Http status code

func (*Error) SetCode

func (e *Error) SetCode(code *int) IError

Set code

func (*Error) SetDetails

func (e *Error) SetDetails(details *ErrorDetails) IError

Set details

type ErrorDetails

type ErrorDetails []ErrorField

Rest error details

type ErrorField

type ErrorField struct {
	// Field error code
	Code *int `json:"code,omitempty"`
	// Field name
	Key *string `json:"key,omitempty"`
	// Message
	Message string `json:"message"`
}

Rest error detail

type IError

type IError interface {
	// Append error detail
	AppendDetail(message string, key *string, code *int) IError
	// Get rest error details
	GetDetails() *ErrorDetails
	// Get message
	Error() string
	// Get http code
	GetHttpCode() int
	// Set detail
	SetDetails(details *ErrorDetails) IError
	// Set error code
	SetCode(code *int) IError
	// Get code
	GetCode() *int
}

Rest error interface

func NewRestError

func NewRestError(message string, httpCode int) IError

New rest Error

func ReadJsonBody

func ReadJsonBody(r *http.Request, data interface{}) (interface{}, IError)

Прочитать тело дескриптор запроса

type JsonDetailError

type JsonDetailError struct {
	// Name of filed
	Name string `json:"name"`
	// Code of error
	Code string `json:"code"`
	// Message of error
	Message string `json:"message,omitempty"`
}

type JsonError

type JsonError struct {
	// Message of error
	Message string `json:"message"`
	// Code of error
	Code string `json:"code"`
	// Errors detail
	Data []JsonDetailError `json:"data,omitempty"`
}

func (*JsonError) FlushDetail

func (e *JsonError) FlushDetail() *JsonError

Flush last detail

func (*JsonError) PopDetail

func (e *JsonError) PopDetail() *JsonDetailError

Pop last detail

func (*JsonError) PushDetail

func (e *JsonError) PushDetail(name string, code string, message string) *JsonError

Append detail

type JsonResponse

type JsonResponse struct {
	// Debug information
	Debug interface{} `json:"debug,omitempty"`
	// Body
	Data interface{} `json:"data,omitempty"`
	// Meta information
	Meta interface{} `json:"meta,omitempty"`
	// Error information
	Error *JsonError `json:"error,omitempty"`
}

func NewErrorJsonResponse

func NewErrorJsonResponse(debug interface{}, message, code string) *JsonResponse

New Json Response with error

func NewOkJsonResponse

func NewOkJsonResponse(debug interface{}, data interface{}, meta interface{}) *JsonResponse

New Json Response without error

type Meta

type Meta struct {
	// Current page
	Page int `json:"page,omitempty"`
	// Limit
	Limit int `json:"limit,omitempty"`
	// Total count
	Total int `json:"total,omitempty"`
}

type Response

type Response struct {

	// Client data
	Data interface{} `json:"data,omitempty"`
	// Code
	Code *int `json:"code,omitempty"`
	// Error details
	Errors interface{} `json:"errors,omitempty"`
	// Client message for end user
	Message string `json:"message,omitempty"`
	// Meta data
	Meta interface{} `json:"meta,omitempty"`
	// contains filtered or unexported fields
}

Rest response object

func NewResponse

func NewResponse(writer http.ResponseWriter, code int) *Response

Create new rest response

func (*Response) Send

func (response *Response) Send()

Send rest response

Jump to

Keyboard shortcuts

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