good morning!!!!

Skip to content
Snippets Groups Projects
exports.go 2.17 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

a's avatar
a committed
// Conn is used to make requests to jsonrpc2 servers
type Conn interface {
	codec.Conn
}

// Handler is the equivalent of http.Handler, but for jsonrpc.
type Handler interface {
	codec.Handler
}

// StreamingConn is a conn that supports streaming methods
type StreamingConn interface {
	codec.StreamingConn
}

// ResponseWriter is used to write responses to the request
a's avatar
a committed
type ResponseWriter = codec.ResponseWriter
a's avatar
a committed

a's avatar
a committed
type (
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
	// Request is the request object
a's avatar
a committed
	Request = codec.Request
a's avatar
ok  
a committed
	// Server is a jrpc server
	Server = server.Server
a's avatar
a committed
	// Middleware is a middleware
	Middleware = func(Handler) Handler
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

a's avatar
ok  
a committed
	// NewServer creates a jrpc server
	NewServer = server.NewServer

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