vhttp

package
v0.0.0-...-f4de95a Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentTypeHeader   = "Content-Type"
	AuthorizationHeader = "Authorization"

	MetadataKey = "metadata"

	NotApplicable = "N/A"
)
View Source
const (
	SubjectContextKey  = "subject"
	NewTokenContextKey = "newToken"
)

Context Key

View Source
const (
	SuccessCode     = "OK"
	SuccessMessage  = "Success"
	SuccessCodeHTTP = 200
)
View Source
const (
	BadRequestCode     = "400"
	BadRequestCodeHTTP = 400
	BadRequestMessage  = "Bad Request"
)
View Source
const (
	UnauthorizedCode     = "401"
	UnauthorizedCodeHTTP = 401
	UnauthorizedMessage  = "Unauthorized"
)
View Source
const (
	ForbiddenCode     = "403"
	ForbiddenCodeHTTP = 403
	ForbiddenMessage  = "Forbidden"
)
View Source
const (
	InternalErrorCode     = "500"
	InternalErrorCodeHTTP = 500
	InternalErrorMessage  = "Internal Error"
)

Variables

View Source
var BadRequestError = NewError("400", "Bad Request", http.StatusBadRequest)

Base Errors

View Source
var EmptyAuthorizationError = NewError("E_AUTH_1", "Authorization value is empty",
	http.StatusBadRequest)

Authorization Errors

View Source
var ExpiredJWTError = NewError("E_AUTH_5", "Expired JWT",
	http.StatusUnauthorized)
View Source
var ForbiddenError = NewError("403", "Forbidden", http.StatusForbidden)
View Source
var InternalError = NewError("500", "Internal Error", http.StatusInternalServerError)
View Source
var InvalidJWTFormatError = NewError("E_AUTH_3", "Invalid JWT Format",
	http.StatusBadRequest)
View Source
var InvalidJWTIssuerError = NewError("E_AUTH_4", "Invalid JWT Issuer",
	http.StatusUnauthorized)
View Source
var InvalidResourceVersionError = NewError("E_COMM_2", "Invalid Resource Version", http.StatusBadRequest)
View Source
var ListQueryParamError = NewError("E_COMM_2", "Query param cannot be null", http.StatusBadRequest)
View Source
var MalformedTokenError = NewError("E_AUTH_2", "Malformed token",
	http.StatusBadRequest)
View Source
var ResourceNotFoundError = NewError("E_COMM_1", "Resource Not Found", http.StatusOK)

Common error

View Source
var TooManyRequestError = NewError("429", "Too Many Request", http.StatusTooManyRequests)
View Source
var UnauthorizedError = NewError("401", "Unauthorized", http.StatusUnauthorized)

Functions

func ExtractAuthValue

func ExtractAuthValue(prefix string, str string) (string, error)

func ExtractBearerAuth

func ExtractBearerAuth(r *http.Request) (token string, err error)

func ParseJSONBody

func ParseJSONBody(r *http.Request, dest interface{}) error

func WrapHandler

func WrapHandler(f func()) gin.HandlerFunc

Types

type DataResponseBadRequest

type DataResponseBadRequest struct {
	Debug map[string]interface{} `json:"__debug"`
}

Data response bad request

type Error

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

func NewError

func NewError(code string, message string, httpStatus int, args ...SetOptionFn) *Error

func (*Error) Code

func (e *Error) Code(code string) *Error

func (*Error) Error

func (e *Error) Error() string

Error implement standard go error interface. If source error is exists then it will print error cause

func (*Error) WithStatus

func (e *Error) WithStatus(httpStatus int) *Error

func (*Error) Wrap

func (e *Error) Wrap(err error) *Error

type Response

type Response struct {
	Success bool              `json:"success" example:"true"`
	Code    string            `json:"code" example:"OK"`
	Message string            `json:"message" example:"Success"`
	Data    interface{}       `json:"data" swaggerignore:"true"`
	Header  map[string]string `json:"-"`
}

Response use standard for response

type ResponseBadRequest

type ResponseBadRequest struct {
	Success bool              `json:"success" example:"false"`
	Code    string            `json:"code" example:"400"`
	Message string            `json:"message" example:"Bad Request"`
	Data    interface{}       `json:"data"`
	Header  map[string]string `json:"-"`
}

type ResponseHandler

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

func NewResponseHandler

func NewResponseHandler(args ...ResponseHandlerOptions) ResponseHandler

func (*ResponseHandler) BadRequest

func (h *ResponseHandler) BadRequest(c *gin.Context, Err *Error) *gin.Context

func (*ResponseHandler) CustomizeError

func (h *ResponseHandler) CustomizeError(c *gin.Context, Err *Error) *gin.Context

func (*ResponseHandler) ErrorHandler

func (h *ResponseHandler) ErrorHandler(c *gin.Context, err error) *gin.Context

func (*ResponseHandler) Forbidden

func (h *ResponseHandler) Forbidden(c *gin.Context, Err *Error) *gin.Context

func (*ResponseHandler) InternalErrorResponse

func (h *ResponseHandler) InternalErrorResponse(c *gin.Context) *gin.Context

func (*ResponseHandler) OK

func (h *ResponseHandler) OK(c *gin.Context) *gin.Context

func (*ResponseHandler) Success

func (h *ResponseHandler) Success(c *gin.Context, data interface{}) *gin.Context

func (*ResponseHandler) Unauthorized

func (h *ResponseHandler) Unauthorized(c *gin.Context, Err *Error) *gin.Context

type ResponseHandlerOptions

type ResponseHandlerOptions struct {
	Debug  bool
	Logger nlogger.Logger
}

type ResponseInternalError

type ResponseInternalError struct {
	Success bool              `json:"success" example:"false"`
	Code    string            `json:"code" example:"500"`
	Message string            `json:"message" example:"Internal Error"`
	Data    interface{}       `json:"data"`
	Header  map[string]string `json:"-"`
}

type ResponseSuccess

type ResponseSuccess struct {
	Success bool              `json:"success" example:"true"`
	Code    string            `json:"code" example:"OK"`
	Message string            `json:"message" example:"Success"`
	Data    interface{}       `json:"data"`
	Header  map[string]string `json:"-"`
}

type SetOptionFn

type SetOptionFn = func(*options)

func SetSuccess

func SetSuccess(success bool) SetOptionFn

Jump to

Keyboard shortcuts

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