api

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2022 License: Apache-2.0 Imports: 15 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

Types

type ErrorType

type ErrorType string

ErrorType is not empty if result status is not success

type Helper added in v0.2.0

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

Helper provider some function to build a service

func NewHelper added in v0.2.0

func NewHelper(lg logrus.FieldLogger, register *prometheus.Registry, metricsPrefix string) *Helper

NewHelper create a new APIWrapper

func (*Helper) MetricsHandler added in v0.2.0

func (h *Helper) MetricsHandler(c *gin.Context)

MetricsHandler process metrics request

func (*Helper) Wrap added in v0.2.0

func (h *Helper) Wrap(f func(ctx *gin.Context) *Result) func(ctx *gin.Context)

Wrap return a gin handler function with common result processed

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

func TestCall

func TestCall(t *testing.T, serveHTTP func(w http.ResponseWriter, req *http.Request), uri, method, data string, ret interface{}) (*require.Assertions, *Result)

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

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