api

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2024 License: MIT Imports: 12 Imported by: 1

README

api

api is a Go library for building RESTful API servers

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTTPError

func HTTPError(code int, f any, a ...any) error

HTTPError returns an error with an embedded HTTP status code

func Handler

func Handler(handler any, permFuncs ...func(*Request) bool) http.Handler

Handler registers a http.Handler from a handler function.

handler must be a function with one of these signatures:

  • http.Handler
  • func (http.ResponseWriter, *http.Request)
  • func [Input, Output any] (*Request, Input) (Output, error)
  • func [Output any] (*Request) (Output, error)

If there are permFuncs, at least one of them must succeed.

Types

type Client

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

Client is a way to connect to 3rd party API servers.

func NewClient

func NewClient(apiEndPoint string) *Client

NewClient creates a new Client ready to use.

func (*Client) Delete

func (c *Client) Delete(url string, dest any) error

Delete makes a HTTP DELETE request to the API.

func (*Client) DisallowUnknownFields added in v0.0.2

func (c *Client) DisallowUnknownFields() *Client

DisallowUnknownFields causes the JSON decoder to return an error when the destination is a struct and the input contains object keys which do not match any non-ignored, exported fields in the destination.

func (*Client) Get

func (c *Client) Get(url string, dest any) error

Get makes a HTTP GET request to the API.

func (*Client) Post

func (c *Client) Post(url string, data any, dest any) error

Post makes a HTTP POST request to the API.

func (*Client) Put

func (c *Client) Put(url string, data any, dest any) error

Put makes a HTTP PUT request to the API.

func (*Client) Request

func (c *Client) Request(method, URL string, data any, dest any) error

Request makes a HTTP request to the API. If data is not a []byte, it will be encoding as a JSON object.

func (*Client) WithHeaderToken

func (c *Client) WithHeaderToken(ht string) *Client

WithHeaderToken specifies which Header line to use when sending a token.

func (*Client) WithParamToken

func (c *Client) WithParamToken(pt string) *Client

WithParamToken specifies which query parameter to use when sending a token.

func (*Client) WithToken

func (c *Client) WithToken(tk string) *Client

WithToken adds a token to a Client.

func (*Client) WithTokenPrefix

func (c *Client) WithTokenPrefix(tp string) *Client

WithTokenPrefix adds an optional prefix to the token in the Header line.

type Request

type Request struct {
	*http.Request
}

Request encapsulates a *http.Request to be able to use the Get and Set methods.

func (*Request) Get

func (r *Request) Get(key string) any

Get retrieves a value from a given key in this Request.

func (*Request) Set

func (r *Request) Set(key string, value any)

Set assigns a value to a given key for this Request. Calls to Request.Set must not be concurrent.

type Server

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

Server is an HTTP request multiplexer.

func NewServer

func NewServer() *Server

NewServer allocates and returns a new Server.

func (*Server) AddMiddleware

func (s *Server) AddMiddleware(f func(next http.Handler) http.Handler)

AddMiddleware adds a new middleware to the Server. This should only be called before the first call to ServeHTTP.

func (*Server) Get

func (s *Server) Get(key string) any

Get retrieves a value from a given key in this Server.

func (*Server) Handle

func (s *Server) Handle(pattern string, handler any, permFuncs ...func(*Request) bool)

Handle registers a handler in the server.

handler must be a function with one of these signatures:

  • http.Handler
  • func (http.ResponseWriter, *http.Request)
  • func [Input, Output any] (*Request, Input) (Output, error)
  • func [Output any] (*Request) (Output, error)

If there are permFuncs, at least one of them must succeed.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP creates a Request, runs the middleware functions, and dispatches the HTTP request to the correct handler from those registered in the server.

func (*Server) Set

func (s *Server) Set(key string, value any)

Set assigns a value to a given key for all the requests in a given server. Calls to Server.Set must not be concurrent.

Jump to

Keyboard shortcuts

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