api

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultPage is the default page number when none is
	// passed.
	DefaultPage = 1
	// DefaultLimit is the default limit when none is
	// passed.
	DefaultLimit = 15
)

Variables

View Source
var (
	// DefaultParams represents the default params if
	// none were passed for the API.
	DefaultParams = params.Defaults{
		Page:           DefaultPage,
		Limit:          DefaultLimit,
		OrderBy:        "created_at",
		OrderDirection: "desc",
	}
)

Functions

func AbortJSON

func AbortJSON(ctx *gin.Context, status int, message string, data interface{})

AbortJSON

Returns RespondJSON and aborts the request with given status, message and data.

func Params

func Params(g *gin.Context) *params.Params

Params

Sets up a new Params struct with context.

func Respond

func Respond(ctx *gin.Context, status int, message string, data interface{}, p ...*pagination.Pagination)

Respond

Returns RespondJSON and sends back the main data for use with the API. Returns status, message and data.

Types

type ErrorJSON

type ErrorJSON struct {
	Errors validation.Errors `json:"errors"`
}

ErrorJSON defines the validation errors if there any when processing data via handlers.

type Meta

type Meta struct {
	RequestTime  string      `json:"request_time"`
	ResponseTime string      `json:"response_time"`
	LatencyTime  string      `json:"latency_time"`
	Pagination   interface{} `json:"pagination,omitempty"`
}

Meta defines any additional information for API request, including pagination for list routes.

func GetMeta

func GetMeta(ctx *gin.Context, p []*pagination.Pagination) Meta

GetMeta

Processes the request and response time and calculates latency time. Sets pagination if the length is greater than one.

type RespondJSON

type RespondJSON struct {
	Status  int         `json:"status"`
	Error   bool        `json:"error"`
	Message string      `json:"message"`
	Meta    Meta        `json:"meta"`
	Data    interface{} `json:"data"`
}

RespondJSON defines the main struct for sending back data for API requests, it includes a status code, if the handler produced an error, a message, meta information and the main data.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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