good morning!!!!

Skip to content
Snippets Groups Projects
exports.go 1.78 KiB
Newer Older
  • Learn to ignore specific revisions
  • 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/jsonrpc"
    
    a's avatar
    a committed
    	"gfx.cafe/open/jrpc/pkg/server"
    )
    
    
    a's avatar
    a committed
    // Conn is used to make requests to jsonrpc2 servers
    
    a's avatar
    a  
    a committed
    type Conn = jsonrpc.Conn
    
    a's avatar
    a committed
    
    
    a's avatar
    a committed
    // Handler is the equivalent of http.Handler, but for jsonrpc.
    
    a's avatar
    a  
    a committed
    type Handler = jsonrpc.Handler
    
    a's avatar
    a committed
    
    
    a's avatar
    a committed
    // ResponseWriter is used to write responses to the request
    
    a's avatar
    a  
    a committed
    type ResponseWriter = jsonrpc.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 = jsonrpc.HandlerFunc
    
    a's avatar
    a committed
    	// Request is the request object
    
    a's avatar
    a committed
    	Request = jsonrpc.Request
    
    a's avatar
    ok  
    a committed
    	// Server is a jrpc server
    
    a's avatar
    a committed
    	// Middleware is a middleware
    	Middleware = func(Handler) Handler
    
    a's avatar
    a committed
    )
    
    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
    
    a's avatar
    a committed
    	ContextWithConn = jsonrpc.ContextWithConn
    
    a's avatar
    a committed
    	// 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
    
    a's avatar
    a committed
    	ConnFromContext = jsonrpc.ConnFromContext
    
    a's avatar
    a committed
    	// 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) {
    
    a's avatar
    a committed
    	return jsonrpc.Do[T](ctx, c, method, args)
    
    a's avatar
    a committed
    }
    
    
    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) {
    
    a's avatar
    a committed
    	return jsonrpc.Call[T](ctx, c, method, args...)
    
    a's avatar
    a committed
    }
    
    
    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 = jsonrpc.CallInto