Documentation
¶
Overview ¶
Package rest provides a set of utilities for making RESTful API calls.
This package includes functions for sending HTTP requests, handling responses, and managing authentication and authorization headers.
Example usage:
// Create a new REST client client := rest.NewClient() // Set the base URL for the API client.SetBaseURL("https://api.example.com") // Set the authentication token client.SetAuthToken("YOUR_AUTH_TOKEN") // Send a GET request response, err := client.Get("/users") if err != nil { log.Fatal(err) } // Print the response body fmt.Println(response.Body) // Close the response body response.Close()
For more information and examples, please refer to the package documentation.
Index ¶
- Constants
- Variables
- func CreateMultipartHeader(param, fileName, contentType string) textproto.MIMEHeader
- func IsValidMultipartVerb(method string) (err error)
- func WriteMultipartFormFile(w *multipart.Writer, fieldName, fileName string, r io.Reader) error
- type AuthHandlerFunc
- type Client
- type ClientOpts
- type ClientOptsBuilder
- func (co *ClientOptsBuilder) BaseUrl(baseurl string) (err error)
- func (co *ClientOptsBuilder) Build() *ClientOpts
- func (co *ClientOptsBuilder) CaCerts(caFilePath ...string) *ClientOptsBuilder
- func (co *ClientOptsBuilder) CodecOpts(options map[string]any) *ClientOptsBuilder
- func (co *ClientOptsBuilder) CookieJar(jar http.CookieJar) *ClientOptsBuilder
- func (co *ClientOptsBuilder) EnvProxy(proxyBasicAuth string) *ClientOptsBuilder
- func (co *ClientOptsBuilder) ErrOnStatus(httpStatusCodes ...int) *ClientOptsBuilder
- func (co *ClientOptsBuilder) ExpectContinueTimeoutMs(t int) *ClientOptsBuilder
- func (co *ClientOptsBuilder) IdleTimeoutMs(t int) *ClientOptsBuilder
- func (co *ClientOptsBuilder) MaxIdlePerHost(maxIdleConnPerHost int) *ClientOptsBuilder
- func (co *ClientOptsBuilder) ProxyAuth(user, password, bypass string) *ClientOptsBuilder
- func (co *ClientOptsBuilder) RequestTimeoutMs(t int) *ClientOptsBuilder
- func (co *ClientOptsBuilder) SSLVerify(verify bool) *ClientOptsBuilder
- func (co *ClientOptsBuilder) TlsCerts(certs ...tls.Certificate) *ClientOptsBuilder
- func (co *ClientOptsBuilder) TlsHandShakeTimeoutMs(t int) *ClientOptsBuilder
- type HandlerFunc
- type MultipartFile
- type Paramtype
- type Request
- func (r *Request) AddFormData(k string, v ...string) *Request
- func (r *Request) AddHeader(k string, v ...string) *Request
- func (r *Request) AddPathParam(k string, v string) *Request
- func (r *Request) AddQueryParam(k string, v ...string) *Request
- func (r *Request) Method() string
- func (r *Request) SeBodyReader(reader io.Reader) *Request
- func (r *Request) SetBody(v interface{}) *Request
- func (r *Request) SetContentType(contentType string) *Request
- func (r *Request) SetMultipartFiles(files ...*MultipartFile) *Request
- type Response
- type Server
- type ServerContext
- func (c *ServerContext) GetBody() (io.Reader, error)
- func (c *ServerContext) GetHeader(name string) string
- func (c *ServerContext) GetMethod() string
- func (c *ServerContext) GetParam(name string, typ Paramtype) (string, error)
- func (c *ServerContext) GetRequest() *http.Request
- func (c *ServerContext) GetURL() string
- func (c *ServerContext) HttpResWriter() http.ResponseWriter
- func (c *ServerContext) InHeaders() http.Header
- func (c *ServerContext) Read(obj interface{}) error
- func (c *ServerContext) SetContentType(contentType string)
- func (c *ServerContext) SetCookie(cookie *http.Cookie)
- func (c *ServerContext) SetHeader(name, value string)
- func (c *ServerContext) SetStatusCode(statusCode int)
- func (c *ServerContext) Write(data interface{}, contentType string) error
- func (c *ServerContext) WriteData(data []byte) (int, error)
- func (c *ServerContext) WriteFrom(data io.Reader)
- func (c *ServerContext) WriteJSON(data interface{}) error
- func (c *ServerContext) WriteString(data string)
- func (c *ServerContext) WriteXML(data interface{}) error
- func (c *ServerContext) WriteYAML(data interface{}) error
- type SrvOptions
- func (o *SrvOptions) GetCertPath() string
- func (o *SrvOptions) GetEnableTLS() bool
- func (o *SrvOptions) GetListenHost() string
- func (o *SrvOptions) GetListenPort() int16
- func (o *SrvOptions) GetPrivateKeyPath() string
- func (o *SrvOptions) SetCertPath(certPath string) *SrvOptions
- func (o *SrvOptions) SetEnableTLS(enableTLS bool) *SrvOptions
- func (o *SrvOptions) SetListenHost(host string) *SrvOptions
- func (o *SrvOptions) SetListenPort(port int16) *SrvOptions
- func (o *SrvOptions) SetPrivateKeyPath(privateKeyPath string) *SrvOptions
- func (o *SrvOptions) Validate() error
Constants ¶
const ( // ContentTypeHeader ContentTypeHeader = "Content-Type" // JSONContentType JSONContentType = "application/json" // XMLContentType XMLContentType = "text/xml" // XmlApplicationContentType XmlApplicationContentType = "application/xml" // YAMLContentType YAMLContentType = "text/yaml" // ProxyAuthorizationHeader ProxyAuthorization = "Proxy-Authorization" // AuthorizationHeader Authorization // AcceptHeader AcceptHeader = "Accept" // AcceptEncodingHeader AcceptEncodingHeader = "Accept-Encoding" // AcceptLanguageHeader AcceptLanguageHeader = "Accept-Language" // PathSeparator PathSeparator = "/" )
Variables ¶
var ErrInvalidCertPath = errors.New("empty cert path")
var ErrInvalidConfig = errors.New("empty config path")
var ErrInvalidID = errors.New("empty id")
var ErrInvalidListenHost = errors.New("empty listen host")
var ErrInvalidListenPort = errors.New("empty listen port")
var ErrInvalidParamType = errors.New("invalid param type provided")
var ErrInvalidPrivateKeyPath = errors.New("empty private key path")
var ErrNilOptions = errors.New("nil options")
Functions ¶
func CreateMultipartHeader ¶ added in v1.2.0
func CreateMultipartHeader(param, fileName, contentType string) textproto.MIMEHeader
CreateMultipartHeader creates a multipart header with the given parameters
func IsValidMultipartVerb ¶ added in v1.2.0
IsValidMultipartVerb checks if the method is valid for multipart content
Types ¶
type AuthHandlerFunc ¶ added in v1.2.6
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents a REST client.
func NewClientWithOptions ¶ added in v1.2.6
func NewClientWithOptions(options *ClientOpts) *Client
type ClientOpts ¶ added in v1.2.6
type ClientOpts struct { *clients.ClientOptions AuthHandlers map[clients.AuthType]AuthHandlerFunc // contains filtered or unexported fields }
type ClientOptsBuilder ¶ added in v1.2.6
type ClientOptsBuilder struct { *clients.OptionsBuilder // contains filtered or unexported fields }
func CliOptsBuilder ¶ added in v1.2.6
func CliOptsBuilder() *ClientOptsBuilder
func (*ClientOptsBuilder) BaseUrl ¶ added in v1.2.6
func (co *ClientOptsBuilder) BaseUrl(baseurl string) (err error)
func (*ClientOptsBuilder) Build ¶ added in v1.2.6
func (co *ClientOptsBuilder) Build() *ClientOpts
func (*ClientOptsBuilder) CaCerts ¶ added in v1.2.6
func (co *ClientOptsBuilder) CaCerts(caFilePath ...string) *ClientOptsBuilder
func (*ClientOptsBuilder) CodecOpts ¶ added in v1.2.6
func (co *ClientOptsBuilder) CodecOpts(options map[string]any) *ClientOptsBuilder
func (*ClientOptsBuilder) CookieJar ¶ added in v1.2.6
func (co *ClientOptsBuilder) CookieJar(jar http.CookieJar) *ClientOptsBuilder
func (*ClientOptsBuilder) EnvProxy ¶ added in v1.2.6
func (co *ClientOptsBuilder) EnvProxy(proxyBasicAuth string) *ClientOptsBuilder
func (*ClientOptsBuilder) ErrOnStatus ¶ added in v1.2.6
func (co *ClientOptsBuilder) ErrOnStatus(httpStatusCodes ...int) *ClientOptsBuilder
func (*ClientOptsBuilder) ExpectContinueTimeoutMs ¶ added in v1.2.6
func (co *ClientOptsBuilder) ExpectContinueTimeoutMs(t int) *ClientOptsBuilder
func (*ClientOptsBuilder) IdleTimeoutMs ¶ added in v1.2.6
func (co *ClientOptsBuilder) IdleTimeoutMs(t int) *ClientOptsBuilder
func (*ClientOptsBuilder) MaxIdlePerHost ¶ added in v1.2.6
func (co *ClientOptsBuilder) MaxIdlePerHost(maxIdleConnPerHost int) *ClientOptsBuilder
func (*ClientOptsBuilder) ProxyAuth ¶ added in v1.2.6
func (co *ClientOptsBuilder) ProxyAuth(user, password, bypass string) *ClientOptsBuilder
func (*ClientOptsBuilder) RequestTimeoutMs ¶ added in v1.2.6
func (co *ClientOptsBuilder) RequestTimeoutMs(t int) *ClientOptsBuilder
func (*ClientOptsBuilder) SSLVerify ¶ added in v1.2.6
func (co *ClientOptsBuilder) SSLVerify(verify bool) *ClientOptsBuilder
func (*ClientOptsBuilder) TlsCerts ¶ added in v1.2.6
func (co *ClientOptsBuilder) TlsCerts(certs ...tls.Certificate) *ClientOptsBuilder
func (*ClientOptsBuilder) TlsHandShakeTimeoutMs ¶ added in v1.2.6
func (co *ClientOptsBuilder) TlsHandShakeTimeoutMs(t int) *ClientOptsBuilder
type HandlerFunc ¶ added in v1.2.0
type HandlerFunc func(context ServerContext)
type MultipartFile ¶
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request struct holds the http Request for the rest client
func (*Request) AddFormData ¶
AddFormData function adds the form data with the name specified by k list of values in order as specified in v If the key does not exist then it creates a new form data by calling url.Values.Set() function on the first key and the value Setting form data will have precedence over to setting body directly.
func (*Request) AddPathParam ¶
AddPathParam function adds the path parameter with key as the name of the parameter and v as the value of the parameter that needs to be replaced
func (*Request) AddQueryParam ¶
AddQueryParam function adds the query parameter with the name specified by k list of values in order as specified in v If the key does not exist then it creates a new form data by calling url.Values.Set() function passing the first key and value
func (*Request) SetContentType ¶
func (*Request) SetMultipartFiles ¶
func (r *Request) SetMultipartFiles(files ...*MultipartFile) *Request
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
func (*Response) Decode ¶
Decode Function decodes the response body to a suitable object. The format of the body is determined by Content-Type header in the response
func (*Response) StatusCode ¶
StatusCode provides the status code of the response
type Server ¶ added in v1.2.0
type Server interface { // Server is a lifecytcle component lifecycle.Component // Opts returns the options of the server Opts() *SrvOptions // AddRoute adds a route to the server AddRoute(path string, handler HandlerFunc, method ...string) (route *turbo.Route, err error) // AddRoute adds a route to the server Post(path string, handler HandlerFunc) (route *turbo.Route, err error) // AddRoute adds a route to the server Get(path string, handler HandlerFunc) (route *turbo.Route, err error) // AddRoute adds a route to the server Put(path string, handler HandlerFunc) (route *turbo.Route, err error) // AddRoute adds a route to the server Delete(path string, handler HandlerFunc) (route *turbo.Route, err error) // Unhandled adds a handler for unhandled routes Unhandled(handler HandlerFunc) (err error) // Unsupported adds a handler for unsupported methods Unsupported(handler HandlerFunc) (err error) // AddGlobalFilter adds a global filter to the server AddGlobalFilter(filter turbo.FilterFunc) (err error) //Turbo returns the turbo router Router() *turbo.Router }
Server is the interface that wraps the ServeHTTP method.
func DefaultServer ¶ added in v1.2.0
DefaultServer creates a new Server with the default options.
func NewServer ¶ added in v1.2.0
func NewServer(opts *SrvOptions) (rServer Server, err error)
NewServer creates a new Server with the given options.
func NewServerFrom ¶ added in v1.2.0
New creates a new Server with the given configuration file of the options.
type ServerContext ¶ added in v1.2.1
type ServerContext struct {
// contains filtered or unexported fields
}
ServerContext is the struct that holds the request and response of the server.
func (*ServerContext) GetBody ¶ added in v1.2.1
func (c *ServerContext) GetBody() (io.Reader, error)
GetBody returns the body of the request.
func (*ServerContext) GetHeader ¶ added in v1.2.1
func (c *ServerContext) GetHeader(name string) string
GetHeader returns the header of the request.
func (*ServerContext) GetMethod ¶ added in v1.2.1
func (c *ServerContext) GetMethod() string
GetMethod returns the method of the request.
func (*ServerContext) GetParam ¶ added in v1.2.1
func (c *ServerContext) GetParam(name string, typ Paramtype) (string, error)
Options is the struct that holds the configuration for the Server.
func (*ServerContext) GetRequest ¶ added in v1.2.1
func (c *ServerContext) GetRequest() *http.Request
GetRequest returns the request. for most Rest Use cases this would not be required
func (*ServerContext) GetURL ¶ added in v1.2.1
func (c *ServerContext) GetURL() string
GetURL returns the URL of the request.
func (*ServerContext) HttpResWriter ¶ added in v1.2.1
func (c *ServerContext) HttpResWriter() http.ResponseWriter
HttpResWriter returns the http.ResponseWriter
func (*ServerContext) InHeaders ¶ added in v1.2.1
func (c *ServerContext) InHeaders() http.Header
InHeaders returns the headers of the request.
func (*ServerContext) Read ¶ added in v1.2.1
func (c *ServerContext) Read(obj interface{}) error
Read reads the body of the request into the given object.
func (*ServerContext) SetContentType ¶ added in v1.2.1
func (c *ServerContext) SetContentType(contentType string)
SetContentType sets the content type of the response.
func (*ServerContext) SetCookie ¶ added in v1.2.1
func (c *ServerContext) SetCookie(cookie *http.Cookie)
SetCookie sets the cookie of the response.
func (*ServerContext) SetHeader ¶ added in v1.2.1
func (c *ServerContext) SetHeader(name, value string)
SetHeader sets the header of the response.
func (*ServerContext) SetStatusCode ¶ added in v1.2.1
func (c *ServerContext) SetStatusCode(statusCode int)
SetStatusCode sets the status code of the response.
func (*ServerContext) Write ¶ added in v1.2.1
func (c *ServerContext) Write(data interface{}, contentType string) error
Write writes the object to the response with the given content type and status code.
func (*ServerContext) WriteData ¶ added in v1.2.1
func (c *ServerContext) WriteData(data []byte) (int, error)
WriteData writes the data to the response.
func (*ServerContext) WriteFrom ¶ added in v1.2.1
func (c *ServerContext) WriteFrom(data io.Reader)
WriteFrom writes the data from the reader to the response.
func (*ServerContext) WriteJSON ¶ added in v1.2.1
func (c *ServerContext) WriteJSON(data interface{}) error
WriteJSON writes the object to the response in JSON format.
func (*ServerContext) WriteString ¶ added in v1.2.1
func (c *ServerContext) WriteString(data string)
WriteString writes the string to the response.
func (*ServerContext) WriteXML ¶ added in v1.2.1
func (c *ServerContext) WriteXML(data interface{}) error
WriteXML writes the object to the response in XML format.
func (*ServerContext) WriteYAML ¶ added in v1.2.1
func (c *ServerContext) WriteYAML(data interface{}) error
WriteYAML writes the object to the response in YAML format.
type SrvOptions ¶ added in v1.2.0
type SrvOptions struct { Id string `json:"id" yaml:"id" bson:"id" mapstructure:"id"` PathPrefix string `json:"path_prefix,omitempty" yaml:"path_prefix,omitempty" bson:"path_prefix,omitempty" mapstructure:"path_prefix,omitempty"` ListenHost string `json:"listen_host" yaml:"listen_host" bson:"listen_host" mapstructure:"listen_host"` ListenPort int16 `json:"listen_port" yaml:"listen_port" bson:"listen_port" mapstructure:"listen_port"` ReadTimeout int64 `` /* 127-byte string literal not displayed */ WriteTimeout int64 `` /* 131-byte string literal not displayed */ EnableTLS bool `json:"enable_tls" yaml:"enable_tls" bson:"enable_tls" mapstructure:"enable_tls"` PrivateKeyPath string `` /* 138-byte string literal not displayed */ CertPath string `json:"cert_path,omitempty" yaml:"cert_path,omitempty" bson:"cert_path,omitempty" mapstructure:"cert,omitempty"` Cors *filters.CorsOptions `json:"cors,omitempty" yaml:"cors,omitempty" bson:"cors,omitempty" mapstructure:"cors,omitempty"` }
SrvOptions is the configuration for the server
func DefaultSrvOptions ¶ added in v1.2.0
func DefaultSrvOptions() *SrvOptions
DefaultSrvOptions returns the default options for the server The default options are:
- PathPrefix: "/"
- Id: "default-http-server"
- ListenHost: "localhost"
- ListenPort: 8080
- ReadTimeout: 20000
- WriteTimeout: 20000
- Cors: &filters.CorsOptions{ MaxAge: 0, AllowedOrigins: []string{"*"}, AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"}, ResponseStatus: http.StatusNoContent, }
func EmptySrvOptions ¶ added in v1.2.0
func EmptySrvOptions() *SrvOptions
EmptySrvOptions returns a new server options
func (*SrvOptions) GetCertPath ¶ added in v1.2.0
func (o *SrvOptions) GetCertPath() string
GetCertPath returns the cert path
func (*SrvOptions) GetEnableTLS ¶ added in v1.2.0
func (o *SrvOptions) GetEnableTLS() bool
GetEnableTLS returns the enable TLS value
func (*SrvOptions) GetListenHost ¶ added in v1.2.0
func (o *SrvOptions) GetListenHost() string
GetListenHost returns the listen host
func (*SrvOptions) GetListenPort ¶ added in v1.2.0
func (o *SrvOptions) GetListenPort() int16
GetListenPort returns the listen port
func (*SrvOptions) GetPrivateKeyPath ¶ added in v1.2.0
func (o *SrvOptions) GetPrivateKeyPath() string
GetPrivateKeyPath returns the private key path
func (*SrvOptions) SetCertPath ¶ added in v1.2.0
func (o *SrvOptions) SetCertPath(certPath string) *SrvOptions
SetCertPath sets the cert path
func (*SrvOptions) SetEnableTLS ¶ added in v1.2.0
func (o *SrvOptions) SetEnableTLS(enableTLS bool) *SrvOptions
SetEnableTLS sets the enable TLS value
func (*SrvOptions) SetListenHost ¶ added in v1.2.0
func (o *SrvOptions) SetListenHost(host string) *SrvOptions
SetListenHost sets the listen host
func (*SrvOptions) SetListenPort ¶ added in v1.2.0
func (o *SrvOptions) SetListenPort(port int16) *SrvOptions
SetListenPort sets the listen port
func (*SrvOptions) SetPrivateKeyPath ¶ added in v1.2.0
func (o *SrvOptions) SetPrivateKeyPath(privateKeyPath string) *SrvOptions
SetPrivateKeyPath sets the private key path
func (*SrvOptions) Validate ¶ added in v1.2.0
func (o *SrvOptions) Validate() error
Validate validates the server options