github.com/decred/politeia@v1.4.0/politeiad/cmd/legacypoliteia/politeia.go (about)

     1  // Copyright (c) 2022 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 main
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  	"io"
    11  	"net/http"
    12  
    13  	v1 "github.com/decred/politeia/politeiawww/api/www/v1"
    14  )
    15  
    16  // politeia.go contains API requests to the politeia API.
    17  
    18  const (
    19  	politeiaHost = "https://proposals.decred.org/api"
    20  )
    21  
    22  // userByID retrieves and returns the user object from the politeia API using
    23  // the provided user ID.
    24  func userByID(c *http.Client, userID string) (*v1.User, error) {
    25  	url := politeiaHost + "/v1/user/" + userID
    26  	r, err := c.Get(url)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	defer r.Body.Close()
    31  
    32  	body, err := io.ReadAll(r.Body)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	var udr v1.UserDetailsReply
    38  	err = json.Unmarshal(body, &udr)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	return &udr.User, nil
    44  }
    45  
    46  // userByPubKey retrieves and returns the user object from the politeia API
    47  // using the provided public key.
    48  func userByPubKey(c *http.Client, pubkey string) (*v1.AbridgedUser, error) {
    49  	url := politeiaHost + "/v1/users?publickey=" + pubkey
    50  	r, err := c.Get(url)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	defer r.Body.Close()
    55  
    56  	body, err := io.ReadAll(r.Body)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	var ur v1.UsersReply
    62  	err = json.Unmarshal(body, &ur)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	if len(ur.Users) == 0 {
    68  		return nil, fmt.Errorf("no user found for pubkey %v", pubkey)
    69  	}
    70  
    71  	return &ur.Users[0], nil
    72  }