http

package module
v1.2.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2023 License: MIT Imports: 22 Imported by: 2

Documentation

Index

Constants

View Source
const ContextKeyRequestIPAddress string = "request_ip"

ContextKeyRequestIPAddress is the key of RequestIP information injected into the request context

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client provides methods for making REST requests

func NewClient

func NewClient(timeout time.Duration) Client

NewClient creates a new Client instance

func (Client) Delete

func (c Client) Delete(ctx context.Context, request HTTPRequest) (HTTPResult, error)

Delete execute a http DELETE method with application/json headers

func (Client) Get

func (c Client) Get(ctx context.Context, request HTTPRequest) (HTTPResult, error)

Get execute a http GET method

func (Client) Patch

func (c Client) Patch(ctx context.Context, request HTTPRequest) (rst HTTPResult, err error)

Patch execute a http PATCH method with application/json headers

func (Client) Post

func (c Client) Post(ctx context.Context, request HTTPRequest) (HTTPResult, error)

Post execute a http POST method with application/json headers

func (Client) PostForm

func (c Client) PostForm(ctx context.Context, request HTTPRequest) (HTTPResult, error)

PostForm execute a http POST method with "application/x-www-form-urlencoded" headers

func (Client) Put

func (c Client) Put(ctx context.Context, request HTTPRequest) (rst HTTPResult, err error)

Put execute a http PUT method with application/json headers

type ErrorListResponse

type ErrorListResponse struct {
	TraceID string         `json:"trace_id,omitempty"`
	Errs    []errorPayload `json:"errors"`
	// contains filtered or unexported fields
}

ErrorListResponse defines how list of errors should be presented to clients of HTTPServer. It implements fasthttp's HTTPError contract (https://github.com/jackwhelpton/fasthttp-routing/blob/master/error.go#L12), so it keeps its error handling behaviour.

func NewErrorListResponse

func NewErrorListResponse(ctx context.Context, errs ...error) ErrorListResponse

NewErrorListResponse creates a new ErrorListResponse object.

func (ErrorListResponse) Error

func (e ErrorListResponse) Error() string

Error returns the error message.

func (ErrorListResponse) StatusCode

func (e ErrorListResponse) StatusCode() int

StatusCode returns the HTTP status code.

func (ErrorListResponse) WithStatusCode

func (e ErrorListResponse) WithStatusCode(status int) ErrorListResponse

WithStatusCode is a way to explicitly define the desired StatusCode to be sent to the client with an ErrorListResponse.

type ErrorResponse

type ErrorResponse struct {
	TraceID string       `json:"trace_id,omitempty"`
	Err     errorPayload `json:"error"`
	// contains filtered or unexported fields
}

ErrorResponse defines how errors should be presented to clients of HTTPServer. It implements fasthttp's HTTPError contract (https://github.com/jackwhelpton/fasthttp-routing/blob/master/error.go#L12), so it keeps its error handling behaviour.

func NewErrorResponse

func NewErrorResponse(ctx context.Context, err error) ErrorResponse

NewErrorResponse creates a new ErrorResponse object.

func (ErrorResponse) Error

func (e ErrorResponse) Error() string

Error returns the error message.

func (ErrorResponse) StatusCode

func (e ErrorResponse) StatusCode() int

StatusCode returns the HTTP status code.

type HTTPHeaders

type HTTPHeaders map[string]string

HTTPHeaders is a map containing the relation key=value of the headers used on the http rest request.

type HTTPQueryParams

type HTTPQueryParams map[string]string

HTTPQueryParams is a map containing the relation key=value of the query params used on the http rest request

type HTTPRequest

type HTTPRequest struct {
	URL         string
	Body        []byte
	Headers     HTTPHeaders
	QueryParams HTTPQueryParams
}

HTTPRequest are the params used to build a new http rest request

type HTTPResult

type HTTPResult struct {
	StatusCode int
	Response   []byte
}

HTTPResult are the params returned from the client HTTP request

type MessageResponse

type MessageResponse struct {
	Message string `json:"message"`
}

MessageResponse is a generic message that should be sent to a client of HTTP Server.

func NewMessageResponse

func NewMessageResponse(msg string, params ...interface{}) MessageResponse

NewMessageResponse creates a MessageResponse

type ResourceCreatedResponse

type ResourceCreatedResponse struct {
	ID      string `json:"id"`
	Message string `json:"message"`
}

ResourceCreatedResponse is is the default response sent when a new resource is created in the system.

func NewResourceCreatedResponse

func NewResourceCreatedResponse(id string) ResourceCreatedResponse

NewResourceCreatedResponse creates a new ResourceCreatedResponse.

func (ResourceCreatedResponse) WithMessage

func (r ResourceCreatedResponse) WithMessage(msg string, params ...interface{}) ResourceCreatedResponse

WithMessage overrides the default message.

type Server

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

Server is an HTTPServer object that can serve HTTP requests It is based on httpfast implementation

func NewServer

func NewServer(in ServerInput) Server

NewServer creates a new instance of Server

func (Server) HandleRequestInMemory added in v1.2.0

func (s Server) HandleRequestInMemory(req *http.Request) (*http.Response, error)

Handle a request in memory using the server router

func (Server) Run

func (s Server) Run() error

Run listen and serves HTTP requests at the Port specified in Server construction

type ServerInput

type ServerInput struct {
	Port           int
	ReadBufferSize int // Bytes, e.g. 4096 == 4kB
	AllowedOrigins []string
	AllowedHeaders []string
	ExposedHeaders []string
	Handler        func(*routing.Router)
	Logger         logger
}

ServerInput encapsulates the necessary Inputs to initialize a Server

Directories

Path Synopsis
middleware
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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