response

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterCustomCode

func RegisterCustomCode(code int, status, message string) error

RegisterCustomCode was used to register the custom code description, it would overwrite the previous one when conflicted.

func ResponseError

func ResponseError(ctx *gin.Context, statusCode int, err error)

ResponseError would write the error data output with status error

func ResponseWithAccepted

func ResponseWithAccepted(ctx *gin.Context, data any)

ResponseWithAccepted would write the success data output with status accepted

func ResponseWithCreated

func ResponseWithCreated(ctx *gin.Context, data any)

ResponseWithCreated would write the success data output with status created

func ResponseWithErrors

func ResponseWithErrors(ctx *gin.Context, statusCode, subCode int, errors []any)

ResponseWithErrors would write meta with error and empty data

func ResponseWithOK

func ResponseWithOK(ctx *gin.Context, data any)

ResponseWithOK would write the success data output with status ok

func ResponseWithSuccess

func ResponseWithSuccess(ctx *gin.Context, statusCode int, data any)

ResponseWithSuccess would write the success data output and status into http writer

Types

type Description

type Description struct {
	Status  string
	Message string
}

func HttpCodeDescription

func HttpCodeDescription(code int) Description

HttpCodeDescription returns the description of the http code.

type Meta

type Meta struct {
	Code    int    `json:"code"`
	Type    string `json:"type,omitempty"`
	Message string `json:"message,omitempty"`
	Errors  []any  `json:"errors,omitempty"`
}

func (Meta) IsError

func (m Meta) IsError() bool

IsError return whether the response meta was error or not(http code >= 400)

func (Meta) StatusCode

func (m Meta) StatusCode() int

StatusCode return http status code in meta

func (Meta) SubCode

func (m Meta) SubCode() int

SubCode return sub code in meta

type Response

type Response struct {
	Meta Meta `json:"meta"`
	Data any  `json:"data"`
	// contains filtered or unexported fields
}

func UnmarshalResponse

func UnmarshalResponse(bytes []byte) (*Response, error)

UnmarshalResponse create response

func (*Response) Code

func (r *Response) Code() int

Code return code in response's meta

func (*Response) Errors

func (r *Response) Errors() []any

Errors return errors in response's meta

func (*Response) GetData

func (r *Response) GetData(v any) error

GetData was used to unmarshal the data field into obj

func (*Response) IsError

func (r *Response) IsError() bool

IsError return whether the response was error or not(http code >= 400)

func (*Response) Message

func (r *Response) Message() string

Message return message in response's meta

func (*Response) StatusCode

func (r *Response) StatusCode() int

StatusCode return http status code in response's meta

func (*Response) SubCode

func (r *Response) SubCode() int

SubCode return sub code in response's meta

func (*Response) Type

func (r *Response) Type() string

Type return type in response's meta

Jump to

Keyboard shortcuts

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