rest

package
v0.20.2 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentTypeJSON    = "application/json"
	ContentTypeMsgPack = "application/msgpack"
)

Variables

This section is empty.

Functions

func DecodeRequestBody

func DecodeRequestBody(w http.ResponseWriter, r *http.Request, v interface{}) error

DecodeRequestBody decodes JSON or Msgpack data from the request body into a struct

func StreamData

func StreamData[P Pointer[T], T any](
	c *RESTClient,
	ctx context.Context,
	method string,
	path string,
	request interface{},
	fn func(P) (bool, error),
) error

Thin generic wrapper - only this small function is duplicated per type

func WriteResponse

func WriteResponse(status int, w http.ResponseWriter, r *http.Request, v interface{}) error

WriteResponse encodes and writes a JSON or Msgpack response

Types

type Pointer

type Pointer[T any] interface {
	*T
}

Define a constraint for pointer types

type RESTClient

type RESTClient struct {
	HTTPClient *http.Client
	// contains filtered or unexported fields
}

func NewClient

func NewClient(baseURL string, token string, insecureSkipVerify bool) (*RESTClient, error)

func (*RESTClient) AppendUserAgent

func (c *RESTClient) AppendUserAgent(userAgent string) *RESTClient

func (*RESTClient) ClearHeaders

func (c *RESTClient) ClearHeaders() *RESTClient

func (*RESTClient) Close

func (c *RESTClient) Close()

func (*RESTClient) Delete

func (c *RESTClient) Delete(ctx context.Context, path string, request interface{}, response interface{}, successCode int) (int, error)

func (*RESTClient) DeleteHeader

func (c *RESTClient) DeleteHeader(key string) *RESTClient

func (*RESTClient) Get

func (c *RESTClient) Get(ctx context.Context, path string, response interface{}) (int, error)

func (*RESTClient) Post

func (c *RESTClient) Post(ctx context.Context, path string, request interface{}, response interface{}, successCode int) (int, error)

func (*RESTClient) Put

func (c *RESTClient) Put(ctx context.Context, path string, request interface{}, response interface{}, successCode int) (int, error)

func (*RESTClient) SendData

func (c *RESTClient) SendData(ctx context.Context, method string, path string, request interface{}, response interface{}, successCode int) (int, error)

func (*RESTClient) SetAccept added in v0.19.0

func (c *RESTClient) SetAccept(accept ...string) *RESTClient

func (*RESTClient) SetAuthToken

func (c *RESTClient) SetAuthToken(token string) *RESTClient

func (*RESTClient) SetBaseUrl

func (c *RESTClient) SetBaseUrl(baseURL string) (*RESTClient, error)

func (*RESTClient) SetContentType

func (c *RESTClient) SetContentType(contentType string) *RESTClient

func (*RESTClient) SetHeader

func (c *RESTClient) SetHeader(key, value string) *RESTClient

func (*RESTClient) SetTimeout

func (c *RESTClient) SetTimeout(timeout time.Duration) *RESTClient

func (*RESTClient) SetTokenFormat

func (c *RESTClient) SetTokenFormat(format string) *RESTClient

func (*RESTClient) SetTokenKey

func (c *RESTClient) SetTokenKey(key string) *RESTClient

func (*RESTClient) SetUserAgent

func (c *RESTClient) SetUserAgent(userAgent string) *RESTClient

type StreamResponseFunc

type StreamResponseFunc func(chunk interface{}) (isDone bool, err error)

type StreamWriter

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

StreamWriter writes chunks using Server-Sent Events

func NewStreamWriter added in v0.19.0

func NewStreamWriter(w http.ResponseWriter, r *http.Request) *StreamWriter

NewStreamWriter creates a new stream writer

func (*StreamWriter) Close

func (sw *StreamWriter) Close() error

Close closes the stream writer

func (*StreamWriter) WriteChunk

func (sw *StreamWriter) WriteChunk(chunk interface{}) error

WriteChunk writes a single chunk as SSE

func (*StreamWriter) WriteEnd

func (sw *StreamWriter) WriteEnd() error

WriteEnd signals the end of the stream

Jump to

Keyboard shortcuts

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