good morning!!!!

Skip to content
Snippets Groups Projects
exports.go 1.93 KiB
Newer Older
a's avatar
a committed
package jrpc

import (
	"context"

a's avatar
a committed
	"gfx.cafe/open/jrpc/contrib/codecs"
a's avatar
a committed
	"gfx.cafe/open/jrpc/pkg/codec"
	"gfx.cafe/open/jrpc/pkg/server"
)

// to make the repo cleaner, we export everything here. this way the packages dont need to ever import this

type (
a's avatar
a committed
	// Conn is used to make requests to jsonrpc2 servers
a's avatar
a committed
	Conn = codec.Conn
a's avatar
a committed
	// Handler is the equivilant of http.Handler, but for jsonrpc.
a's avatar
a committed
	Handler = codec.Handler
a's avatar
a committed
	// HandlerFunc is a Handler that exists as a function
a's avatar
a committed
	HandlerFunc = codec.HandlerFunc
a's avatar
a committed
	// ResponseWriter is used to write responses to the request
a's avatar
a committed
	ResponseWriter = codec.ResponseWriter
a's avatar
a committed
	// StreamingConn is a conn that supports streaming methods
a's avatar
a committed
	StreamingConn = codec.Conn
a's avatar
a committed
	// Request is the request object
a's avatar
a committed
	Request = codec.Request
a's avatar
a committed
)
type (
a's avatar
a committed
	// BatchElem is an element of a batch request
a's avatar
a committed
	BatchElem = codec.BatchElem
)

var (
a's avatar
a committed

	// DialContext is to dial a conn with context
	DialContext = codecs.DialContext
	// Dial is to dial a conn with context.Background()
	Dial = codecs.Dial

a's avatar
a committed
	// ContextWithConn will attach a conn to the context
	ContextWithConn = codec.ContextWithConn
	// ContextWithPeerInfo will attach a peerinfo to the context
a's avatar
a committed
	ContextWithPeerInfo = server.ContextWithPeerInfo

a's avatar
a committed
	// ConnFromContext will retrieve a conn from context
	ConnFromContext = codec.ConnFromContext
	// PeerInfoFromContext will retrieve a peerinfo from context
a's avatar
a committed
	PeerInfoFromContext = server.PeerInfoFromContext
)

a's avatar
a committed
// Do will use the conn to perform a jsonrpc2 call.
a's avatar
a committed
func Do[T any](ctx context.Context, c Conn, method string, args any) (*T, error) {
	return codec.Do[T](ctx, c, method, args)
}

a's avatar
a committed
// Call will use the conn to perform a jsonrpc2 call, except the args will be encoded as an array of arguments.
a's avatar
a committed
func Call[T any](ctx context.Context, c Conn, method string, args ...any) (*T, error) {
	return codec.Call[T](ctx, c, method, args...)
}

a's avatar
a committed
// CallInto is the same as Call, except instead of returning, you provide a pointer to the result
a's avatar
a committed
var CallInto = codec.CallInto