github.com/decred/politeia@v1.4.0/politeiawww/client/pi.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  	"encoding/base64"
     9  	"encoding/json"
    10  	"fmt"
    11  	"net/http"
    12  
    13  	piv1 "github.com/decred/politeia/politeiawww/api/pi/v1"
    14  	rcv1 "github.com/decred/politeia/politeiawww/api/records/v1"
    15  )
    16  
    17  // PiPolicy sends a pi v1 Policy request to politeiawww.
    18  func (c *Client) PiPolicy() (*piv1.PolicyReply, error) {
    19  	resBody, err := c.makeReq(http.MethodPost,
    20  		piv1.APIRoute, piv1.RoutePolicy, nil)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	var pr piv1.PolicyReply
    26  	err = json.Unmarshal(resBody, &pr)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	return &pr, nil
    32  }
    33  
    34  // PiSetBillingStatus sends a pi v1 SetBillingStatus request
    35  // to politeiawww.
    36  func (c *Client) PiSetBillingStatus(sbs piv1.SetBillingStatus) (*piv1.SetBillingStatusReply, error) {
    37  	resBody, err := c.makeReq(http.MethodPost,
    38  		piv1.APIRoute, piv1.RouteSetBillingStatus, sbs)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	var sbsr piv1.SetBillingStatusReply
    44  	err = json.Unmarshal(resBody, &sbsr)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	return &sbsr, nil
    50  }
    51  
    52  // PiSummaries sends a pi v1 Summaries request to politeiawww.
    53  func (c *Client) PiSummaries(s piv1.Summaries) (*piv1.SummariesReply, error) {
    54  	resBody, err := c.makeReq(http.MethodPost,
    55  		piv1.APIRoute, piv1.RouteSummaries, s)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	var sr piv1.SummariesReply
    61  	err = json.Unmarshal(resBody, &sr)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	return &sr, nil
    67  }
    68  
    69  // PiBillingStatusChanges sends a pi v1 BillingStatusChanges request to
    70  // politeiawww.
    71  func (c *Client) PiBillingStatusChanges(bscs piv1.BillingStatusChanges) (*piv1.BillingStatusChangesReply, error) {
    72  	resBody, err := c.makeReq(http.MethodPost,
    73  		piv1.APIRoute, piv1.RouteBillingStatusChanges, bscs)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	var bscsr piv1.BillingStatusChangesReply
    79  	err = json.Unmarshal(resBody, &bscsr)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  
    84  	return &bscsr, nil
    85  }
    86  
    87  // ProposalMetadataDecode decodes and returns the ProposalMetadata from the
    88  // Provided record files. An error returned if a ProposalMetadata is not found.
    89  func ProposalMetadataDecode(files []rcv1.File) (*piv1.ProposalMetadata, error) {
    90  	var pmp *piv1.ProposalMetadata
    91  	for _, v := range files {
    92  		if v.Name != piv1.FileNameProposalMetadata {
    93  			continue
    94  		}
    95  		b, err := base64.StdEncoding.DecodeString(v.Payload)
    96  		if err != nil {
    97  			return nil, err
    98  		}
    99  		var pm piv1.ProposalMetadata
   100  		err = json.Unmarshal(b, &pm)
   101  		if err != nil {
   102  			return nil, err
   103  		}
   104  		pmp = &pm
   105  		break
   106  	}
   107  	if pmp == nil {
   108  		return nil, fmt.Errorf("proposal metadata not found")
   109  	}
   110  	return pmp, nil
   111  }
   112  
   113  // VoteMetadataDecode decodes and returns the VoteMetadata from the provided
   114  // backend files. Nil is returned if a VoteMetadata is not found.
   115  func VoteMetadataDecode(files []rcv1.File) (*piv1.VoteMetadata, error) {
   116  	var vmp *piv1.VoteMetadata
   117  	for _, v := range files {
   118  		if v.Name != piv1.FileNameVoteMetadata {
   119  			continue
   120  		}
   121  		b, err := base64.StdEncoding.DecodeString(v.Payload)
   122  		if err != nil {
   123  			return nil, err
   124  		}
   125  		var vm piv1.VoteMetadata
   126  		err = json.Unmarshal(b, &vm)
   127  		if err != nil {
   128  			return nil, err
   129  		}
   130  		vmp = &vm
   131  		break
   132  	}
   133  	return vmp, nil
   134  }