github.com/kaydxh/golang@v0.0.131/go/net/http/http_client.repository.factory.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package http
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  	"time"
    28  
    29  	"github.com/go-playground/validator/v10"
    30  	"google.golang.org/protobuf/proto"
    31  )
    32  
    33  type FactoryConfigFunc func(c *FactoryConfig) error
    34  
    35  type FactoryConfig struct {
    36  	Validator *validator.Validate
    37  	Url       string
    38  	Timeout   time.Duration //接口处理超时时间
    39  	*Client
    40  	RetryTimes    int
    41  	RetryInterval time.Duration
    42  }
    43  
    44  func (fc *FactoryConfig) ApplyOptions(configFuncs ...FactoryConfigFunc) error {
    45  
    46  	for _, f := range configFuncs {
    47  		err := f(fc)
    48  		if err != nil {
    49  			return fmt.Errorf("failed to apply factory config, err: %v", err)
    50  		}
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func (fc FactoryConfig) Validate() error {
    57  	valid := fc.Validator
    58  	if valid == nil {
    59  		valid = validator.New()
    60  	}
    61  	return valid.Struct(fc)
    62  }
    63  
    64  type ProtoMessage interface {
    65  	proto.Message
    66  }
    67  
    68  type Repository[REQ any, RESP any] struct {
    69  	Url     string
    70  	Timeout time.Duration
    71  
    72  	*Client
    73  
    74  	// not include the first call
    75  	RetryTimes    int
    76  	RetryInterval time.Duration
    77  }
    78  
    79  func newRepository[REQ any, RESP any](ctx context.Context, fc FactoryConfig) (*Repository[REQ, RESP], error) {
    80  	repo := &Repository[REQ, RESP]{
    81  		Url:           fc.Url,
    82  		Timeout:       fc.Timeout,
    83  		Client:        fc.Client,
    84  		RetryTimes:    fc.RetryTimes,
    85  		RetryInterval: fc.RetryInterval,
    86  	}
    87  
    88  	return repo, nil
    89  
    90  }
    91  
    92  type Factory[REQ any, RESP any] struct {
    93  	fc FactoryConfig
    94  }
    95  
    96  func NewFactory[REQ any, RESP any](fc FactoryConfig, configFuncs ...FactoryConfigFunc) (Factory[REQ, RESP], error) {
    97  	err := fc.ApplyOptions(configFuncs...)
    98  	if err != nil {
    99  		return Factory[REQ, RESP]{}, err
   100  	}
   101  
   102  	err = fc.Validate()
   103  	if err != nil {
   104  		return Factory[REQ, RESP]{}, err
   105  	}
   106  
   107  	return Factory[REQ, RESP]{fc: fc}, nil
   108  }
   109  
   110  func (f Factory[REQ, RESP]) NewClient(ctx context.Context) (*Repository[REQ, RESP], error) {
   111  	return newRepository[REQ, RESP](ctx, f.fc)
   112  }