123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package client
- import (
- "fmt"
- "net/url"
- "strconv"
- )
- const (
- generateTokenUrl = "/ds/api/admin/token/generate"
- deleteTokenUrl = "/ds/api/admin/token/delete"
- getTokensUrl = "/ds/api/admin/token/query"
- )
- func (c *Client) GenerateToken(token string, baseUrl string, name string) (string, error) {
- fullUrl, err := url.JoinPath(baseUrl, generateTokenUrl)
- if err != nil {
- return "", err
- }
- resp := new(struct {
- MsgResponse
- Token string `json:"token"`
- })
- err = c.post(token, fullUrl, map[string]any{
- "name": name,
- }, resp)
- if err != nil {
- return "", err
- }
- if !resp.Success {
- return "", fmt.Errorf(resp.Msg)
- }
- return resp.Token, nil
- }
- func (c *Client) DeleteToken(token string, baseUrl string, name string) error {
- fullUrl, err := url.JoinPath(baseUrl, deleteTokenUrl)
- if err != nil {
- return err
- }
- resp := new(MsgResponse)
- err = c.post(token, fullUrl, map[string]any{
- "name": name,
- }, resp)
- if err != nil {
- return err
- }
- if !resp.Success {
- return fmt.Errorf(resp.Msg)
- }
- return nil
- }
- func (c *Client) GetTokens(token string, baseUrl string, name string, pageNo int, pageSize int) ([]TokenInfo, error) {
- fullUrl, err := url.JoinPath(baseUrl, getTokensUrl)
- if err != nil {
- return nil, err
- }
- resp := new(InfosResponse[TokenInfo])
- err = c.get(token, fullUrl, map[string]string{
- "name": name,
- "pageNo": strconv.Itoa(pageNo),
- "pageSize": strconv.Itoa(pageSize),
- }, resp)
- if err != nil {
- return nil, err
- }
- if !resp.Success {
- return nil, fmt.Errorf(resp.Msg)
- }
- return resp.Infos, nil
- }
|