github.com/decred/politeia@v1.4.0/politeiad/client/user.go (about) 1 // Copyright (c) 2020-2021 The Decred developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package client 6 7 import ( 8 "context" 9 "encoding/json" 10 "fmt" 11 12 pdv2 "github.com/decred/politeia/politeiad/api/v2" 13 "github.com/decred/politeia/politeiad/plugins/usermd" 14 ) 15 16 // Author sends the user plugin Author command to the politeiad v2 API. 17 func (c *Client) Author(ctx context.Context, token string) (string, error) { 18 // Setup request 19 cmds := []pdv2.PluginCmd{ 20 { 21 Token: token, 22 ID: usermd.PluginID, 23 Command: usermd.CmdAuthor, 24 Payload: "", 25 }, 26 } 27 28 // Send request 29 replies, err := c.PluginReads(ctx, cmds) 30 if err != nil { 31 return "", err 32 } 33 if len(replies) == 0 { 34 return "", fmt.Errorf("no replies found") 35 } 36 pcr := replies[0] 37 err = extractPluginCmdError(pcr) 38 if err != nil { 39 return "", err 40 } 41 42 // Decode reply 43 var ar usermd.AuthorReply 44 err = json.Unmarshal([]byte(pcr.Payload), &ar) 45 if err != nil { 46 return "", err 47 } 48 49 return ar.UserID, nil 50 } 51 52 // UserRecords sends the user plugin UserRecords command to the politeiad v2 53 // API. 54 func (c *Client) UserRecords(ctx context.Context, userID string) (*usermd.UserRecordsReply, error) { 55 // Setup request 56 ur := usermd.UserRecords{ 57 UserID: userID, 58 } 59 b, err := json.Marshal(ur) 60 if err != nil { 61 return nil, err 62 } 63 cmds := []pdv2.PluginCmd{ 64 { 65 ID: usermd.PluginID, 66 Command: usermd.CmdUserRecords, 67 Payload: string(b), 68 }, 69 } 70 71 // Send request 72 replies, err := c.PluginReads(ctx, cmds) 73 if err != nil { 74 return nil, err 75 } 76 if len(replies) == 0 { 77 return nil, fmt.Errorf("no replies found") 78 } 79 pcr := replies[0] 80 err = extractPluginCmdError(pcr) 81 if err != nil { 82 return nil, err 83 } 84 85 // Decode reply 86 var urr usermd.UserRecordsReply 87 err = json.Unmarshal([]byte(pcr.Payload), &urr) 88 if err != nil { 89 return nil, err 90 } 91 92 return &urr, nil 93 }