github.com/zooyer/miskit@v1.0.71/sdk/ice/ice.go (about)

     1  package ice
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"github.com/zooyer/miskit/log"
     7  	"github.com/zooyer/miskit/zrpc"
     8  	"github.com/zooyer/miskit/zuid"
     9  	"time"
    10  )
    11  
    12  type Option struct {
    13  	AppID     int64
    14  	AppSecret string
    15  	Addr      string
    16  	Retry     int
    17  	Logger    *log.Logger
    18  	Timeout   time.Duration
    19  }
    20  
    21  type Client struct {
    22  	url    string
    23  	option Option
    24  	client *zrpc.Client
    25  }
    26  
    27  type ID struct {
    28  	zuid.ID
    29  }
    30  
    31  func New(option Option) *Client {
    32  	return &Client{
    33  		url:    fmt.Sprintf("http://%s/ice/v1/idgen?app_id=%v&app_secret=%v", option.Addr, option.AppID, option.AppSecret),
    34  		option: option,
    35  		client: zrpc.New("ice", option.Retry, option.Timeout, option.Logger),
    36  	}
    37  }
    38  
    39  func (c *Client) GenID(ctx context.Context) (id ID, err error) {
    40  	var response struct {
    41  		ID int64 `json:"id"`
    42  	}
    43  
    44  	if _, _, err = c.client.Get(ctx, c.url, nil, &response); err != nil {
    45  		return
    46  	}
    47  
    48  	return ID{
    49  		ID: zuid.ID(response.ID),
    50  	}, nil
    51  }