http

package
v0.0.97 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrCodeInvalidRequest     = 400
	ErrCodeInvalidCredentials = 401
	ErrCodeAuthorized         = 403
	ErrCodeEntityNotFound     = 404
)

Variables

View Source
var DefaultHttpClient *http.Client
View Source
var HighQPSHttpClient *http.Client
View Source
var LowQPSHttpClient *http.Client
View Source
var MediumQPSHttpClient *http.Client

Functions

func Call

func Call(req *http.Request, client *http.Client) (response interface{}, err error)

func ErrorToHttpCode

func ErrorToHttpCode(err error) int

func HTTPErrorCode

func HTTPErrorCode(err error) int

func MakeUrl

func MakeUrl(host, path string, args string) (url string)

func NewBytesRequest

func NewBytesRequest(method string, endpoint string, body []byte) (req *http.Request, err error)

func NewJsonRequest

func NewJsonRequest(method string, endpoint string, body map[string]any) (req *http.Request, err error)

func NewRequest

func NewRequest(method string, endpoint string, bodyReader io.Reader) (req *http.Request, err error)

func NormalizeWsUrl added in v0.0.90

func NormalizeWsUrl(httpOrWsUrl string) string

func SendJsonResponse

func SendJsonResponse(writer http.ResponseWriter, resp interface{}, err error)

func WSConnJSONReaderWriter

func WSConnJSONReaderWriter(conn *websocket.Conn) (reader *conc.Reader[gut.StringMap], writer *conc.Writer[conc.Message[gut.StringMap]])

func WSConnWriteError

func WSConnWriteError(wsConn *websocket.Conn, err error) error

func WSConnWriteMessage

func WSConnWriteMessage(wsConn *websocket.Conn, msg interface{}) error

func WSHandleConn

func WSHandleConn[I any, S WSConn[I]](conn *websocket.Conn, ctx S, config *WSConnConfig)

func WSServe

func WSServe[I any, S WSConn[I]](h WSHandler[I, S], config *WSConnConfig) http.HandlerFunc

Types

type BiDirStreamConfig

type BiDirStreamConfig struct {
	PingPeriod time.Duration
	PongPeriod time.Duration
}

func DefaultBiDirStreamConfig

func DefaultBiDirStreamConfig() *BiDirStreamConfig

type BiDirStreamConn

type BiDirStreamConn[I any] interface {
	/**
	 * Called to send the next ping message.
	 */
	SendPing() error

	// Optional Name of the connection
	Name() string

	// Optional connection id
	ConnId() string

	/**
	 * Called to handle the next message from the input stream on the ws conn.
	 */
	HandleMessage(msg I) error

	/**
	 * Called to handle or suppress an error
	 */
	OnError(err error) error

	/**
	 * Called when the connection closes.
	 */
	OnClose()
	OnTimeout() bool
}

type HTTPError

type HTTPError struct {
	Code    int
	Message string
}

func (*HTTPError) Error

func (t *HTTPError) Error() string

type JSONConn

type JSONConn struct {
	Writer    *conc.Writer[conc.Message[any]]
	NameStr   string
	ConnIdStr string
	PingId    int64
}

func (*JSONConn) ConnId

func (j *JSONConn) ConnId() string

func (*JSONConn) DebugInfo

func (j *JSONConn) DebugInfo() any

func (*JSONConn) HandleMessage

func (j *JSONConn) HandleMessage(msg any) error

*

  • Called to handle the next message from the input stream on the ws conn.

func (*JSONConn) Name

func (j *JSONConn) Name() string

func (*JSONConn) OnClose

func (j *JSONConn) OnClose()

*

  • Called when the connection closes.

func (*JSONConn) OnError

func (j *JSONConn) OnError(err error) error

func (*JSONConn) OnStart

func (j *JSONConn) OnStart(conn *websocket.Conn) error

func (*JSONConn) OnTimeout

func (j *JSONConn) OnTimeout() bool

func (*JSONConn) ReadMessage

func (j *JSONConn) ReadMessage(conn *websocket.Conn) (out any, err error)

*

  • Reads the next message from the ws conn.

func (*JSONConn) SendPing

func (j *JSONConn) SendPing() error

*

  • Called to send the next ping message.

type JSONHandler

type JSONHandler struct {
}

func (*JSONHandler) Validate

func (j *JSONHandler) Validate(w http.ResponseWriter, r *http.Request) (out WSConn[any], isValid bool)

type RequestVarMap added in v0.0.88

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

func (*RequestVarMap) GetKey added in v0.0.88

func (r *RequestVarMap) GetKey(req *http.Request, key string) (value any)

func (*RequestVarMap) SetKey added in v0.0.88

func (r *RequestVarMap) SetKey(req *http.Request, key string, value any)

type URLWaiter added in v0.0.90

type URLWaiter struct {
	Method             string
	Url                string
	Headers            map[string]string
	Payload            map[string]any
	DelayBetweenChecks time.Duration

	// Func versions of above so we can do something dynamcially on each iteration
	RequestFunc  func(iter int, prevError error) (*http.Request, error)
	ValidateFunc func(req *http.Request, resp *http.Response) error
}

func (*URLWaiter) Run added in v0.0.90

func (u *URLWaiter) Run() (success bool, iter int, err error)

type WSConn

type WSConn[I any] interface {
	BiDirStreamConn[I]
	/**
	 * Reads the next message from the ws conn.
	 */
	ReadMessage(w *websocket.Conn) (I, error)
	/**
	 * On created.
	 */
	OnStart(conn *websocket.Conn) error
}

type WSConnConfig

type WSConnConfig struct {
	*BiDirStreamConfig
	Upgrader websocket.Upgrader
}

func DefaultWSConnConfig

func DefaultWSConnConfig() *WSConnConfig

type WSHandler

type WSHandler[I any, S WSConn[I]] interface {
	/**
	 * Called to validate an http request to see if it is upgradeable to a ws conn
	 */
	Validate(w http.ResponseWriter, r *http.Request) (S, bool)
}

Jump to

Keyboard shortcuts

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