Skip to content
Snippets Groups Projects
Select Git revision
  • aec6710ed539e422183de28c1e220364275da4c4
  • without_tipselection default
  • develop protected
  • fix/grafana-local-dashboard
  • wasp
  • fix/dashboard-explorer-freeze
  • master
  • feat/timerqueue
  • test/sync_debug_and_650
  • feat/sync_revamp_inv
  • wip/sync
  • tool/db-recovery
  • portcheck/fix
  • fix/synchronization
  • feat/new-dashboard-analysis
  • feat/refactored-analysis-dashboard
  • feat/new-analysis-dashboard
  • test/demo-prometheus-fpc
  • prometheus_metrics
  • wip/analysis-server
  • merge/fpc-test-value-transfer
  • v0.2.2
  • v0.2.1
  • v0.2.0
  • v0.1.3
  • v0.1.2
  • v0.1.1
  • v0.1.0
28 results

lib.go

Blame
  • lib.go 7.90 KiB
    // Implements a very simple wrapper for GoShimmer's web API .
    package goshimmer
    
    import (
    	"bytes"
    	"encoding/json"
    	"errors"
    	"fmt"
    	"io"
    	"io/ioutil"
    	"net/http"
    
    	webapi_broadcastData "github.com/iotaledger/goshimmer/plugins/webapi/broadcastData"
    	webapi_findTransactionHashes "github.com/iotaledger/goshimmer/plugins/webapi/findTransactionHashes"
    	webapi_getNeighbors "github.com/iotaledger/goshimmer/plugins/webapi/getNeighbors"
    	webapi_getTransactionObjectsByHash "github.com/iotaledger/goshimmer/plugins/webapi/getTransactionObjectsByHash"
    	webapi_getTransactionTrytesByHash "github.com/iotaledger/goshimmer/plugins/webapi/getTransactionTrytesByHash"
    	webapi_gtta "github.com/iotaledger/goshimmer/plugins/webapi/gtta"
    	webapi_spammer "github.com/iotaledger/goshimmer/plugins/webapi/spammer"
    	webapi_auth "github.com/iotaledger/goshimmer/plugins/webauth"
    	"github.com/iotaledger/iota.go/consts"
    	"github.com/iotaledger/iota.go/guards"
    	"github.com/iotaledger/iota.go/trinary"
    )
    
    var (
    	ErrBadRequest          = errors.New("bad request")
    	ErrInternalServerError = errors.New("internal server error")
    	ErrNotFound            = errors.New("not found")
    	ErrUnauthorized        = errors.New("unauthorized")
    	ErrUnknownError        = errors.New("unknown error")
    )
    
    const (
    	routeBroadcastData               = "broadcastData"
    	routeGetTransactionTrytesByHash  = "getTransactionTrytesByHash"
    	routeGetTransactionObjectsByHash = "getTransactionObjectsByHash"
    	routeFindTransactionsHashes      = "findTransactionHashes"
    	routeGetNeighbors                = "getNeighbors"
    	routeGetTransactionsToApprove    = "getTransactionsToApprove"
    	routeSpammer                     = "spammer"
    	routeLogin                       = "login"
    
    	contentTypeJSON = "application/json"
    )
    
    func NewGoShimmerAPI(node string, httpClient ...http.Client) *GoShimmerAPI {
    	if len(httpClient) > 0 {
    		return &GoShimmerAPI{node: node, httpClient: httpClient[0]}
    	}
    	return &GoShimmerAPI{node: node}
    }
    
    // GoShimmerAPI is an API wrapper over the web API of GoShimmer.
    type GoShimmerAPI struct {
    	httpClient http.Client
    	node       string
    	jwt        string
    }
    
    type errorresponse struct {
    	Error string `json:"error"`
    }
    
    func interpretBody(res *http.Response, decodeTo interface{}) error {
    	resBody, err := ioutil.ReadAll(res.Body)
    	if err != nil {
    		return fmt.Errorf("unable to read response body: %w", err)
    	}
    	defer res.Body.Close()