api

package
v0.0.0-...-c954afd Latest Latest
Warning

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

Go to latest
Published: May 11, 2020 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// StatusCodeOK is custome status code for ok
	StatusCodeOK string = "REBEL-200"

	// StatusCodeBadRequest is custome status code for bad request
	StatusCodeBadRequest string = "REBEL-400"

	// StatusCodeForbidden is custome status code for forbidden
	StatusCodeForbidden string = "REBEL-401"

	// StatusCodeInternalServerError is custome status for unkown error / internal server error
	StatusCodeInternalServerError string = "REBEL-500"

	// StatusCodeNotFound is custome status code for not found
	StatusCodeNotFound string = "REBEL-404"
)
View Source
const (
	// StatusMessageOK is custome status message for ok
	StatusMessageOK string = "OK"

	// StatusMessageBadRequest is custome status message for bad request
	StatusMessageBadRequest string = "Bad Request"

	// StatusMessageInternalError is custome status message for unknown error / internal server error
	StatusMessageInternalServerError string = "Internal Error"

	// StatusMessageNotFound is custome status message for data not found
	StatusMessageNotFound string = "Not Found"

	// StatusMessageForbidden is custome status message for forbidden
	StatusMessageForbidden string = "Forbidden"
)

Variables

This section is empty.

Functions

func Decode

func Decode(r *http.Request, val interface{}) error

Decode reads the body of an HTTP request looking for a JSON document. The body is decoded into the provided value.

func ErrBadRequest

func ErrBadRequest(err error, message string) error

ErrBadRequest wraps a provided error with an HTTP status code and custome status code for bad request. This function should be used when handlers encounter expected errors.

func ErrForbidden

func ErrForbidden(err error, message string) error

ErrForbidden wraps a provided error with an HTTP status code and custome status code for forbidden. This function should be used when handlers encounter expected errors.

func ErrInternal

func ErrInternal(err error, message string) error

ErrInternal wraps a provided error with an HTTP status code and custome status code for internal server error. This function should be used when handlers encounter expected errors.

func ErrNewRequest

func ErrNewRequest(err error, status string, messageStatus string, httpStatus int) error

ErrNewRequest wraps a provided error with an HTTP status code and custome status code. This function should be used when handlers encounter expected errors.

func ErrNotFound

func ErrNotFound(err error, message string) error

ErrNotFound wraps a provided error with an HTTP status code and custome status code for not found. This function should be used when handlers encounter expected errors.

func GetCode

func GetCode(ctx context.Context, tx *sql.Tx, prefix string, tableName string, companyID uint32) (string, error)

GetCode is function to generate code of transaction

func Response

func Response(w http.ResponseWriter, data interface{}, statusCode string, message string, httpCode int) error

Response converts a Go value to JSON and sends it to the client.

func ResponseError

func ResponseError(w http.ResponseWriter, err error) error

ResponseError sends an error reponse back to the client.

func ResponseOK

func ResponseOK(w http.ResponseWriter, data interface{}, HTTPStatus int) error

ResponseOK converts a Go value to JSON and sends it to the client.

Types

type App

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

App is the entrypoint into our application and what controls the context of each request. Feel free to add any configuration data/logic on this type.

func NewApp

func NewApp(log *log.Logger, mw ...Middleware) *App

NewApp is function to create new App

func (*App) Handle

func (a *App) Handle(method, url string, h Handler)

Handle associates a httprouter Handle function with an HTTP Method and URL pattern.

func (*App) ServeHTTP

func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface.

type Ctx

type Ctx string

Ctx type for encapsulated context key

type Error

type Error struct {
	Err           error
	Status        string
	MessageStatus string
	HTTPStatus    int
}

Error is used to pass an error during the request through the application with web specific context.

func (*Error) Error

func (err *Error) Error() string

Error implements the error interface. It uses the default message of the wrapped error. This is what will be shown in the services' logs.

type ErrorResponse

type ErrorResponse struct {
	Error string `json:"error"`
}

ErrorResponse is the form used for API responses from failures in the API.

type Handler

type Handler func(http.ResponseWriter, *http.Request)

Handler type for force httprouter into standard http handler

type Middleware

type Middleware func(Handler) Handler

Middleware is a function designed to run some code before and/or after another Handler. It is designed to remove boilerplate or other concerns not direct to any given Handler.

type ResponseFormat

type ResponseFormat struct {
	StatusCode string      `json:"status_code"`
	Message    string      `json:"status_message"`
	Data       interface{} `json:"data"`
}

ResponseFormat is used to pass an response in standard format

Jump to

Keyboard shortcuts

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