github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/client/client.go (about)

     1  package client
     2  
     3  import (
     4  	"github.com/volts-dev/volts/internal/body"
     5  	"github.com/volts-dev/volts/internal/header"
     6  )
     7  
     8  type (
     9  	// Client is the interface used to make requests to services.
    10  	// It supports Request/Response via Transport and Publishing via the Broker.
    11  	// It also supports bidirectional streaming of requests.
    12  	IClient interface {
    13  		Init(...Option) error
    14  		Config() *Config
    15  		////NewMessage(topic string, msg interface{}, opts ...MessageOption) Message
    16  		///NewRequest(service, endpoint string, req interface{}, reqOpts ...RequestOption) Request
    17  		//Call(ctx context.Context, req Request, rsp interface{} ) error
    18  		//Call(ctx context.Context, request IRequest, opts ...CallOption) (IResponse, error)
    19  		//Stream(ctx context.Context, req Request, opts ...CallOption) (Stream, error)
    20  		//Publish(ctx context.Context, msg Message, opts ...PublishOption) error
    21  		//String() string
    22  		//NewRequest(service, method string, request interface{}, reqOpts ...RequestOption) IRequest
    23  	}
    24  
    25  	// Request is the interface for a synchronous request used by Call or Stream
    26  	IRequest interface {
    27  		// The service to call
    28  		Service() string
    29  		// The action to take
    30  		Method() string
    31  		// The endpoint to invoke
    32  		//Endpoint() string
    33  		// The content type
    34  		ContentType() string
    35  		Header() header.Header
    36  		// The unencoded request body
    37  		Body() *body.TBody
    38  		// Write to the encoded request writer. This is nil before a call is made
    39  		//Codec() codec.Writer
    40  		// indicates whether the request will be a streaming one rather than unary
    41  		Stream() bool
    42  	}
    43  
    44  	// Response is the response received from a service
    45  	IResponse interface {
    46  		Body() *body.TBody
    47  		// Read the response
    48  		//Codec() codec.Reader
    49  		// read the header
    50  		Header() header.Header
    51  		// Read the undecoded response
    52  		//Read(out interface{}) error
    53  	}
    54  )
    55  
    56  // Creates a new request using the default client. Content Type will
    57  // be set to the default within options and use the appropriate codec
    58  //func NewRequest(service, endpoint string, request interface{}, reqOpts ...RequestOption) IRequest {
    59  //	return DefaultClient.NewRequest(service, endpoint, request, reqOpts...)
    60  //}
    61  
    62  // Makes a synchronous call to a service using the default client
    63  ///func Call(ctx context.Context, request IRequest, opts ...CallOption) (IResponse, error) {
    64  ///	return defaultClient.Call(ctx, request, opts...)
    65  ///}
    66  
    67  // NewClient returns a new client
    68  func New(opts ...Option) IClient {
    69  	return NewRpcClient(opts...) //
    70  }
    71  
    72  func Default(opts ...Option) IClient {
    73  	if defaultClient == nil {
    74  		defaultClient = NewRpcClient(opts...)
    75  	} else {
    76  		defaultClient.Init(opts...)
    77  	}
    78  	return defaultClient
    79  }