/
client.go
2370 lines (1977 loc) · 54.7 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2017-2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package shared
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"reflect"
"strings"
"decred.org/dcrwallet/rpc/walletrpc"
cms "github.com/decred/politeia/politeiawww/api/cms/v1"
www "github.com/decred/politeia/politeiawww/api/www/v1"
"github.com/decred/politeia/util"
"github.com/gorilla/schema"
"golang.org/x/net/publicsuffix"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
// Client is a politeiawww client.
type Client struct {
http *http.Client
cfg *Config
// wallet grpc
ctx context.Context
creds credentials.TransportCredentials
conn *grpc.ClientConn
wallet walletrpc.WalletServiceClient
}
func prettyPrintJSON(v interface{}) error {
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
return fmt.Errorf("MarshalIndent: %v", err)
}
fmt.Fprintf(os.Stdout, "%s\n", b)
return nil
}
// userWWWErrorStatus retrieves the human readable error message for an error
// status code. The status code error message comes from the www api.
func userWWWErrorStatus(e www.ErrorStatusT) string {
s, ok := www.ErrorStatus[e]
if ok {
return s
}
s, ok = cms.ErrorStatus[e]
if ok {
return s
}
return ""
}
// wwwError unmarshals the response body from makeRequest, and handles any
// status code errors from the server. Parses the error code and error context
// from the www api, in case of user error.
func wwwError(body []byte, statusCode int) error {
switch statusCode {
case http.StatusBadRequest:
// User Error
var ue www.UserError
err := json.Unmarshal(body, &ue)
if err != nil {
return fmt.Errorf("unmarshal UserError: %v", err)
}
if ue.ErrorCode != 0 {
var e error
errMsg := userWWWErrorStatus(ue.ErrorCode)
if len(ue.ErrorContext) == 0 {
// Error format when an ErrorContext is not included
e = fmt.Errorf("%v, %v", statusCode, errMsg)
} else {
// Error format when an ErrorContext is included
e = fmt.Errorf("%v, %v: %v", statusCode, errMsg,
strings.Join(ue.ErrorContext, ", "))
}
return e
}
case http.StatusInternalServerError:
// Server Error
var er www.ErrorReply
err := json.Unmarshal(body, &er)
if err != nil {
return fmt.Errorf("unmarshal Error: %v", err)
}
var e error
if len(er.ErrorContext) == 0 {
// Error format when an ErrorContext is not included
e = fmt.Errorf("ServerError timestamp: %v", er.ErrorCode)
} else {
// Error format when an ErrorContext is included
e = fmt.Errorf("ServerError timestamp: %v context: %v",
er.ErrorCode, er.ErrorContext)
}
return e
default:
// Default Status Code Error
return fmt.Errorf("%v", statusCode)
}
return nil
}
// makeRequest sends the provided request to the politeiawww backend specified
// by the Client config. This function handles verbose printing when specified
// by the Client config since verbose printing includes details such as the
// full route and http response codes. Caller functions handle status code
// validation and error checks.
func (c *Client) makeRequest(method, routeVersion, route string, body interface{}) (int, []byte, error) {
// Setup request
var requestBody []byte
var queryParams string
if body != nil {
switch {
case method == http.MethodGet:
// Use reflection in case the interface value is nil
// but the interface type is not. This can happen when
// query params exist but are not used.
if reflect.ValueOf(body).IsNil() {
break
}
// GET requests don't have a request body; instead we
// will populate the query params.
form := url.Values{}
if err := schema.NewEncoder().Encode(body, form); err != nil {
return 0, nil, err
}
queryParams = "?" + form.Encode()
case method == http.MethodPost || method == http.MethodPut:
var err error
requestBody, err = json.Marshal(body)
if err != nil {
return 0, nil, err
}
default:
return 0, nil, fmt.Errorf("unknown http method '%v'", method)
}
}
fullRoute := c.cfg.Host + routeVersion + route + queryParams
// Print request details
switch {
case c.cfg.Verbose && method == http.MethodGet:
fmt.Printf("Request: GET %v\n", fullRoute)
case c.cfg.Verbose && method == http.MethodPost:
fmt.Printf("Request: POST %v\n", fullRoute)
err := prettyPrintJSON(body)
if err != nil {
return 0, nil, err
}
case c.cfg.Verbose && method == http.MethodPut:
fmt.Printf("Request: PUT %v\n", fullRoute)
err := prettyPrintJSON(body)
if err != nil {
return 0, nil, err
}
}
// Create http request
req, err := http.NewRequest(method, fullRoute, bytes.NewReader(requestBody))
if err != nil {
return 0, nil, err
}
req.Header.Add(www.CsrfToken, c.cfg.CSRF)
// Send request
r, err := c.http.Do(req)
if err != nil {
return 0, nil, err
}
defer func() {
r.Body.Close()
}()
responseBody := util.ConvertBodyToByteArray(r.Body, false)
// Print response details
if c.cfg.Verbose {
fmt.Printf("Response: %v\n", r.StatusCode)
}
return r.StatusCode, responseBody, nil
}
// Version returns the version information for the politeiawww instance.
func (c *Client) Version() (*www.VersionReply, error) {
fullRoute := c.cfg.Host + www.PoliteiaWWWAPIRoute + www.RouteVersion
// Print request details
if c.cfg.Verbose {
fmt.Printf("Request: GET %v\n", fullRoute)
}
// Create new http request instead of using makeRequest()
// so that we can save the CSRF tokens to disk.
req, err := http.NewRequest(http.MethodGet, fullRoute, nil)
if err != nil {
return nil, err
}
req.Header.Add(www.CsrfToken, c.cfg.CSRF)
// Send request
r, err := c.http.Do(req)
if err != nil {
return nil, err
}
defer func() {
r.Body.Close()
}()
respBody := util.ConvertBodyToByteArray(r.Body, false)
// Validate response status
if r.StatusCode != http.StatusOK {
var ue www.UserError
err = json.Unmarshal(respBody, &ue)
if err == nil {
return nil, fmt.Errorf("%v, %v %v", r.StatusCode,
userWWWErrorStatus(ue.ErrorCode),
strings.Join(ue.ErrorContext, ", "))
}
return nil, fmt.Errorf("%v", r.StatusCode)
}
// Unmarshal response
var vr www.VersionReply
err = json.Unmarshal(respBody, &vr)
if err != nil {
return nil, fmt.Errorf("unmarshal VersionReply: %v", err)
}
// Print response details
if c.cfg.Verbose {
fmt.Printf("Response: %v\n", r.StatusCode)
err := prettyPrintJSON(vr)
if err != nil {
return nil, err
}
}
// CSRF protection works via the double-submit method.
// One token is sent in the cookie. A second token is
// sent in the header. Both tokens must be persisted
// between CLI commands.
// Persist CSRF header token
c.cfg.CSRF = r.Header.Get(www.CsrfToken)
err = c.cfg.SaveCSRF(c.cfg.CSRF)
if err != nil {
return nil, err
}
// Persist CSRF cookie token
err = c.cfg.SaveCookies(c.http.Jar.Cookies(req.URL))
if err != nil {
return nil, err
}
return &vr, nil
}
// Login logs a user into politeiawww.
func (c *Client) Login(l *www.Login) (*www.LoginReply, error) {
// Setup request
requestBody, err := json.Marshal(l)
if err != nil {
return nil, err
}
fullRoute := c.cfg.Host + www.PoliteiaWWWAPIRoute + www.RouteLogin
// Print request details
if c.cfg.Verbose {
fmt.Printf("Request: POST %v\n", fullRoute)
err := prettyPrintJSON(l)
if err != nil {
return nil, err
}
}
// Create new http request instead of using makeRequest()
// so that we can save the session data for subsequent
// commands
req, err := http.NewRequest(http.MethodPost, fullRoute,
bytes.NewReader(requestBody))
if err != nil {
return nil, err
}
req.Header.Add(www.CsrfToken, c.cfg.CSRF)
// Send request
r, err := c.http.Do(req)
if err != nil {
return nil, err
}
defer func() {
r.Body.Close()
}()
respBody := util.ConvertBodyToByteArray(r.Body, false)
// Validate response status
if r.StatusCode != http.StatusOK {
var ue www.UserError
err = json.Unmarshal(respBody, &ue)
if err == nil {
return nil, fmt.Errorf("%v, %v %v", r.StatusCode,
userWWWErrorStatus(ue.ErrorCode),
strings.Join(ue.ErrorContext, ", "))
}
return nil, fmt.Errorf("%v", r.StatusCode)
}
// Unmarshal response
var lr www.LoginReply
err = json.Unmarshal(respBody, &lr)
if err != nil {
return nil, fmt.Errorf("unmarshal LoginReply: %v", err)
}
// Print response details
if c.cfg.Verbose {
fmt.Printf("Response: %v\n", r.StatusCode)
err := prettyPrintJSON(lr)
if err != nil {
return nil, err
}
}
// Persist session data
ck := c.http.Jar.Cookies(req.URL)
if err = c.cfg.SaveCookies(ck); err != nil {
return nil, err
}
return &lr, nil
}
// Logout logs out a user from politeiawww.
func (c *Client) Logout() (*www.LogoutReply, error) {
fullRoute := c.cfg.Host + www.PoliteiaWWWAPIRoute + www.RouteLogout
// Print request details
if c.cfg.Verbose {
fmt.Printf("Request: POST %v\n", fullRoute)
}
// Create new http request instead of using makeRequest()
// so that we can save the updated cookies to disk
req, err := http.NewRequest(http.MethodPost, fullRoute, nil)
if err != nil {
return nil, err
}
req.Header.Add(www.CsrfToken, c.cfg.CSRF)
// Send request
r, err := c.http.Do(req)
if err != nil {
return nil, err
}
defer func() {
r.Body.Close()
}()
respBody := util.ConvertBodyToByteArray(r.Body, false)
// Validate response status
if r.StatusCode != http.StatusOK {
var ue www.UserError
err = json.Unmarshal(respBody, &ue)
if err == nil {
return nil, fmt.Errorf("%v, %v %v", r.StatusCode,
userWWWErrorStatus(ue.ErrorCode),
strings.Join(ue.ErrorContext, ", "))
}
return nil, fmt.Errorf("%v", r.StatusCode)
}
// Unmarshal response
var lr www.LogoutReply
err = json.Unmarshal(respBody, &lr)
if err != nil {
return nil, fmt.Errorf("unmarshal LogoutReply: %v", err)
}
// Print response details
if c.cfg.Verbose {
fmt.Printf("Response: %v\n", r.StatusCode)
err := prettyPrintJSON(lr)
if err != nil {
return nil, err
}
}
// Persist cookies
ck := c.http.Jar.Cookies(req.URL)
if err = c.cfg.SaveCookies(ck); err != nil {
return nil, err
}
return &lr, nil
}
// Policy returns the politeiawww policy information.
func (c *Client) Policy() (*www.PolicyReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodGet,
www.PoliteiaWWWAPIRoute, www.RoutePolicy, nil)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var pr www.PolicyReply
err = json.Unmarshal(respBody, &pr)
if err != nil {
return nil, fmt.Errorf("unmarshal PolicyReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(pr)
if err != nil {
return nil, err
}
}
return &pr, nil
}
// CMSPolicy returns the politeiawww policy information.
func (c *Client) CMSPolicy() (*cms.PolicyReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodGet,
www.PoliteiaWWWAPIRoute, www.RoutePolicy, nil)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var pr cms.PolicyReply
err = json.Unmarshal(respBody, &pr)
if err != nil {
return nil, fmt.Errorf("unmarshal CMSPolicyReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(pr)
if err != nil {
return nil, err
}
}
return &pr, nil
}
// InviteNewUser creates a new cmswww user.
func (c *Client) InviteNewUser(inu *cms.InviteNewUser) (*cms.InviteNewUserReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodPost,
cms.APIRoute, cms.RouteInviteNewUser, inu)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var inur cms.InviteNewUserReply
err = json.Unmarshal(respBody, &inur)
if err != nil {
return nil, fmt.Errorf("unmarshal InviteNewUserReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(inur)
if err != nil {
return nil, err
}
}
return &inur, nil
}
// RegisterUser finalizes the signup process for a new cmswww user.
func (c *Client) RegisterUser(ru *cms.RegisterUser) (*cms.RegisterUserReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodPost,
cms.APIRoute, cms.RouteRegisterUser, ru)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var rur cms.RegisterUserReply
err = json.Unmarshal(respBody, &rur)
if err != nil {
return nil, fmt.Errorf("unmarshal RegisterUserReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(rur)
if err != nil {
return nil, err
}
}
return &rur, nil
}
// NewUser creates a new politeiawww user.
func (c *Client) NewUser(nu *www.NewUser) (*www.NewUserReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodPost,
www.PoliteiaWWWAPIRoute, www.RouteNewUser, nu)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var nur www.NewUserReply
err = json.Unmarshal(respBody, &nur)
if err != nil {
return nil, fmt.Errorf("unmarshal NewUserReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(nur)
if err != nil {
return nil, err
}
}
return &nur, nil
}
// VerifyNewUser verifies a user's email address.
func (c *Client) VerifyNewUser(vnu *www.VerifyNewUser) (*www.VerifyNewUserReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodGet,
www.PoliteiaWWWAPIRoute, www.RouteVerifyNewUser, vnu)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var vnur www.VerifyNewUserReply
err = json.Unmarshal(respBody, &vnur)
if err != nil {
return nil, fmt.Errorf("unmarshal VerifyNewUserReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(vnur)
if err != nil {
return nil, err
}
}
return &vnur, nil
}
// Me returns user details for the logged in user.
func (c *Client) Me() (*www.LoginReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodGet,
www.PoliteiaWWWAPIRoute, www.RouteUserMe, nil)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var lr www.LoginReply
err = json.Unmarshal(respBody, &lr)
if err != nil {
return nil, fmt.Errorf("unmarshal LoginReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(lr)
if err != nil {
return nil, err
}
}
return &lr, nil
}
// Secret pings politeiawww.
func (c *Client) Secret() (*www.UserError, error) {
statusCode, respBody, err := c.makeRequest(http.MethodPost,
www.PoliteiaWWWAPIRoute, www.RouteSecret, nil)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var ue www.UserError
err = json.Unmarshal(respBody, &ue)
if err != nil {
return nil, fmt.Errorf("unmarshal UserError: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(ue)
if err != nil {
return nil, err
}
}
return &ue, nil
}
// ChangeUsername changes the username of the logged in user.
func (c *Client) ChangeUsername(cu *www.ChangeUsername) (*www.ChangeUsernameReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodPost,
www.PoliteiaWWWAPIRoute, www.RouteChangeUsername, cu)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var cur www.ChangeUsernameReply
err = json.Unmarshal(respBody, &cur)
if err != nil {
return nil, fmt.Errorf("unmarshal ChangeUsernameReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(cur)
if err != nil {
return nil, err
}
}
return &cur, nil
}
// ChangePassword changes the password for the logged in user.
func (c *Client) ChangePassword(cp *www.ChangePassword) (*www.ChangePasswordReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodPost,
www.PoliteiaWWWAPIRoute, www.RouteChangePassword, cp)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var cpr www.ChangePasswordReply
err = json.Unmarshal(respBody, &cpr)
if err != nil {
return nil, fmt.Errorf("unmarshal ChangePasswordReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(cpr)
if err != nil {
return nil, err
}
}
return &cpr, nil
}
// ResetPassword resets the password of the specified user.
func (c *Client) ResetPassword(rp *www.ResetPassword) (*www.ResetPasswordReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodPost,
www.PoliteiaWWWAPIRoute, www.RouteResetPassword, rp)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var rpr www.ResetPasswordReply
err = json.Unmarshal(respBody, &rpr)
if err != nil {
return nil, fmt.Errorf("unmarshal ResetPasswordReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(rpr)
if err != nil {
return nil, err
}
}
return &rpr, nil
}
// VerifyResetPassword sends the VerifyResetPassword command to politeiawww.
func (c *Client) VerifyResetPassword(vrp www.VerifyResetPassword) (*www.VerifyResetPasswordReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodPost,
www.PoliteiaWWWAPIRoute, www.RouteVerifyResetPassword, vrp)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var reply www.VerifyResetPasswordReply
err = json.Unmarshal(respBody, &reply)
if err != nil {
return nil, fmt.Errorf("unmarshal VerifyResetPasswordReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(reply)
if err != nil {
return nil, err
}
}
return &reply, nil
}
// UserProposalPaywall retrieves proposal credit paywall information for the
// logged in user.
func (c *Client) UserProposalPaywall() (*www.UserProposalPaywallReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodGet,
www.PoliteiaWWWAPIRoute, www.RouteUserProposalPaywall, nil)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var ppdr www.UserProposalPaywallReply
err = json.Unmarshal(respBody, &ppdr)
if err != nil {
return nil, fmt.Errorf("unmarshal ProposalPaywalDetailsReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(ppdr)
if err != nil {
return nil, err
}
}
return &ppdr, nil
}
// NewInvoice submits the specified invoice to politeiawww for the logged in
// user.
func (c *Client) NewInvoice(ni *cms.NewInvoice) (*cms.NewInvoiceReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodPost,
cms.APIRoute, cms.RouteNewInvoice, ni)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var nir cms.NewInvoiceReply
err = json.Unmarshal(respBody, &nir)
if err != nil {
return nil, fmt.Errorf("unmarshal NewInvoiceReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(nir)
if err != nil {
return nil, err
}
}
return &nir, nil
}
// EditInvoice edits the specified invoice with the logged in user.
func (c *Client) EditInvoice(ei *cms.EditInvoice) (*cms.EditInvoiceReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodPost,
cms.APIRoute, cms.RouteEditInvoice, ei)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var eir cms.EditInvoiceReply
err = json.Unmarshal(respBody, &eir)
if err != nil {
return nil, fmt.Errorf("unmarshal EditInvoiceReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(eir)
if err != nil {
return nil, err
}
}
return &eir, nil
}
// ProposalDetails retrieves the specified proposal.
func (c *Client) ProposalDetails(token string, pd *www.ProposalsDetails) (*www.ProposalDetailsReply, error) {
route := "/proposals/" + token
statusCode, respBody, err := c.makeRequest(http.MethodGet,
www.PoliteiaWWWAPIRoute, route, pd)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var pr www.ProposalDetailsReply
err = json.Unmarshal(respBody, &pr)
if err != nil {
return nil, fmt.Errorf("unmarshal ProposalDetailsReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(pr)
if err != nil {
return nil, err
}
}
return &pr, nil
}
// UserInvoices retrieves the proposals that have been submitted by the
// specified user.
func (c *Client) UserInvoices(up *cms.UserInvoices) (*cms.UserInvoicesReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodGet,
cms.APIRoute, cms.RouteUserInvoices, up)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var upr cms.UserInvoicesReply
err = json.Unmarshal(respBody, &upr)
if err != nil {
return nil, fmt.Errorf("unmarshal UserInvoicesReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(upr)
if err != nil {
return nil, err
}
}
return &upr, nil
}
// ProposalBilling retrieves the billing for the requested proposal
func (c *Client) ProposalBilling(pb *cms.ProposalBilling) (*cms.ProposalBillingReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodPost,
cms.APIRoute, cms.RouteProposalBilling, pb)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var pbr cms.ProposalBillingReply
err = json.Unmarshal(respBody, &pbr)
if err != nil {
return nil, fmt.Errorf("unmarshal ProposalBillingReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(pbr)
if err != nil {
return nil, err
}
}
return &pbr, nil
}
// ProposalBillingDetails retrieves the billing for the requested proposal
func (c *Client) ProposalBillingDetails(pbd *cms.ProposalBillingDetails) (*cms.ProposalBillingDetailsReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodPost,
cms.APIRoute, cms.RouteProposalBillingDetails, pbd)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var pbdr cms.ProposalBillingDetailsReply
err = json.Unmarshal(respBody, &pbdr)
if err != nil {
return nil, fmt.Errorf("unmarshal ProposalBillingDetailsReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(pbdr)
if err != nil {
return nil, err
}
}
return &pbdr, nil
}
// ProposalBillingSummary retrieves the billing for all approved proposals.
func (c *Client) ProposalBillingSummary(pbd *cms.ProposalBillingSummary) (*cms.ProposalBillingSummaryReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodGet,
cms.APIRoute, cms.RouteProposalBillingSummary, pbd)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var pbdr cms.ProposalBillingSummaryReply
err = json.Unmarshal(respBody, &pbdr)
if err != nil {
return nil, fmt.Errorf("unmarshal ProposalBillingSummaryReply: %v", err)
}
if c.cfg.Verbose {
err := prettyPrintJSON(pbdr)
if err != nil {
return nil, err
}
}
return &pbdr, nil
}
// Invoices retrieves invoices base on possible field set in the request
// month/year and/or status
func (c *Client) Invoices(ai *cms.Invoices) (*cms.InvoicesReply, error) {
statusCode, respBody, err := c.makeRequest(http.MethodPost,
cms.APIRoute, cms.RouteInvoices, ai)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, wwwError(respBody, statusCode)
}
var air cms.InvoicesReply
err = json.Unmarshal(respBody, &air)
if err != nil {