github.com/decred/politeia@v1.4.0/politeiad/client/pi.go (about) 1 // Copyright (c) 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 11 pdv2 "github.com/decred/politeia/politeiad/api/v2" 12 "github.com/decred/politeia/politeiad/plugins/pi" 13 ) 14 15 // PiSetBillingStatus sends the pi plugin SetBillingStatus command to the 16 // politeiad v2 API. 17 func (c *Client) PiSetBillingStatus(ctx context.Context, sbs pi.SetBillingStatus) (*pi.SetBillingStatusReply, error) { 18 // Setup request 19 b, err := json.Marshal(sbs) 20 if err != nil { 21 return nil, err 22 } 23 cmd := pdv2.PluginCmd{ 24 Token: sbs.Token, 25 ID: pi.PluginID, 26 Command: pi.CmdSetBillingStatus, 27 Payload: string(b), 28 } 29 30 // Send request 31 reply, err := c.PluginWrite(ctx, cmd) 32 if err != nil { 33 return nil, err 34 } 35 36 // Decode reply 37 var sbsr pi.SetBillingStatusReply 38 err = json.Unmarshal([]byte(reply), &sbsr) 39 if err != nil { 40 return nil, err 41 } 42 43 return &sbsr, nil 44 } 45 46 // PiSummaries sends a page of pi plugin Summary commands to the politeiad 47 // v2 API. 48 func (c *Client) PiSummaries(ctx context.Context, tokens []string) (map[string]pi.SummaryReply, error) { 49 // Setup request 50 cmds := make([]pdv2.PluginCmd, 0, len(tokens)) 51 for _, t := range tokens { 52 cmds = append(cmds, pdv2.PluginCmd{ 53 Token: t, 54 ID: pi.PluginID, 55 Command: pi.CmdSummary, 56 Payload: "", 57 }) 58 } 59 60 // Send request 61 replies, err := c.PluginReads(ctx, cmds) 62 if err != nil { 63 return nil, err 64 } 65 66 // Prepare reply 67 ssr := make(map[string]pi.SummaryReply, len(replies)) 68 for _, v := range replies { 69 err = extractPluginCmdError(v) 70 if err != nil { 71 // Individual summary errors are ignored. The token will not 72 // be included in the returned summaries map. 73 continue 74 } 75 var sr pi.SummaryReply 76 err = json.Unmarshal([]byte(v.Payload), &sr) 77 if err != nil { 78 return nil, err 79 } 80 ssr[v.Token] = sr 81 } 82 83 return ssr, nil 84 } 85 86 // PiBillingStatusChanges sends a page of pi plugin BillingStatusChanges 87 // commands to the politeiad v2 API. 88 func (c *Client) PiBillingStatusChanges(ctx context.Context, tokens []string) (map[string]pi.BillingStatusChangesReply, error) { 89 // Setup request 90 cmds := make([]pdv2.PluginCmd, 0, len(tokens)) 91 for _, t := range tokens { 92 cmds = append(cmds, pdv2.PluginCmd{ 93 Token: t, 94 ID: pi.PluginID, 95 Command: pi.CmdBillingStatusChanges, 96 Payload: "", 97 }) 98 } 99 100 // Send request 101 replies, err := c.PluginReads(ctx, cmds) 102 if err != nil { 103 return nil, err 104 } 105 106 // Prepare reply 107 bscsr := make(map[string]pi.BillingStatusChangesReply, len(replies)) 108 for _, v := range replies { 109 err = extractPluginCmdError(v) 110 if err != nil { 111 // Individual summary errors are ignored. The token will not 112 // be included in the returned summaries map. 113 continue 114 } 115 var bscr pi.BillingStatusChangesReply 116 err = json.Unmarshal([]byte(v.Payload), &bscr) 117 if err != nil { 118 return nil, err 119 } 120 bscsr[v.Token] = bscr 121 } 122 123 return bscsr, nil 124 125 }