Skip to content

Commit

Permalink
Feature/fetch users (#210)
Browse files Browse the repository at this point in the history
* add fetch users

* Fix
  • Loading branch information
jadeydi authored Nov 14, 2024
1 parent 7a492b1 commit c98a88d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func main() {
Commands: []*cli.Command{
appMeCmdCli,
userCmdCli,
getUsersCmdCli,
transferCmdCli,
verifyPINCmdCli,
// batchTransferCmdCli,
Expand Down
54 changes: 54 additions & 0 deletions cli/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"context"
"encoding/json"
"log"
"os"
"strings"

"github.com/MixinNetwork/bot-api-go-client/v3"
"github.com/urfave/cli/v2"
)

var getUsersCmdCli = &cli.Command{
Name: "users",
Action: getUsersCmd,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "keystore,k",
Usage: "keystore download from https://developers.mixin.one/dashboard",
},
&cli.StringFlag{
Name: "users",
Usage: "user ids",
},
},
}

func getUsersCmd(ctx *cli.Context) error {
keystore := ctx.String("keystore")
idStr := ctx.String("users")

log.Println(keystore, idStr)
dat, err := os.ReadFile(keystore)
if err != nil {
panic(err)
}
log.Println(string(dat))
var su bot.SafeUser
err = json.Unmarshal([]byte(dat), &su)
if err != nil {
panic(err)
}

ids := strings.Split(idStr, ",")
users, err := bot.GetUsers(context.Background(), ids, &su)
if err != nil {
panic(err)
}
for _, user := range users {
log.Printf("%#v", user)
}
return nil
}
25 changes: 25 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ func GetUser(ctx context.Context, userId string, su *SafeUser) (*User, error) {
return resp.Data, nil
}

func GetUsers(ctx context.Context, userIds []string, su *SafeUser) ([]*User, error) {
url := "/users/fetch"
data, _ := json.Marshal(userIds)
token, err := SignAuthenticationToken("POST", url, string(data), su)
if err != nil {
return nil, err
}
body, err := Request(ctx, "POST", url, data, token)
if err != nil {
return nil, ServerError(ctx, err)
}
var resp struct {
Data []*User `json:"data"`
Error Error `json:"error"`
}
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, BadDataError(ctx)
}
if resp.Error.Code > 0 {
return nil, resp.Error
}
return resp.Data, nil
}

func SearchUser(ctx context.Context, mixinId string, su *SafeUser) (*User, error) {
url := fmt.Sprintf("/search/%s", mixinId)
token, err := SignAuthenticationToken("GET", url, "", su)
Expand Down

0 comments on commit c98a88d

Please sign in to comment.