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

     1  package ice
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/zooyer/jsons"
     9  	"github.com/zooyer/miskit/log"
    10  	"github.com/zooyer/miskit/zrpc"
    11  )
    12  
    13  type Option struct {
    14  	AppID     int64
    15  	AppSecret string
    16  	Addr      string
    17  	Retry     int
    18  	Logger    *log.Logger
    19  	Timeout   time.Duration
    20  }
    21  
    22  type Client struct {
    23  	url    string
    24  	option Option
    25  	client *zrpc.Client
    26  }
    27  
    28  func New(option Option) *Client {
    29  	return &Client{
    30  		url:    fmt.Sprintf("http://%s/upm/v1/auth", option.Addr),
    31  		option: option,
    32  		client: zrpc.New("upm", option.Retry, option.Timeout, option.Logger),
    33  	}
    34  }
    35  
    36  func (c *Client) Auth(ctx context.Context, user int64, cond map[string]interface{}) (auth bool, args jsons.Raw, err error) {
    37  	var resp struct {
    38  		Auth bool      `json:"auth"`
    39  		Args jsons.Raw `json:"args"`
    40  	}
    41  
    42  	for key, val := range cond {
    43  		if _, ok := val.(string); !ok {
    44  			cond[key] = fmt.Sprint(val)
    45  		}
    46  	}
    47  
    48  	var req = map[string]interface{}{
    49  		"app_id":     c.option.AppID,
    50  		"app_secret": c.option.AppSecret,
    51  		"user_id":    user,
    52  		"perm_cond":  cond,
    53  	}
    54  
    55  	if _, _, err = c.client.PostJSON(ctx, c.url, req, &resp); err != nil {
    56  		return
    57  	}
    58  
    59  	return resp.Auth, resp.Args, nil
    60  }