http_response

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2022 License: BSD-3-Clause Imports: 4 Imported by: 0

README

std-http-response

My own standard of HTTP Response.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Body

type Body struct {
	Success   bool        `json:"success"`
	Timestamp time.Time   `json:"timestamp,omitempty"`
	Message   string      `json:"message,omitempty"`
	Errors    []Error     `json:"errors,omitempty"`
	Data      interface{} `json:"data,omitempty"`
}

Body defines standard HTTP body body.

func NewEmpty

func NewEmpty() *Body

NewEmpty creates a new empty HTTP Response Body.

func (*Body) GetData

func (response *Body) GetData() (data interface{}, exists bool)

GetData returns the HTTP Response Body's data and whether it's exist or not.

func (*Body) GetErrors

func (response *Body) GetErrors() (errors []Error, exists bool)

GetErrors returns the HTTP Response Body's errors and whether it's exist or not.

func (*Body) GetMessage

func (response *Body) GetMessage() (message string, exists bool)

GetMessage returns the HTTP Response Body's message and whether it's exist or not.

type Builder

type Builder interface {
	// AppendError add an error to HTTP Response Body.
	AppendError(code int64, reason string) Builder

	// AppendErrors add multiple errors to HTTP Response Body.
	AppendErrors(errors ...Error) Builder

	// Build returns the HTTP Response Body.
	Build() Body

	// Clean all the information set to HTTP Response Body.
	Clean() Builder

	// SetData set the HTTP Response Body's Data.
	SetData(data interface{}) Builder

	// SetErrors set the HTTP Response Body's Errors.
	SetErrors(errors ...Error) Builder

	// SetMessage set the HTTP Response Body's Message.
	SetMessage(message string) Builder

	// SetStatusCode set the status code of HTTP Response.
	SetStatusCode(statusCode int) Builder

	// Send the HTTP Response via the SendFunc callback provided.
	Send(callback SendFunc) error

	// Write the HTTP Response to the http.ResponseWriter.
	Write(writer http.ResponseWriter) (int, error)
}

Builder defines methods to build HTTP Response Body.

func NewBuilder

func NewBuilder() Builder

NewBuilder create a new Builder instance.

type Error

type Error struct {
	Code   int64  `json:"code"`
	Reason string `json:"reason"`
}

Error defines the error information may be contained in the HTTP Response Body.

type SendFunc

type SendFunc func(statusCode int, value interface{}) error

Jump to

Keyboard shortcuts

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