github.com/aeternity/aepp-sdk-go/v7@v7.0.1/naet/node.go (about)

     1  package naet
     2  
     3  import (
     4  	"strings"
     5  
     6  	apiclient "github.com/aeternity/aepp-sdk-go/v7/swagguard/node/client"
     7  	httptransport "github.com/go-openapi/runtime/client"
     8  	"github.com/go-openapi/strfmt"
     9  )
    10  
    11  // Node represents a HTTP connection to an aeternity node
    12  type Node struct {
    13  	*apiclient.Node
    14  }
    15  
    16  func urlComponents(url string) (host string, schemas []string) {
    17  	p := strings.Split(url, "://")
    18  	if len(p) == 1 {
    19  		host = p[0]
    20  		schemas = []string{"http"}
    21  		return
    22  	}
    23  	host = p[1]
    24  	schemas = []string{p[0]}
    25  	return
    26  }
    27  
    28  // NewNode instantiates a new swagger HTTP client to an aeternity node. No
    29  // network connection is actually performed, this only sets up the HTTP client
    30  // code.
    31  func NewNode(nodeURL string, debug bool) *Node {
    32  	// create the transport
    33  	host, schemas := urlComponents(nodeURL)
    34  	transport := httptransport.New(host, "/v2", schemas)
    35  	transport.SetDebug(debug)
    36  	// create the API client, with the transport
    37  	openAPIClient := apiclient.New(transport, strfmt.Default)
    38  	aecli := &Node{
    39  		Node: openAPIClient,
    40  	}
    41  	return aecli
    42  }