request

package
v1.8.94 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const AuthHeaderKey = "Authorization"

AuthHeaderKey is the name of the Authorization header

View Source
const AuthHeaderValuePrefix = "Bearer "

AuthHeaderValuePrefix is the start of the Authorization string (in the authorization header) used to authorize the request

View Source
const DeviceHeader = "X-Device"

Variables

This section is empty.

Functions

func BadRequest

func BadRequest(r *Request, w http.ResponseWriter, e error)

BadRequest returns a bad request status (400)

func Created

func Created(r *Request, w http.ResponseWriter)

Created returns a created status (201)

func Forbidden

func Forbidden(r *Request, w http.ResponseWriter)

Forbidden returns a forbidden status (403)

func HTML added in v1.8.50

func HTML(r *Request, w http.ResponseWriter, content string)

func HTTPMethodTypeToString added in v1.8.11

func HTTPMethodTypeToString(t HTTPMethodType) string

func HandleError

func HandleError(r *Request, w http.ResponseWriter, e error)

HandleError handles errors returned from the service layer and calls a api error handler to return the corresponding HTTP response

func InternalServerError

func InternalServerError(r *Request, w http.ResponseWriter, e error)

InternalServerError returns a 500 server error response

func JSON

func JSON(r *Request, w http.ResponseWriter, body interface{})

JSON Returns an ok status with json-encoded body

func NoContent

func NoContent(r *Request, w http.ResponseWriter)

NoContent returns a noContent status (204)

func NotFound

func NotFound(r *Request, w http.ResponseWriter)

NotFound returns a not-found status

func NotImplemented

func NotImplemented(r *Request, w http.ResponseWriter)

NotImplemented shows a text response for not implemented method (501)

func OK

func OK(r *Request, w http.ResponseWriter)

OK Returns an ok status

func PrintArgs

func PrintArgs(r *http.Request)

PrintArgs prints args

func QueryArgInt

func QueryArgInt(r *http.Request, name string, defaultVal int) (val int)

QueryArgInt checks the incoming request `r` for a query argument named `name` and if it exists, attempts to parse it to an integer If the argument does not exist, the value `defaultVal` is returned

func QueryArgInt64

func QueryArgInt64(r *http.Request, name string, defaultVal int64) (val int64)

QueryArgInt64 checks the incoming request `r` for a query argument named `name` and if it exists, attempts to parse it to an 64-bit integer If the argument does not exist, the value `defaultVal` is returned

func QueryArgString

func QueryArgString(r *http.Request, name string, defaultVal string) (val string)

QueryArgString checks the incoming request `r` for a query argument named `name` and if it exists, returns it Else, it returns `defaultVal`

func URLParamInt64

func URLParamInt64(r *http.Request, name string, defaultVal int64) (val int64)

URLParamInt64 returns an int64 value from a url parameter

func URLParamString

func URLParamString(r *http.Request, name string, defaultVal string) (val string)

URLParamString returns a string url parameter or the default if not found

func Unauthorized

func Unauthorized(r *Request, w http.ResponseWriter)

Unauthorized returns an unauthorized status (401)

Types

type ErrorResponse

type ErrorResponse struct {
	Status string `json:"status"`
	Detail string `json:"detail"`
}

ErrorResponse is the structure of a response that is an error @model ErrorResponse

type HTTPMethodType

type HTTPMethodType int

HTTPMethodType is the type of a method request

const (
	// HTTPMethodUnknown is an unknown method type
	HTTPMethodUnknown HTTPMethodType = iota
	// HTTPMethodGet is the http method 'GET'
	HTTPMethodGet
	// HTTPMethodPost is the http method POST
	HTTPMethodPost
	// HTTPMethodPut is the http method PUT
	HTTPMethodPut
	// HTTPMethodDelete is the http method DELETE
	HTTPMethodDelete
	// HTTPMethodOptions is the http method OPTIONS
	HTTPMethodOptions
	// HTTPMethodHead is the http method HEAD
	HTTPMethodHead
	// HTTPMethodTrace is the http method TRACE
	HTTPMethodTrace
	// HTTPMethodPatch is the http method PATCH
	HTTPMethodPatch
)

func HTTPMethodTypeFromString

func HTTPMethodTypeFromString(method string) HTTPMethodType

HTTPMethodTypeFromString converts a method from a string to its method type

func (HTTPMethodType) Int64

func (h HTTPMethodType) Int64() int64

Int64 returns the int64 representation of the HTTPMethodType value

type IResponseLogger

type IResponseLogger interface {

	// HandleError handles errors returned from the service layer and
	// calls a api error handler to return the corresponding HTTP response
	HandleError(r *Request, w http.ResponseWriter, e error)

	// NotImplemented shows a text response for not implemented method (501)
	NotImplemented(r *Request, w http.ResponseWriter)

	// InternalServerError returns a 500 server error response
	InternalServerError(r *Request, w http.ResponseWriter, e error)

	// NotFound returns a not-found status
	NotFound(r *Request, w http.ResponseWriter)

	// BadRequest returns a bad request status (400)
	BadRequest(r *Request, w http.ResponseWriter, e error)

	// Unauthorized returns an unauthorized status (401)
	Unauthorized(r *Request, w http.ResponseWriter)

	// Forbidden returns a forbidden status (403)
	Forbidden(r *Request, w http.ResponseWriter)

	// NoContent returns a noContent status (204)
	NoContent(r *Request, w http.ResponseWriter)

	// Created returns a created status (201)
	Created(r *Request, w http.ResponseWriter)

	// JSON Returns an ok status with json-encoded body
	JSON(r *Request, w http.ResponseWriter, body interface{})

	// HTML Returns an ok status with html body
	HTML(r *Request, w http.ResponseWriter, body string)

	// OK Returns an ok status
	OK(r *Request, w http.ResponseWriter)
}

IResponseLogger outlines methods for handling logging for API Responses

type Request

type Request struct {
	Method           string
	Path             string
	Headers          map[string]string
	Params           map[string]string
	Body             string
	ControllerMethod string
	ResponseCode     int64
	Error            string
	UserID           int64
	User             types.IUserContainer
	RootRequest      *http.Request `json:"-"`
	ActionType       int64
	BodyReadCloser   io.ReadCloser
}

Request is a request object

func NewRequest

func NewRequest(r *http.Request) *Request

NewRequest is a factory method for a request

func (*Request) Arg

func (r *Request) Arg(name string, defaultVal string) string

Arg returns a string value for an argument in the request named `name` If it exists, returns it as a string Else, returns the `defaultVal`

func (*Request) ArgInt

func (r *Request) ArgInt(name string, defaultVal int) int

ArgInt returns an int value for an argument in the request named `name` If it exists, attempts to part it to a 64-bit integer Else, `defaultVal` is returned

func (*Request) ArgInt64

func (r *Request) ArgInt64(name string, defaultVal int64) int64

ArgInt64 returns an int64 value for an argument in the request named `name` If it exists, attempts to part it to a 64-bit integer Else, `defaultVal` is returned

func (*Request) AuthKey

func (r *Request) AuthKey() string

AuthKey returns the authorization key from the request header

func (*Request) BodyJSON

func (r *Request) BodyJSON(obj interface{})

BodyJSON extracts the json from the body of a post or put request

func (*Request) Device added in v1.8.94

func (r *Request) Device() string

Device returns the device key from the request header

func (*Request) Header

func (r *Request) Header(name string) string

Header gets a header by its name

func (*Request) IP added in v1.8.45

func (r *Request) IP() string

IP returns the IP from which the request originated

func (*Request) ToJSONString

func (r *Request) ToJSONString() string

ToJSONString returns a json string representation of the request

type Response

type Response struct {
}

Response is the API response handler

func NewResponse

func NewResponse() *Response

NewResponse returns a new response object

func (*Response) BadRequest

func (s *Response) BadRequest(r *Request, w http.ResponseWriter, e error)

BadRequest returns a bad request status (400)

func (*Response) Created

func (s *Response) Created(r *Request, w http.ResponseWriter)

Created returns a created status (201)

func (*Response) Forbidden

func (s *Response) Forbidden(r *Request, w http.ResponseWriter)

Forbidden returns a forbidden status (403)

func (*Response) HTML added in v1.8.50

func (s *Response) HTML(r *Request, w http.ResponseWriter, body string)

HTML Returns an ok status with html body

func (*Response) HandleError

func (s *Response) HandleError(r *Request, w http.ResponseWriter, e error)

HandleError handles errors returned from the service layer and calls a api error handler to return the corresponding HTTP response

func (*Response) InternalServerError

func (s *Response) InternalServerError(r *Request, w http.ResponseWriter, e error)

InternalServerError returns a 500 server error response

func (*Response) JSON

func (s *Response) JSON(r *Request, w http.ResponseWriter, body interface{})

JSON Returns an ok status with json-encoded body

func (*Response) NoContent

func (s *Response) NoContent(r *Request, w http.ResponseWriter)

NoContent returns a noContent status (204)

func (*Response) NotFound

func (s *Response) NotFound(r *Request, w http.ResponseWriter)

NotFound returns a not-found status

func (*Response) NotImplemented

func (s *Response) NotImplemented(r *Request, w http.ResponseWriter)

NotImplemented shows a text response for not implemented method (501)

func (*Response) OK

func (s *Response) OK(r *Request, w http.ResponseWriter)

OK Returns an ok status

func (*Response) Unauthorized

func (s *Response) Unauthorized(r *Request, w http.ResponseWriter)

Unauthorized returns an unauthorized status (401)

type UserLogActionType

type UserLogActionType int64

UserLogActionType is the type of action in the user log

const (
	// UserLogActionTypeAPI is an API Action Type
	UserLogActionTypeAPI UserLogActionType = iota + 1
	// UserLogActionTypeClient is an API Action Type
	UserLogActionTypeClient
)

func (UserLogActionType) Int64

func (lt UserLogActionType) Int64() int64

Int64 returns an int64 of the constant

Jump to

Keyboard shortcuts

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