rest

package
v0.23.2 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2026 License: Apache-2.0 Imports: 17 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 NewMuxHTTPClient added in v0.23.0

func NewMuxHTTPClient(user *model.User) *http.Client

NewMuxHTTPClient creates an *http.Client that routes requests through the API mux with the given user injected into context for authentication. This allows standard HTTP clients (like mcpopenai.Client) to make in-process API calls without needing a valid auth token.

func SetAPIMux added in v0.23.0

func SetAPIMux(mux *http.ServeMux)

SetAPIMux stores the API mux for direct calls

func StreamData

func StreamData[P Pointer[T], T any](
	c *HTTPClient,
	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 HTTPClient added in v0.23.0

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

func NewClient

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

func (*HTTPClient) AppendUserAgent added in v0.23.0

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

func (*HTTPClient) ClearHeaders added in v0.23.0

func (c *HTTPClient) ClearHeaders() RESTClient

func (*HTTPClient) Close added in v0.23.0

func (c *HTTPClient) Close()

func (*HTTPClient) Delete added in v0.23.0

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

func (*HTTPClient) DeleteHeader added in v0.23.0

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

func (*HTTPClient) Get added in v0.23.0

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

func (*HTTPClient) GetAuthToken added in v0.23.0

func (c *HTTPClient) GetAuthToken() string

func (*HTTPClient) GetBaseURL added in v0.23.0

func (c *HTTPClient) GetBaseURL() string

func (*HTTPClient) GetJSON added in v0.23.0

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

GetJSON sends a GET request with JSON accept header (thread-safe)

func (*HTTPClient) Post added in v0.23.0

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

func (*HTTPClient) PostJSON added in v0.23.0

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

PostJSON sends a POST request with JSON content type and JSON accept header (thread-safe)

func (*HTTPClient) PostWithContentType added in v0.23.0

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

func (*HTTPClient) Put added in v0.23.0

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

func (*HTTPClient) PutJSON added in v0.23.0

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

PutJSON sends a PUT request with JSON content type and JSON accept header (thread-safe)

func (*HTTPClient) PutWithContentType added in v0.23.0

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

func (*HTTPClient) SendData added in v0.23.0

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

func (*HTTPClient) SendDataWithContentType added in v0.23.0

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

SendDataWithContentType sends a request with a specific content type (thread-safe)

func (*HTTPClient) SendDataWithContentTypeAndAccept added in v0.23.0

func (c *HTTPClient) SendDataWithContentTypeAndAccept(ctx context.Context, method string, path string, request interface{}, response interface{}, successCode int, contentType string, accept string) (int, error)

SendDataWithContentTypeAndAccept sends a request with specific content type and accept header (thread-safe)

func (*HTTPClient) SetAccept added in v0.23.0

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

func (*HTTPClient) SetAuthToken added in v0.23.0

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

func (*HTTPClient) SetBaseUrl added in v0.23.0

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

func (*HTTPClient) SetContentType added in v0.23.0

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

func (*HTTPClient) SetHeader added in v0.23.0

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

func (*HTTPClient) SetTimeout added in v0.23.0

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

func (*HTTPClient) SetTokenFormat added in v0.23.0

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

func (*HTTPClient) SetTokenKey added in v0.23.0

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

func (*HTTPClient) SetUserAgent added in v0.23.0

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

type MuxClient added in v0.23.0

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

MuxClient calls API handlers directly via mux without HTTP overhead

func (*MuxClient) AppendUserAgent added in v0.23.0

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

func (*MuxClient) ClearHeaders added in v0.23.0

func (c *MuxClient) ClearHeaders() RESTClient

func (*MuxClient) Delete added in v0.23.0

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

func (*MuxClient) DeleteHeader added in v0.23.0

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

func (*MuxClient) Get added in v0.23.0

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

func (*MuxClient) GetAuthToken added in v0.23.0

func (c *MuxClient) GetAuthToken() string

func (*MuxClient) GetBaseURL added in v0.23.0

func (c *MuxClient) GetBaseURL() string

func (*MuxClient) GetJSON added in v0.23.0

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

func (*MuxClient) Post added in v0.23.0

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

func (*MuxClient) PostJSON added in v0.23.0

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

func (*MuxClient) Put added in v0.23.0

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

func (*MuxClient) PutJSON added in v0.23.0

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

func (*MuxClient) RoundTrip added in v0.23.0

func (c *MuxClient) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper by routing the request through the API mux with the user injected into context for authentication.

func (*MuxClient) SetAccept added in v0.23.0

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

func (*MuxClient) SetAuthToken added in v0.23.0

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

func (*MuxClient) SetBaseUrl added in v0.23.0

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

func (*MuxClient) SetContentType added in v0.23.0

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

func (*MuxClient) SetHeader added in v0.23.0

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

func (*MuxClient) SetTimeout added in v0.23.0

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

func (*MuxClient) SetTokenFormat added in v0.23.0

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

func (*MuxClient) SetTokenKey added in v0.23.0

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

func (*MuxClient) SetUserAgent added in v0.23.0

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

type Pointer

type Pointer[T any] interface {
	*T
}

Define a constraint for pointer types

type RESTClient

type RESTClient interface {
	Get(ctx context.Context, path string, response interface{}) (int, error)
	Post(ctx context.Context, path string, request interface{}, response interface{}, successCode int) (int, error)
	Put(ctx context.Context, path string, request interface{}, response interface{}, successCode int) (int, error)
	Delete(ctx context.Context, path string, request interface{}, response interface{}, successCode int) (int, error)
	PostJSON(ctx context.Context, path string, request interface{}, response interface{}, successCode int) (int, error)
	PutJSON(ctx context.Context, path string, request interface{}, response interface{}, successCode int) (int, error)
	GetJSON(ctx context.Context, path string, response interface{}) (int, error)
	SetTimeout(timeout time.Duration) RESTClient
	SetContentType(contentType string) RESTClient
	SetAccept(accept ...string) RESTClient
	SetUserAgent(userAgent string) RESTClient
	AppendUserAgent(userAgent string) RESTClient
	SetAuthToken(token string) RESTClient
	SetTokenKey(key string) RESTClient
	SetTokenFormat(format string) RESTClient
	SetHeader(key, value string) RESTClient
	DeleteHeader(key string) RESTClient
	ClearHeaders() RESTClient
	GetBaseURL() string
	GetAuthToken() string
	SetBaseUrl(baseURL string) (RESTClient, error)
}

RESTClient interface for both HTTP and Mux clients

func NewMuxClient added in v0.23.0

func NewMuxClient(user *model.User) RESTClient

NewMuxClient creates a client that calls mux directly

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

func (*StreamWriter) WriteEvent added in v0.23.0

func (sw *StreamWriter) WriteEvent(eventType string, data any) error

WriteEvent writes an SSE comment event in the format ":eventType:jsonData\n\n" This is used for tool status notifications that standard OpenAI clients ignore

Jump to

Keyboard shortcuts

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