api

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2020 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// StatusSuccess indicate result Status is success, the data of result is available
	StatusSuccess Status = "success"
	// StatusError indicate result is failed, the data may be empty
	StatusError Status = "error"
	// ErrorBadData indicate that result is failed because the wrong request data
	ErrorBadData ErrorType = "bad_data"
	// ErrorInternal indicate that result is failed because the request data may be right but the server is something wrong
	ErrorInternal ErrorType = "internal"
)

Variables

This section is empty.

Functions

func Get

func Get(url string, ret interface{}) error

Get do get request to target url and save data to ret

func Post

func Post(url string, req interface{}, ret interface{}) (err error)

Post do http post with standard response format

func TestCall

func TestCall(t *testing.T, engine *gin.Engine, uri, method, data string, ret interface{}) *require.Assertions

TestCall create a httptest server and do http request to it the data in params will be write to server and the ret in params is deemed to the Data of common Result

func Wrap

func Wrap(log logrus.FieldLogger, f func(ctx *gin.Context) *Result) func(ctx *gin.Context)

Wrap return a gin handler function with common result processed

Types

type ErrorType

type ErrorType string

ErrorType is not empty if result status is not success

type Result

type Result struct {
	// ErrorType is the type of result if Status is not success
	ErrorType ErrorType `json:"errorType,omitempty"`
	// Err indicate the error detail
	Err string `json:"error,omitempty"`
	// Data is the real data of result, data may be nil even if Status is success
	Data interface{} `json:"data,omitempty"`
	// Status indicate whether the result is success
	Status Status `json:"status"`
}

Result is the common format of all response

func BadDataErr

func BadDataErr(err error, format string, args ...interface{}) *Result

BadDataErr make a result with ErrorType ErrorBadData

func Data

func Data(data interface{}) *Result

Data make a result with data or nil, the Status will be set to StatusSuccess

func InternalErr

func InternalErr(err error, format string, args ...interface{}) *Result

InternalErr make a result with ErrorType ErrorInternal

type Status

type Status string

Status indicate the result status of request, success or error

Jump to

Keyboard shortcuts

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