rapi

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2023 License: BSD-3-Clause Imports: 20 Imported by: 0

README

rAPI

Go Reference Maintainability Rating Quality Gate Status

rAPI is a Go (Golang) package that simplifies building and consuming RESTful APIs. It provides an HTTP handler for creating APIs and a client for making API requests.

Installation

You can install rAPI using the go get command:

go get github.com/goinsane/rapi

Contributing

Contributions are welcome! If you find a bug or want to add a feature, please open an issue or create a pull request.

License

This project is licensed under the BSD License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CallOption added in v0.8.0

type CallOption interface {
	// contains filtered or unexported methods
}

CallOption configures how we set up the http call.

func WithAdditionalRequestHeader added in v0.2.0

func WithAdditionalRequestHeader(header ...http.Header) CallOption

WithAdditionalRequestHeader returns a CallOption that adds the given http headers to the request headers.

func WithErrOut added in v0.3.0

func WithErrOut(errOut error) CallOption

WithErrOut returns a CallOption that defines the error output to return as Caller.Call error.

func WithForceBody added in v0.5.0

func WithForceBody(forceBody bool) CallOption

WithForceBody returns a CallOption that forces sending input in the request body for all methods including HEAD and GET.

func WithMaxResponseBodySize added in v0.2.0

func WithMaxResponseBodySize(maxResponseBodySize int64) CallOption

WithMaxResponseBodySize returns a CallOption that limits maximum response body size.

func WithRequestHeader added in v0.2.0

func WithRequestHeader(header ...http.Header) CallOption

WithRequestHeader returns a CallOption that sets the given http headers to the request headers.

type Caller

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

Caller is the HTTP requester to do JSON requests with the given method to the given endpoint. The method and endpoint are given from Factory.

func (*Caller) Call

func (c *Caller) Call(ctx context.Context, in interface{}, opts ...CallOption) (result *Response, err error)

Call does the HTTP request with the given input and CallOption's.

type DoFunc

type DoFunc func(req *Request, send SendFunc)

DoFunc is a function type to process requests from Handler.

type Factory added in v0.2.0

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

Factory is Caller factory to create new Caller's.

func NewFactory added in v0.2.0

func NewFactory(client *http.Client, u *url.URL, opts ...CallOption) (f *Factory)

NewFactory creates a new Factory.

func (*Factory) Caller added in v0.2.0

func (f *Factory) Caller(endpoint string, method string, out interface{}, opts ...CallOption) *Caller

Caller creates a new Caller with the given endpoint and method.

type Handler

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

Handler implements http.Handler to process JSON requests based on pattern and registered methods. Handler is similar to http.ServeMux in terms of operation.

func NewHandler

func NewHandler(opts ...HandlerOption) (h *Handler)

NewHandler creates a new Handler by given HandlerOption's.

func (*Handler) Handle

func (h *Handler) Handle(pattern string, opts ...HandlerOption) Registrar

Handle creates a Registrar to register methods for the given pattern.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is implementation of http.Handler.

type HandlerOption

type HandlerOption interface {
	// contains filtered or unexported methods
}

HandlerOption sets options such as middleware, read timeout, etc.

func WithAllowEncoding added in v0.2.0

func WithAllowEncoding(allowEncoding bool) HandlerOption

WithAllowEncoding returns a HandlerOption that allows encoded content types such as gzip to be returned. By default, encoding is allowed.

func WithMaxRequestBodySize

func WithMaxRequestBodySize(maxRequestBodySize int64) HandlerOption

WithMaxRequestBodySize returns a HandlerOption that limits maximum request body size.

func WithMiddleware

func WithMiddleware(middleware ...MiddlewareFunc) HandlerOption

WithMiddleware returns a HandlerOption that adds middlewares.

func WithOnError

func WithOnError(onError func(error, *http.Request)) HandlerOption

WithOnError returns a HandlerOption that sets the function to be called on error.

func WithReadTimeout added in v0.9.0

func WithReadTimeout(readTimeout time.Duration) HandlerOption

WithReadTimeout returns a HandlerOption that limits maximum request body read duration.

func WithWriteTimeout added in v0.9.0

func WithWriteTimeout(writeTimeout time.Duration) HandlerOption

WithWriteTimeout returns a HandlerOption that limits maximum response body write duration.

type HeaderOption added in v0.2.0

type HeaderOption struct {
	KeyVals []HeaderOptionKeyVal
	Map     map[string]string
}

func ParseHeaderOptions added in v0.2.0

func ParseHeaderOptions(directive string) (options []HeaderOption)

type HeaderOptionKeyVal added in v0.2.0

type HeaderOptionKeyVal struct {
	Key string
	Val string
}

type InvalidContentTypeError added in v0.6.0

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

InvalidContentTypeError occurs when the request or response body content type is invalid.

func (*InvalidContentTypeError) ContentType added in v0.6.0

func (e *InvalidContentTypeError) ContentType() string

ContentType returns the invalid content type.

func (*InvalidContentTypeError) Error added in v0.6.0

func (e *InvalidContentTypeError) Error() string

Error is the implementation of error.

type MiddlewareFunc added in v0.2.0

type MiddlewareFunc func(req *Request, send SendFunc, do DoFunc)

MiddlewareFunc is a function type to process requests as middleware from Handler.

type Registrar added in v0.8.0

type Registrar interface {
	// Register registers method with the given parameters to Handler. The pattern was given from Handler.Handle.
	Register(method string, in interface{}, do DoFunc, opts ...HandlerOption) Registrar
}

Registrar is method registrar and created by Handler.Handle.

type Request

type Request struct {
	*http.Request
	Data []byte
	In   interface{}
}

Request encapsulates http.Request and gives input from request. It is used in DoFunc and MiddlewareFunc.

type RequestError added in v0.6.0

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

RequestError is the request error from http.Client. It is returned from Caller.Call.

func (*RequestError) Error added in v0.6.0

func (e *RequestError) Error() string

Error is the implementation of error.

type Response

type Response struct {
	*http.Response
	Data []byte
	Out  interface{}
}

Response encapsulates http.Response and gives data and output from response. It is returned from Caller.Call.

type SendFunc

type SendFunc func(out interface{}, code int, header ...http.Header)

SendFunc is a function type to send response in DoFunc or MiddlewareFunc.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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