github.com/lastbackend/toolkit@v0.0.0-20241020043710-cafa37b95aad/pkg/client/client.go (about)

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"github.com/lastbackend/toolkit/pkg/client/grpc/resolver"
     6  	"google.golang.org/grpc"
     7  	"time"
     8  )
     9  
    10  type GRPCStream grpc.ClientStream
    11  type GRPCClient interface {
    12  	GetResolver() resolver.Resolver
    13  	SetResolver(resolver resolver.Resolver)
    14  	Conn(pool string) (grpc.ClientConnInterface, error)
    15  	Call(ctx context.Context, service, method string, req, rsp interface{}, opts ...GRPCCallOption) error
    16  	Stream(ctx context.Context, service, method string, body interface{}, opts ...GRPCCallOption) (grpc.ClientStream, error)
    17  }
    18  
    19  type GRPCCallOption func(*GRPCCallOptions)
    20  
    21  type GRPCCallOptions struct {
    22  	Backoff               GRPCBackoffFunc
    23  	Retries               time.Duration
    24  	RequestTimeout        time.Duration
    25  	Context               context.Context
    26  	Headers               map[string]string
    27  	MaxCallSendMsgSize    int
    28  	MaxCallRecvMsgSize    int
    29  	MaxRetryRPCBufferSize int
    30  	CallContentSubtype    string
    31  }
    32  
    33  func GRPCOptionHeaders(h map[string]string) GRPCCallOption {
    34  	return func(o *GRPCCallOptions) {
    35  		o.Headers = h
    36  	}
    37  }
    38  
    39  func GRPCOptionMaxCallSendMsgSize(bytes int) GRPCCallOption {
    40  	return func(o *GRPCCallOptions) {
    41  		o.MaxCallSendMsgSize = bytes
    42  	}
    43  }
    44  
    45  func GRPCOptionMaxCallRecvMsgSize(bytes int) GRPCCallOption {
    46  	return func(o *GRPCCallOptions) {
    47  		o.MaxCallRecvMsgSize = bytes
    48  	}
    49  }
    50  
    51  func GRPCOptionRequestTimeout(timeout time.Duration) GRPCCallOption {
    52  	return func(o *GRPCCallOptions) {
    53  		o.RequestTimeout = timeout
    54  	}
    55  }
    56  
    57  type GRPCBackoffFunc func(ctx context.Context, req *GRPCRequest, attempts int) (time.Duration, error)
    58  type GRPCRetryFunc func(ctx context.Context, req *GRPCRequest, retryCount int, err error) (bool, error)
    59  
    60  type GRPCRequest struct {
    61  	service string
    62  	method  string
    63  	headers map[string]string
    64  	body    interface{}
    65  }
    66  
    67  func NewGRPCRequest(method, service string, body interface{}, headers map[string]string) *GRPCRequest {
    68  	r := new(GRPCRequest)
    69  	r.service = method
    70  	r.method = service
    71  	r.body = body
    72  	if headers == nil {
    73  		headers = make(map[string]string, 0)
    74  	}
    75  	r.headers = headers
    76  	return r
    77  }
    78  
    79  func (r *GRPCRequest) Service() string {
    80  	return r.service
    81  }
    82  
    83  func (r *GRPCRequest) Method() string {
    84  	return r.method
    85  }
    86  
    87  func (r *GRPCRequest) Body() interface{} {
    88  	return r.body
    89  }
    90  
    91  func (r *GRPCRequest) Headers() map[string]string {
    92  	return r.headers
    93  }
    94  
    95  type HTTPClient interface {
    96  	Get() error
    97  	Post() error
    98  }