stub

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2022 License: BSD-3-Clause Imports: 16 Imported by: 2

README

STUB

This package provides a set of utilities and helper methods to call a REST or RPC (over websocket) By using this package many of the boilerplate codes are not necessary anymore.

REST example:

    package main

import (
	"context"
	"net/http"

	"github.com/clubpay/ronykit/stub"
)

func main() {
	ctx := context.Background()
	s := stub.New("webhook.site", stub.Secure())
	httpCtx := s.REST().
		SetMethod(http.MethodGet).
		SetPath("/22fda9e7-1660-406e-b11e-993e070f175e").
		SetQuery("someKey", "someValue").
		Run(ctx)
	defer httpCtx.Release()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error added in v0.6.2

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

func NewError added in v0.6.2

func NewError(code int, item string) *Error

func NewErrorWithMsg added in v0.6.3

func NewErrorWithMsg(msg ronykit.Message) *Error

func WrapError added in v0.6.2

func WrapError(err error) *Error

func (Error) Code added in v0.6.4

func (err Error) Code() int

func (Error) Error added in v0.6.2

func (err Error) Error() string

func (Error) Is added in v0.6.4

func (err Error) Is(target error) bool

func (Error) Item added in v0.6.4

func (err Error) Item() string

func (Error) Msg added in v0.6.3

func (err Error) Msg() ronykit.Message

func (Error) Unwrap added in v0.6.4

func (err Error) Unwrap() error
type Header map[string]string

type Option

type Option func(cfg *config)

func DumpTo added in v0.6.2

func DumpTo(w io.Writer) Option

func Name added in v0.6.5

func Name(name string) Option

func Secure

func Secure() Option

func SkipTLSVerify

func SkipTLSVerify() Option

func WithDialTimeout added in v0.7.0

func WithDialTimeout(timeout time.Duration) Option

func WithReadTimeout

func WithReadTimeout(timeout time.Duration) Option

func WithWriteTimeout

func WithWriteTimeout(timeout time.Duration) Option

type RESTCtx added in v0.7.0

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

func (*RESTCtx) AutoRun added in v0.7.0

func (hc *RESTCtx) AutoRun(
	ctx context.Context, route string, enc ronykit.Encoding, m ronykit.Message,
	opt ...RESTOption,
) *RESTCtx

AutoRun is a helper method, which fills the request based on the input arguments. It checks the route which is a path pattern, and fills the dynamic url params based on the `m`'s `tag` keys. Example:

type Request struct {
		ID int64 `json:"id"`
		Name string `json:"name"`
}

AutoRun(

	context.Background(),
  "/something/:id/:name",
  ronykit.JSON,
  &Request{ID: 10, Name: "customName"},

)

Is equivalent to:

SetPath("/something/10/customName"). Run(context.Background())

func (*RESTCtx) CopyBody added in v0.7.0

func (hc *RESTCtx) CopyBody(dst []byte) []byte

CopyBody copies the body to `dst`. It creates a new slice and returns it if dst is nil.

func (*RESTCtx) DefaultResponseHandler added in v0.7.0

func (hc *RESTCtx) DefaultResponseHandler(h RESTResponseHandler) *RESTCtx

func (*RESTCtx) DumpRequest added in v0.7.0

func (hc *RESTCtx) DumpRequest() string

func (*RESTCtx) DumpRequestTo added in v0.7.0

func (hc *RESTCtx) DumpRequestTo(w io.Writer) *RESTCtx

DumpRequestTo accepts a writer and will write the request dump to it when Run is executed.

Please refer to DumpResponseTo

func (*RESTCtx) DumpResponse added in v0.7.0

func (hc *RESTCtx) DumpResponse() string

func (*RESTCtx) DumpResponseTo added in v0.7.0

func (hc *RESTCtx) DumpResponseTo(w io.Writer) *RESTCtx

DumpResponseTo accepts a writer and will write the response dump to it when Run is executed. Example:

httpCtx := s.REST().
							DumpRequestTo(os.Stdout).
							DumpResponseTo(os.Stdout).
							GET("https//google.com").
							Run(ctx)
defer httpCtx.Release()

**YOU MUST NOT USE httpCtx after httpCtx.Release() is called.**

func (*RESTCtx) Err added in v0.7.0

func (hc *RESTCtx) Err() *Error

Err returns the error if any occurred during the execution.

func (*RESTCtx) Error added in v0.7.0

func (hc *RESTCtx) Error() error

Error returns the error if any occurred during the execution.

func (*RESTCtx) GET added in v0.7.0

func (hc *RESTCtx) GET(path string) *RESTCtx

func (*RESTCtx) GetBody added in v0.7.0

func (hc *RESTCtx) GetBody() []byte

GetBody returns the body, but please note that the returned slice is only valid until Release is called. If you need to use the body after releasing RESTCtx then use CopyBody method.

func (*RESTCtx) GetHeader added in v0.7.0

func (hc *RESTCtx) GetHeader(key string) string

GetHeader returns the header value for key in the response

func (*RESTCtx) OPTIONS added in v0.7.0

func (hc *RESTCtx) OPTIONS(path string) *RESTCtx

func (*RESTCtx) PATCH added in v0.7.0

func (hc *RESTCtx) PATCH(path string) *RESTCtx

func (*RESTCtx) POST added in v0.7.0

func (hc *RESTCtx) POST(path string) *RESTCtx

func (*RESTCtx) PUT added in v0.7.0

func (hc *RESTCtx) PUT(path string) *RESTCtx

func (*RESTCtx) Release added in v0.7.0

func (hc *RESTCtx) Release()

Release frees the allocates internal resources to be re-used. You MUST NOT refer to any method of this object after calling this method, if you call any method after Release has been called, the result is unpredictable.

func (*RESTCtx) Run added in v0.7.0

func (hc *RESTCtx) Run(ctx context.Context, opt ...RESTOption) *RESTCtx

func (*RESTCtx) SetBody added in v0.7.0

func (hc *RESTCtx) SetBody(body []byte) *RESTCtx

func (*RESTCtx) SetHeader added in v0.7.0

func (hc *RESTCtx) SetHeader(key, value string) *RESTCtx

func (*RESTCtx) SetMethod added in v0.7.0

func (hc *RESTCtx) SetMethod(method string) *RESTCtx

func (*RESTCtx) SetPath added in v0.7.0

func (hc *RESTCtx) SetPath(path string) *RESTCtx

func (*RESTCtx) SetQuery added in v0.7.0

func (hc *RESTCtx) SetQuery(key, value string) *RESTCtx

func (*RESTCtx) SetResponseHandler added in v0.7.0

func (hc *RESTCtx) SetResponseHandler(statusCode int, h RESTResponseHandler) *RESTCtx

func (*RESTCtx) StatusCode added in v0.7.0

func (hc *RESTCtx) StatusCode() int

StatusCode returns the status code of the response

type RESTOption added in v0.6.10

type RESTOption func(cfg *restConfig)

func WithPreflightREST added in v0.6.10

func WithPreflightREST(h ...RESTPreflightHandler) RESTOption

WithPreflightREST register one or many handlers to run in sequence before actually making requests.

type RESTPreflightHandler added in v0.6.10

type RESTPreflightHandler func(r *fasthttp.Request)

type RESTResponse

type RESTResponse interface {
	StatusCode() int
	GetBody() []byte
	GetHeader(key string) string
}

type RESTResponseHandler

type RESTResponseHandler func(ctx context.Context, r RESTResponse) *Error

type RPCContainerHandler added in v0.7.0

type RPCContainerHandler func(c ronykit.IncomingRPCContainer)

type RPCMessageHandler added in v0.7.0

type RPCMessageHandler func(ctx context.Context, msg ronykit.Message, hdr Header, err error)

type Stub

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

func New

func New(hostPort string, opts ...Option) *Stub

func (*Stub) REST

func (s *Stub) REST(opt ...RESTOption) *RESTCtx

func (*Stub) Websocket added in v0.7.0

func (s *Stub) Websocket(opts ...WebsocketOption) *WebsocketCtx

type WebsocketCtx added in v0.7.0

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

func (*WebsocketCtx) BinaryMessage added in v0.7.0

func (wCtx *WebsocketCtx) BinaryMessage(
	ctx context.Context, predicate string, req, res ronykit.Message,
	cb RPCMessageHandler,
) error

func (*WebsocketCtx) Connect added in v0.7.0

func (wCtx *WebsocketCtx) Connect(ctx context.Context, path string) error

func (*WebsocketCtx) Disconnect added in v0.7.0

func (wCtx *WebsocketCtx) Disconnect() error

func (*WebsocketCtx) TextMessage added in v0.7.0

func (wCtx *WebsocketCtx) TextMessage(
	ctx context.Context, predicate string, req, res ronykit.Message,
	cb RPCMessageHandler,
) error

type WebsocketOption added in v0.7.0

type WebsocketOption func(cfg *wsConfig)

func WithCustomDialer added in v0.7.0

func WithCustomDialer(d *websocket.Dialer) WebsocketOption

func WithCustomRPC added in v0.7.0

func WithDefaultHandler added in v0.7.0

func WithDefaultHandler(h RPCContainerHandler) WebsocketOption

func WithHandler added in v0.7.0

func WithHandler(predicate string, h RPCContainerHandler) WebsocketOption

func WithPredicateKey added in v0.7.0

func WithPredicateKey(key string) WebsocketOption

func WithUpgradeHeader added in v0.7.0

func WithUpgradeHeader(key string, values ...string) WebsocketOption

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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