github.com/decred/politeia@v1.4.0/politeiad/client/ticketvote.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/ticketvote" 14 ) 15 16 // TicketVoteAuthorize sends the ticketvote plugin Authorize command to the 17 // politeiad v2 API. 18 func (c *Client) TicketVoteAuthorize(ctx context.Context, a ticketvote.Authorize) (*ticketvote.AuthorizeReply, error) { 19 // Setup request 20 b, err := json.Marshal(a) 21 if err != nil { 22 return nil, err 23 } 24 cmd := pdv2.PluginCmd{ 25 Token: a.Token, 26 ID: ticketvote.PluginID, 27 Command: ticketvote.CmdAuthorize, 28 Payload: string(b), 29 } 30 31 // Send request 32 reply, err := c.PluginWrite(ctx, cmd) 33 if err != nil { 34 return nil, err 35 } 36 37 // Decode reply 38 var ar ticketvote.AuthorizeReply 39 err = json.Unmarshal([]byte(reply), &ar) 40 if err != nil { 41 return nil, err 42 } 43 44 return &ar, nil 45 } 46 47 // TicketVoteStart sends the ticketvote plugin Start command to the politeiad 48 // v2 API. 49 func (c *Client) TicketVoteStart(ctx context.Context, token string, s ticketvote.Start) (*ticketvote.StartReply, error) { 50 // Setup request 51 b, err := json.Marshal(s) 52 if err != nil { 53 return nil, err 54 } 55 cmd := pdv2.PluginCmd{ 56 Token: token, 57 ID: ticketvote.PluginID, 58 Command: ticketvote.CmdStart, 59 Payload: string(b), 60 } 61 62 // Send request 63 reply, err := c.PluginWrite(ctx, cmd) 64 if err != nil { 65 return nil, err 66 } 67 68 // Decode reply 69 var sr ticketvote.StartReply 70 err = json.Unmarshal([]byte(reply), &sr) 71 if err != nil { 72 return nil, err 73 } 74 75 return &sr, nil 76 } 77 78 // TicketVoteCastBallot sends the ticketvote plugin CastBallot command to the 79 // politeiad v2 API. 80 func (c *Client) TicketVoteCastBallot(ctx context.Context, token string, cb ticketvote.CastBallot) (*ticketvote.CastBallotReply, error) { 81 // Setup request 82 b, err := json.Marshal(cb) 83 if err != nil { 84 return nil, err 85 } 86 cmd := pdv2.PluginCmd{ 87 Token: token, 88 ID: ticketvote.PluginID, 89 Command: ticketvote.CmdCastBallot, 90 Payload: string(b), 91 } 92 93 // Send request 94 reply, err := c.PluginWrite(ctx, cmd) 95 if err != nil { 96 return nil, err 97 } 98 99 // Decode reply 100 var cbr ticketvote.CastBallotReply 101 err = json.Unmarshal([]byte(reply), &cbr) 102 if err != nil { 103 return nil, err 104 } 105 106 return &cbr, nil 107 } 108 109 // TicketVoteDetails sends the ticketvote plugin Details command to the 110 // politeiad v2 API. 111 func (c *Client) TicketVoteDetails(ctx context.Context, token string) (*ticketvote.DetailsReply, error) { 112 // Setup request 113 cmds := []pdv2.PluginCmd{ 114 { 115 Token: token, 116 ID: ticketvote.PluginID, 117 Command: ticketvote.CmdDetails, 118 Payload: "", 119 }, 120 } 121 122 // Send request 123 replies, err := c.PluginReads(ctx, cmds) 124 if err != nil { 125 return nil, err 126 } 127 if len(replies) == 0 { 128 return nil, fmt.Errorf("no replies found") 129 } 130 pcr := replies[0] 131 err = extractPluginCmdError(pcr) 132 if err != nil { 133 return nil, err 134 } 135 136 // Decode reply 137 var dr ticketvote.DetailsReply 138 err = json.Unmarshal([]byte(pcr.Payload), &dr) 139 if err != nil { 140 return nil, err 141 } 142 143 return &dr, nil 144 } 145 146 // TicketVoteResults sends the ticketvote plugin Results command to the 147 // politeiad v2 API. 148 func (c *Client) TicketVoteResults(ctx context.Context, token string) (*ticketvote.ResultsReply, error) { 149 // Setup request 150 cmds := []pdv2.PluginCmd{ 151 { 152 Token: token, 153 ID: ticketvote.PluginID, 154 Command: ticketvote.CmdResults, 155 Payload: "", 156 }, 157 } 158 159 // Send request 160 replies, err := c.PluginReads(ctx, cmds) 161 if err != nil { 162 return nil, err 163 } 164 if len(replies) == 0 { 165 return nil, fmt.Errorf("no replies found") 166 } 167 pcr := replies[0] 168 err = extractPluginCmdError(pcr) 169 if err != nil { 170 return nil, err 171 } 172 173 // Decode reply 174 var rr ticketvote.ResultsReply 175 err = json.Unmarshal([]byte(pcr.Payload), &rr) 176 if err != nil { 177 return nil, err 178 } 179 180 return &rr, nil 181 } 182 183 // TicketVoteSummary sends the ticketvote plugin Summary command to the 184 // politeiad v2 API. 185 func (c *Client) TicketVoteSummary(ctx context.Context, token string) (*ticketvote.SummaryReply, error) { 186 // Setup request 187 cmds := []pdv2.PluginCmd{ 188 { 189 ID: ticketvote.PluginID, 190 Command: ticketvote.CmdSummary, 191 Token: token, 192 Payload: "", 193 }, 194 } 195 196 // Send request 197 replies, err := c.PluginReads(ctx, cmds) 198 if err != nil { 199 return nil, err 200 } 201 if len(replies) == 0 { 202 return nil, fmt.Errorf("no replies found") 203 } 204 pcr := replies[0] 205 err = extractPluginCmdError(pcr) 206 if err != nil { 207 return nil, err 208 } 209 210 // Decode reply 211 var sr ticketvote.SummaryReply 212 err = json.Unmarshal([]byte(pcr.Payload), &sr) 213 if err != nil { 214 return nil, err 215 } 216 217 return &sr, nil 218 } 219 220 // TicketVoteSummaries sends a batch of ticketvote plugin Summary commands to 221 // the politeiad v2 API. Individual summary errors are not returned, the token 222 // will simply be left out of the returned map. 223 func (c *Client) TicketVoteSummaries(ctx context.Context, tokens []string) (map[string]ticketvote.SummaryReply, error) { 224 // Setup request 225 cmds := make([]pdv2.PluginCmd, 0, len(tokens)) 226 for _, v := range tokens { 227 cmds = append(cmds, pdv2.PluginCmd{ 228 Token: v, 229 ID: ticketvote.PluginID, 230 Command: ticketvote.CmdSummary, 231 Payload: "", 232 }) 233 } 234 235 // Send request 236 replies, err := c.PluginReads(ctx, cmds) 237 if err != nil { 238 return nil, err 239 } 240 241 // Prepare reply 242 summaries := make(map[string]ticketvote.SummaryReply, len(replies)) 243 for _, v := range replies { 244 err = extractPluginCmdError(v) 245 if err != nil { 246 // Individual summary errors are ignored. The token will not 247 // be included in the returned summaries map. 248 continue 249 } 250 var sr ticketvote.SummaryReply 251 err = json.Unmarshal([]byte(v.Payload), &sr) 252 if err != nil { 253 return nil, err 254 } 255 summaries[v.Token] = sr 256 } 257 258 return summaries, nil 259 } 260 261 // TicketVoteSubmissions sends the ticketvote plugin Submissions command to the 262 // politeiad v2 API. 263 func (c *Client) TicketVoteSubmissions(ctx context.Context, token string) ([]string, error) { 264 // Setup request 265 cmds := []pdv2.PluginCmd{ 266 { 267 Token: token, 268 ID: ticketvote.PluginID, 269 Command: ticketvote.CmdSubmissions, 270 Payload: "", 271 }, 272 } 273 274 // Send request 275 replies, err := c.PluginReads(ctx, cmds) 276 if err != nil { 277 return nil, err 278 } 279 if len(replies) == 0 { 280 return nil, fmt.Errorf("no replies found") 281 } 282 pcr := replies[0] 283 err = extractPluginCmdError(pcr) 284 if err != nil { 285 return nil, err 286 } 287 288 // Decode reply 289 var sr ticketvote.SubmissionsReply 290 err = json.Unmarshal([]byte(pcr.Payload), &sr) 291 if err != nil { 292 return nil, err 293 } 294 295 return sr.Submissions, nil 296 } 297 298 // TicketVoteInventory sends the ticketvote plugin Inventory command to the 299 // politeiad v2 API. 300 func (c *Client) TicketVoteInventory(ctx context.Context, i ticketvote.Inventory) (*ticketvote.InventoryReply, error) { 301 // Setup request 302 b, err := json.Marshal(i) 303 if err != nil { 304 return nil, err 305 } 306 cmds := []pdv2.PluginCmd{ 307 { 308 ID: ticketvote.PluginID, 309 Command: ticketvote.CmdInventory, 310 Payload: string(b), 311 }, 312 } 313 314 // Send request 315 replies, err := c.PluginReads(ctx, cmds) 316 if err != nil { 317 return nil, err 318 } 319 if len(replies) == 0 { 320 return nil, fmt.Errorf("no replies found") 321 } 322 pcr := replies[0] 323 err = extractPluginCmdError(pcr) 324 if err != nil { 325 return nil, err 326 } 327 328 // Decode reply 329 var ir ticketvote.InventoryReply 330 err = json.Unmarshal([]byte(pcr.Payload), &ir) 331 if err != nil { 332 return nil, err 333 } 334 335 return &ir, nil 336 } 337 338 // TicketVoteTimestamps sends the ticketvote plugin Timestamps command to the 339 // politeiad v2 API. 340 func (c *Client) TicketVoteTimestamps(ctx context.Context, token string, t ticketvote.Timestamps) (*ticketvote.TimestampsReply, error) { 341 // Setup request 342 b, err := json.Marshal(t) 343 if err != nil { 344 return nil, err 345 } 346 cmds := []pdv2.PluginCmd{ 347 { 348 ID: ticketvote.PluginID, 349 Command: ticketvote.CmdTimestamps, 350 Token: token, 351 Payload: string(b), 352 }, 353 } 354 355 // Send request 356 replies, err := c.PluginReads(ctx, cmds) 357 if err != nil { 358 return nil, err 359 } 360 if len(replies) == 0 { 361 return nil, fmt.Errorf("no replies found") 362 } 363 pcr := replies[0] 364 err = extractPluginCmdError(pcr) 365 if err != nil { 366 return nil, err 367 } 368 369 // Decode reply 370 var sr ticketvote.TimestampsReply 371 err = json.Unmarshal([]byte(pcr.Payload), &sr) 372 if err != nil { 373 return nil, err 374 } 375 376 return &sr, nil 377 }