github.com/decred/politeia@v1.4.0/politeiad/cmd/legacypoliteia/dcrdata.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 "bytes" 9 "encoding/json" 10 "fmt" 11 "io" 12 "net/http" 13 14 dcrdata "github.com/decred/dcrdata/v6/api/types" 15 ) 16 17 // dcrdata.go contains API requests to the dcrdata API. 18 19 const ( 20 dcrdataHost = "https://dcrdata.decred.org/api" 21 ) 22 23 // trimmedTxs returned the trimmed transaction data for each of the provided 24 // transaction hashes. 25 func (c *convertCmd) trimmedTxs(txs []string) ([]dcrdata.TrimmedTx, error) { 26 req := dcrdata.Txns{ 27 Transactions: txs, 28 } 29 reqBody, err := json.Marshal(req) 30 if err != nil { 31 return nil, err 32 } 33 34 var ( 35 url = dcrdataHost + "/txs/trimmed" 36 b = bytes.NewReader(reqBody) 37 ) 38 r, err := c.client.Post(url, "application/json; charset=utf-8", b) 39 if err != nil { 40 return nil, err 41 } 42 defer r.Body.Close() 43 44 if r.StatusCode != http.StatusOK { 45 body, err := io.ReadAll(r.Body) 46 if err != nil { 47 return nil, fmt.Errorf("dcrdata error: %v %v %v", 48 r.StatusCode, url, err) 49 } 50 return nil, fmt.Errorf("dcrdata error: %v %v %s", 51 r.StatusCode, url, body) 52 } 53 54 var ttxs []dcrdata.TrimmedTx 55 decoder := json.NewDecoder(r.Body) 56 if err := decoder.Decode(&ttxs); err != nil { 57 return nil, err 58 } 59 60 return ttxs, nil 61 }