shttp

package
v0.0.0-...-c393e39 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: ISC Imports: 29 Imported by: 2

Documentation

Index

Constants

View Source
const (
	DefaultClientConnectionTimeout = 30 // seconds
	DefaultClientRequestTimeout    = 30 // seconds
)

Variables

This section is empty.

Functions

func AdaptativeErrorHandler

func AdaptativeErrorHandler(h *Handler, status int, code string, msg string, data ErrorData)

func DefaultErrorHandler

func DefaultErrorHandler(h *Handler, status int, code string, msg string, data ErrorData)

func HandleAPIClientError

func HandleAPIClientError(res *http.Response, body []byte) error

func JSONErrorHandler

func JSONErrorHandler(h *Handler, status int, code string, msg string, data ErrorData)

func LoadCertificates

func LoadCertificates(certificates []string) (*x509.CertPool, error)

func ParseAbsoluteURI

func ParseAbsoluteURI(s string, schemes []string) (*url.URL, error)

func ParseHTTPSURI

func ParseHTTPSURI(s string) (*url.URL, error)

func ParseHTTPURI

func ParseHTTPURI(s string) (*url.URL, error)

func RequestAcceptsText

func RequestAcceptsText(req *http.Request) bool

Types

type APIClient

type APIClient struct {
	Cfg    APIClientCfg
	Client *http.Client

	BasicUsername string
	BasicPassword string
	BearerToken   string
	Cookie        *http.Cookie
	// contains filtered or unexported fields
}

func NewAPIClient

func NewAPIClient(cfg APIClientCfg) (*APIClient, error)

func (*APIClient) SendRequest

func (c *APIClient) SendRequest(method, uriRefString string, reqBody, resBody any) (*http.Response, error)

func (*APIClient) SendRequestWithHeader

func (c *APIClient) SendRequestWithHeader(method, uriRefString string, header http.Header, reqBody, resBody any) (*http.Response, error)

type APIClientCfg

type APIClientCfg struct {
	Client      *http.Client          `json:"-"`
	HandleError APIClientErrorHandler `json:"-"`
	BaseURI     string                `json:"base_uri"`
}

type APIClientErrorHandler

type APIClientErrorHandler func(*http.Response, []byte) error

type Client

type Client struct {
	Cfg ClientCfg
	Log *log.Logger

	Client *http.Client
	// contains filtered or unexported fields
}

func NewClient

func NewClient(cfg ClientCfg) (*Client, error)

func (*Client) CloseConnections

func (c *Client) CloseConnections()

func (*Client) DialTLSContext

func (c *Client) DialTLSContext(ctx context.Context, network, address string) (net.Conn, error)

func (*Client) Do

func (c *Client) Do(req *http.Request) (*http.Response, error)

type ClientCfg

type ClientCfg struct {
	Log *log.Logger `json:"-"`

	ConnectionTimeout *int `json:"connection_timeout"` // seconds
	RequestTimeout    *int `json:"request_timeout"`    // seconds

	LogRequests         bool `json:"log_requests"`
	DisableRedirections bool `json:"disable_redirections"`

	TLS *TLSClientCfg `json:"tls"`

	Header http.Header `json:"-"`
}

func (*ClientCfg) ValidateJSON

func (cfg *ClientCfg) ValidateJSON(v *ejson.Validator)

type ErrorData

type ErrorData interface{}

type ErrorHandler

type ErrorHandler func(*Handler, int, string, string, ErrorData)

type Handler

type Handler struct {
	Server *Server
	Log    *log.Logger

	Method      string
	PathPattern string
	RouteId     string // based on the method and path pattern

	Request        *http.Request
	Query          url.Values
	ResponseWriter http.ResponseWriter

	ClientAddress string // optional
	RequestId     string // optional
	// contains filtered or unexported fields
}

func (*Handler) AcceptedMediaRanges

func (h *Handler) AcceptedMediaRanges() MediaRanges

func (*Handler) HasQueryParameter

func (h *Handler) HasQueryParameter(name string) bool

func (*Handler) JSONRequestData

func (h *Handler) JSONRequestData(dest interface{}) error

func (*Handler) JSONRequestDataExt

func (h *Handler) JSONRequestDataExt(data []byte, dest interface{}, fn func(*ejson.Validator)) error

The extended version of JSONRequestData is useful in specific situations:

1. JSON data must be extracted and/or transformed from the request body before decoding.

2. Extra validation steps must be performed with the same Validator object.

func (*Handler) PathVariable

func (h *Handler) PathVariable(name string) string

func (*Handler) QueryParameter

func (h *Handler) QueryParameter(name string) string

func (*Handler) Reply

func (h *Handler) Reply(status int, r io.Reader)

func (*Handler) ReplyChunk

func (h *Handler) ReplyChunk(r io.Reader) error

func (*Handler) ReplyCompactJSON

func (h *Handler) ReplyCompactJSON(status int, value interface{})

func (*Handler) ReplyEmpty

func (h *Handler) ReplyEmpty(status int)

func (*Handler) ReplyError

func (h *Handler) ReplyError(status int, code, format string, args ...interface{})

func (*Handler) ReplyErrorData

func (h *Handler) ReplyErrorData(status int, code string, data ErrorData, format string, args ...interface{})

func (*Handler) ReplyFile

func (h *Handler) ReplyFile(filePath string)

func (*Handler) ReplyInternalError

func (h *Handler) ReplyInternalError(status int, format string, args ...interface{})

func (*Handler) ReplyJSON

func (h *Handler) ReplyJSON(status int, value interface{})

func (*Handler) ReplyJSONChunk

func (h *Handler) ReplyJSONChunk(value interface{}) error

func (*Handler) ReplyNotImplemented

func (h *Handler) ReplyNotImplemented(feature string)

func (*Handler) ReplyRedirect

func (h *Handler) ReplyRedirect(status int, uri string)

func (*Handler) ReplyText

func (h *Handler) ReplyText(status int, body string)

func (*Handler) ReplyValidationErrors

func (h *Handler) ReplyValidationErrors(err ejson.ValidationErrors)

func (*Handler) RequestData

func (h *Handler) RequestData() ([]byte, error)

func (*Handler) SetCookie

func (h *Handler) SetCookie(cookie *http.Cookie)

func (*Handler) UUIDPathVariable

func (h *Handler) UUIDPathVariable(name string) (uuid.UUID, error)

func (*Handler) UUIDQueryParameter

func (h *Handler) UUIDQueryParameter(name string) (uuid.UUID, error)

type JSONError

type JSONError struct {
	Code    string          `json:"code,omitempty"`
	Message string          `json:"message"`
	RawData json.RawMessage `json:"data,omitempty"`
	Data    ErrorData       `json:"-"`
}

func (*JSONError) DecodeData

func (err *JSONError) DecodeData(target interface{}) error

func (*JSONError) Error

func (err *JSONError) Error() string

func (*JSONError) MarshalJSON

func (err *JSONError) MarshalJSON() ([]byte, error)

func (*JSONError) UnmarshalJSON

func (err *JSONError) UnmarshalJSON(data []byte) error

type MediaRange

type MediaRange struct {
	Type       string
	Subtype    string
	Parameters map[string]string
	Weight     float64
}

func (*MediaRange) MatchesMediaType

func (mr *MediaRange) MatchesMediaType(fullType string) bool

func (*MediaRange) Parameter

func (mr *MediaRange) Parameter(name string) string

func (*MediaRange) Parse

func (mr *MediaRange) Parse(s string) error

type MediaRanges

type MediaRanges []*MediaRange

func (*MediaRanges) Parse

func (mrs *MediaRanges) Parse(s string)

func (MediaRanges) SelectMediaType

func (mrs MediaRanges) SelectMediaType(mediaTypes ...string) string

type Range

type Range struct {
	Start *int64
	End   *int64
}

func FullRange

func FullRange(start, end int64) Range

func PartialRange

func PartialRange(start int64) Range

func SuffixRange

func SuffixRange(suffixLength int64) Range

func (Range) GoString

func (r Range) GoString() string

func (Range) String

func (r Range) String() string

type Ranges

type Ranges []Range

func (*Ranges) Parse

func (rs *Ranges) Parse(s string) error

type ResponseWriter

type ResponseWriter struct {
	Status int
	// contains filtered or unexported fields
}

func NewResponseWriter

func NewResponseWriter(w http.ResponseWriter) *ResponseWriter

func (*ResponseWriter) Flush

func (w *ResponseWriter) Flush()

func (*ResponseWriter) Header

func (w *ResponseWriter) Header() http.Header

func (*ResponseWriter) Hijack

func (w *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)

func (*ResponseWriter) Write

func (w *ResponseWriter) Write(data []byte) (int, error)

func (*ResponseWriter) WriteHeader

func (w *ResponseWriter) WriteHeader(status int)

type RoundTripper

type RoundTripper struct {
	Cfg *ClientCfg
	Log *log.Logger

	http.RoundTripper
}

func NewRoundTripper

func NewRoundTripper(rt http.RoundTripper, cfg *ClientCfg) *RoundTripper

func (*RoundTripper) RoundTrip

func (rt *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

type RouteFunc

type RouteFunc func(*Handler)

type Server

type Server struct {
	Cfg ServerCfg
	Log *log.Logger
	// contains filtered or unexported fields
}

func NewServer

func NewServer(cfg ServerCfg) (*Server, error)

func (*Server) Route

func (s *Server) Route(pathPattern, method string, routeFunc RouteFunc)

func (*Server) RouteId

func (s *Server) RouteId(method, pathPattern string) string

func (*Server) ServeHTTP

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

func (*Server) Start

func (s *Server) Start() error

func (*Server) Stop

func (s *Server) Stop()

type ServerCfg

type ServerCfg struct {
	Log           *log.Logger    `json:"-"`
	ErrorChan     chan<- error   `json:"-"`
	InfluxClient  *influx.Client `json:"-"`
	Name          string         `json:"-"`
	ErrorHandler  ErrorHandler   `json:"-"`
	DataDirectory string         `json:"-"`

	Address string `json:"address"`

	TLS *TLSServerCfg `json:"tls"`

	LogSuccessfulRequests bool `json:"log_successful_requests"`
	HideInternalErrors    bool `json:"hide_internal_errors"`
	MethodLessRouteIds    bool `json:"method_less_route_ids"`
	ShutdownTimeout       int  `json:"shutdown_timeout"` // seconds
}

func (*ServerCfg) ValidateJSON

func (cfg *ServerCfg) ValidateJSON(v *ejson.Validator)

type TLSClientCfg

type TLSClientCfg struct {
	CACertificates []string `json:"ca_certificates"`
}

type TLSServerCfg

type TLSServerCfg struct {
	Certificate string `json:"certificate"`
	PrivateKey  string `json:"private_key"`
}

func (*TLSServerCfg) ValidateJSON

func (cfg *TLSServerCfg) ValidateJSON(v *ejson.Validator)

type ValidationJSONErrorData

type ValidationJSONErrorData struct {
	ValidationErrors ejson.ValidationErrors `json:"validation_errors"`
}

Jump to

Keyboard shortcuts

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