httpx

package
v0.0.1-alpha Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrStatusCheckFailed  = errors.New("unable to send http request")
	ErrInvalidHTTPRequest = errors.New("invalid http request")
)
View Source
var DefaultCodec = &Codec{
	Decoder:      DefaultDecoderChain(),
	Encoder:      JSONEncoder(),
	ErrorEncoder: JSONErrorEncoder(),
}
View Source
var ErrServerStartTimeout = errors.New("http server start timeout")

Functions

func ContextWithPathParams

func ContextWithPathParams(ctx context.Context, values url.Values) context.Context

ContextWithPathParams

func CookieDecoder

func CookieDecoder() transport.DecoderFunc

func DefaultDecoderChain

func DefaultDecoderChain() transport.Decoder

func DefaultStatusCheckFunc

func DefaultStatusCheckFunc(status string, code int) (err error)

func HeaderDecoder

func HeaderDecoder() transport.DecoderFunc

func HyperMediaDecoder

func HyperMediaDecoder() transport.DecoderFunc

func JSONEncoder

func JSONEncoder() transport.EncoderFunc

JSONEncoder encodes any response as JSON and writes it to the ResponseWriter

func JSONErrorEncoder

func JSONErrorEncoder() transport.ErrorEncoderFunc

JSONErrorEncoder encodes any response as JSON and writes it to the ResponseWriter

func JSONRequest

func JSONRequest[T any](ctx context.Context, params JSONRequestParams) (result T, err error)

JSONRequest function is a generic HTTP client function that can send HTTP requests and decode responses into any Go struct or value. The function takes a context, HTTP method, URL, request body, and a map of headers as inputs. It uses the default HTTP client provided by Go to make the HTTP request, with the request body and headers added to the request

func NewServer

func NewServer(params *NewServerParams) (*http.Server, error)

func NewTCPListener

func NewTCPListener(addr ListenAddr) (net.Listener, error)

func PathParamsDecoder

func PathParamsDecoder() transport.DecoderFunc

func PathParamsFromContext

func PathParamsFromContext(ctx context.Context) url.Values

PathParamsFromContext

func QueryParamsDecoder

func QueryParamsDecoder() transport.DecoderFunc

func StartServer

func StartServer(
	ctx context.Context,
	listener net.Listener,
	server *http.Server,
) error

StartServer starts the server in a non-blocking fashion It will return an error in the following cases 1. The server fails to start (port conflict) 2. The context is cancelled 3. The server takes longer than 5 seconds to start You must call server.Shutdown() to stop the server

func ValueDecoder

func ValueDecoder(tag string, get func(request transport.Request) ValueSet) transport.DecoderFunc

Types

type CloudflareMeta

type CloudflareMeta struct {
	Country      string
	TrueClientIP net.IP
}

func CloudflareMetaFromHeaders

func CloudflareMetaFromHeaders(header http.Header) (meta CloudflareMeta)

type Context

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

func (*Context) Codec

func (c *Context) Codec() transport.Codec

func (*Context) Request

func (c *Context) Request() transport.Request

func (*Context) Response

func (c *Context) Response() transport.Response

type DefaultJSONError

type DefaultJSONError struct {
	Message string `json:"message"`
	Status  int    `json:"status"`
}

func (DefaultJSONError) Error

func (d DefaultJSONError) Error() string

func (DefaultJSONError) GetStatusCode

func (d DefaultJSONError) GetStatusCode() int

func (DefaultJSONError) PrepareResponse

func (d DefaultJSONError) PrepareResponse() any

type ErrorResponse

type ErrorResponse struct {
	URL        *url.URL      `json:"url"`
	Method     string        `json:"method"`
	StatusCode int           `json:"status_code"`
	Body       *bytes.Buffer `json:"body"`
	Header     http.Header   `json:"header"`
	Err        error         `json:"-"`
}

func NewErrorResponse

func NewErrorResponse(res *http.Response) *ErrorResponse

NewErrorResponse attempts to read the response body after a request This is to improve visibility into HTTP request failures

func (*ErrorResponse) Error

func (r *ErrorResponse) Error() string

func (*ErrorResponse) Unwrap

func (r *ErrorResponse) Unwrap() error

func (*ErrorResponse) Wrap

func (r *ErrorResponse) Wrap(err error) error

type GinMux

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

func NewGinMux

func NewGinMux() *GinMux

func NewGinMuxWithMiddleware

func NewGinMuxWithMiddleware(middleware ...func(engine *gin.Engine) gin.HandlerFunc) *GinMux

func (GinMux) Handle

func (g GinMux) Handle(handler *Handler)

func (GinMux) ServeHTTP

func (g GinMux) ServeHTTP(writer http.ResponseWriter, request *http.Request)

type Handler

type Handler struct {
	Path    string
	Methods []string
	Handler transport.Handler
	Codec   transport.Codec

	// TODO: think about emitting errors at a higher level
	// Maybe we need a logger here
	OnError func(err error)
}

Handler is an HTTP adapter for transport.Handler

func NewHandler

func NewHandler(path string, handler transport.Handler) *Handler

func (*Handler) ServeHTTP

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

ServeHTTP implements http.Handler

func (*Handler) WithMethods

func (h *Handler) WithMethods(methods ...string) *Handler

type HandlerFactory

type HandlerFactory interface {
	HTTPHandlerFactory(*middleware.Registry) []*Handler
}

type HandlerFactoryFunc

type HandlerFactoryFunc func() []*Handler

func (HandlerFactoryFunc) HTTPHandlerFactory

func (h HandlerFactoryFunc) HTTPHandlerFactory() []*Handler

type JSONRequestParams

type JSONRequestParams struct {
	Method      string
	Client      *http.Client
	Url         *url.URL
	Body        any
	Headers     http.Header
	StatusCheck StatusCheckFunc
}

type ListenAddr

type ListenAddr string

type NewServerParams

type NewServerParams struct {
	Mux      ServeMux
	Handlers []*Handler
}

type Request

type Request struct {
	*http.Request
	// contains filtered or unexported fields
}

func NewRequest

func NewRequest(r *http.Request) *Request

func (*Request) Body

func (r *Request) Body() io.ReadCloser

func (*Request) BodyBuffer

func (r *Request) BodyBuffer() *bytes.Buffer

func (*Request) Headers

func (r *Request) Headers() http.Header

func (*Request) Method

func (r *Request) Method() string

func (*Request) ParseMediaType

func (r *Request) ParseMediaType() (mediatype string, params map[string]string, err error)

func (*Request) Path

func (r *Request) Path() string

func (*Request) PathParams

func (r *Request) PathParams() url.Values

func (*Request) QueryParams

func (r *Request) QueryParams() url.Values

func (*Request) URL

func (r *Request) URL() *url.URL

func (*Request) Underlying

func (r *Request) Underlying() any

func (*Request) Version

func (r *Request) Version() string

func (*Request) WithContext

func (r *Request) WithContext(ctx context.Context) transport.Request

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func NewResponse

func NewResponse(w http.ResponseWriter) *ResponseWriter

func (*ResponseWriter) BodyBuffer

func (r *ResponseWriter) BodyBuffer() *bytes.Buffer

func (*ResponseWriter) BytesWritten

func (r *ResponseWriter) BytesWritten() int64

func (*ResponseWriter) DelCookie

func (r *ResponseWriter) DelCookie(cookie http.Cookie) transport.Response

func (*ResponseWriter) DelCookieByName

func (r *ResponseWriter) DelCookieByName(name string) transport.Response

func (*ResponseWriter) GetStatusCode

func (r *ResponseWriter) GetStatusCode() int

func (*ResponseWriter) Headers

func (r *ResponseWriter) Headers() http.Header

func (*ResponseWriter) Redirect

func (r *ResponseWriter) Redirect(req transport.Request, url string, code int)

func (*ResponseWriter) SetCookie

func (r *ResponseWriter) SetCookie(cookie http.Cookie) transport.Response

func (*ResponseWriter) SetStatusCode

func (r *ResponseWriter) SetStatusCode(i int)

func (*ResponseWriter) Underlying

func (r *ResponseWriter) Underlying() any

func (*ResponseWriter) Write

func (r *ResponseWriter) Write(b []byte) (int, error)

type ServeMux

type ServeMux interface {
	http.Handler

	// Handle registers a Handler with its enclosed muxer
	// If a handler already exists for pattern it may panic.
	Handle(handler *Handler)
}

type StatusCheckFunc

type StatusCheckFunc func(status string, code int) error

type StdLibMux

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

func NewStdLibMux

func NewStdLibMux() *StdLibMux

func (StdLibMux) Handle

func (s StdLibMux) Handle(handler *Handler)

func (StdLibMux) ServeHTTP

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

type ValueSet

type ValueSet map[string][]string

Jump to

Keyboard shortcuts

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