Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: configurable hosts #89

Merged
merged 21 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/api_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type ApiNodesService Service
// Get all available nodes serving APIs.
func (s *ApiNodesService) All(ctx context.Context, query *Pagination) (*ApiNodesResponse, *http.Response, error) {
var responseStruct *ApiNodesResponse
resp, err := s.client.SendRequest(ctx, "GET", "api-nodes", query, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", "api-nodes", query, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand Down
2 changes: 1 addition & 1 deletion client/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type BlockchainService Service
// Get blockchain information.
func (s *BlockchainService) Info(ctx context.Context) (*BlockchainInfo, *http.Response, error) {
var responseStruct *BlockchainInfo
resp, err := s.client.SendRequest(ctx, "GET", "blockchain", nil, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", "blockchain", nil, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand Down
10 changes: 5 additions & 5 deletions client/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type BlocksService Service
// Get all blocks.
func (s *BlocksService) List(ctx context.Context, query *Pagination) (*Blocks, *http.Response, error) {
var responseStruct *Blocks
resp, err := s.client.SendRequest(ctx, "GET", "blocks", query, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", "blocks", query, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -34,7 +34,7 @@ func (s *BlocksService) Get(ctx context.Context, id int64) (*GetBlock, *http.Res
uri := fmt.Sprintf("blocks/%v", id)

var responseStruct *GetBlock
resp, err := s.client.SendRequest(ctx, "GET", uri, nil, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", uri, nil, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -46,7 +46,7 @@ func (s *BlocksService) Get(ctx context.Context, id int64) (*GetBlock, *http.Res
// Get the first block.
func (s *BlocksService) First(ctx context.Context) (*GetBlock, *http.Response, error) {
var responseStruct *GetBlock
resp, err := s.client.SendRequest(ctx, "GET", "blocks/first", nil, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", "blocks/first", nil, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -58,7 +58,7 @@ func (s *BlocksService) First(ctx context.Context) (*GetBlock, *http.Response, e
// Get the last block.
func (s *BlocksService) Last(ctx context.Context) (*GetBlock, *http.Response, error) {
var responseStruct *GetBlock
resp, err := s.client.SendRequest(ctx, "GET", "blocks/last", nil, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", "blocks/last", nil, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -72,7 +72,7 @@ func (s *BlocksService) Transactions(ctx context.Context, id int64, query *Pagin
uri := fmt.Sprintf("blocks/%v/transactions", id)

var responseStruct *GetBlockTransactions
resp, err := s.client.SendRequest(ctx, "GET", uri, query, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", uri, query, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand Down
55 changes: 36 additions & 19 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@ import (
"github.com/google/go-querystring/query"
)

const (
defaultBaseURL = "https://dwallets.ark.io/api/"
)
type Hosts struct {
API string
Transactions string
EVM string
}

type Client struct {
client *http.Client

BaseURL *url.URL
httpClient *http.Client
Hosts Hosts

common Service
common Service

ApiNodes *ApiNodesService
Blocks *BlocksService
Blockchain *BlockchainService
Commits *CommitsService
Blockchain *BlockchainService
Commits *CommitsService
Delegates *DelegatesService
Node *NodeService
Peers *PeersService
Expand All @@ -48,14 +49,15 @@ type Service struct {
client *Client
}

func NewClient(httpClient *http.Client) *Client {
func NewClient(httpClient *http.Client, hosts Hosts) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}

baseURL, _ := url.Parse(defaultBaseURL)

c := &Client{client: httpClient, BaseURL: baseURL}
c := &Client{
httpClient: httpClient,
Hosts: hosts,
}
c.common.client = c

c.ApiNodes = (*ApiNodesService)(&c.common)
Expand All @@ -73,13 +75,28 @@ func NewClient(httpClient *http.Client) *Client {
return c
}

func (c *Client) SendRequest(ctx context.Context, method string, urlStr string, queryString interface{}, body interface{}, model interface{}) (*http.Response, error) {
// Create a new HTTP request
if !strings.HasSuffix(c.BaseURL.Path, "/") {
return nil, fmt.Errorf("BaseURL must have a trailing slash, but %q does not", c.BaseURL)

func (c *Client) SendRequest(ctx context.Context, method string, endpoint string, queryString interface{}, body interface{}, model interface{}, hostType string) (*http.Response, error) {
var host string
switch hostType {
case "transactions":
host = c.Hosts.Transactions
case "evm":
host = c.Hosts.EVM
default:
host = c.Hosts.API
}

parsedHost, err := url.Parse(host)
if err != nil {
return nil, fmt.Errorf("invalid host URL: %v", err)
}

if !strings.HasSuffix(parsedHost.Path, "/") {
parsedHost.Path += "/"
}

u, err := c.BaseURL.Parse(urlStr)
u, err := parsedHost.Parse(endpoint)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -114,7 +131,7 @@ func (c *Client) SendRequest(ctx context.Context, method string, urlStr string,
req.Header.Set("Content-Type", "application/json")

// Execute the previously created HTTP request
resp, err := c.client.Do(req)
resp, err := c.httpClient.Do(req)

if err != nil {
select {
Expand Down
2 changes: 1 addition & 1 deletion client/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (s *CommitsService) GetCommit(ctx context.Context, height int) (*CommitResp
uri := fmt.Sprintf("commits/%d", height)

var responseStruct *CommitResponse
resp, err := s.client.SendRequest(ctx, "GET", uri, nil, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", uri, nil, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand Down
8 changes: 4 additions & 4 deletions client/delegates.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type DelegatesService Service
// Get all accounts.
func (s *DelegatesService) List(ctx context.Context, query *Pagination) (*Delegates, *http.Response, error) {
var responseStruct *Delegates
resp, err := s.client.SendRequest(ctx, "GET", "delegates", query, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", "delegates", query, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -34,7 +34,7 @@ func (s *DelegatesService) Get(ctx context.Context, id string) (*GetDelegate, *h
uri := fmt.Sprintf("delegates/%v", id)

var responseStruct *GetDelegate
resp, err := s.client.SendRequest(ctx, "GET", uri, nil, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", uri, nil, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -48,7 +48,7 @@ func (s *DelegatesService) Blocks(ctx context.Context, id string, query *Paginat
uri := fmt.Sprintf("delegates/%v/blocks", id)

var responseStruct *GetDelegateBlocks
resp, err := s.client.SendRequest(ctx, "GET", uri, query, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", uri, query, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -62,7 +62,7 @@ func (s *DelegatesService) Voters(ctx context.Context, id string, query *Paginat
uri := fmt.Sprintf("delegates/%v/voters", id)

var responseStruct *GetDelegateVoters
resp, err := s.client.SendRequest(ctx, "GET", uri, query, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", uri, query, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand Down
8 changes: 4 additions & 4 deletions client/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type NodeService Service
// Get the node status.
func (s *NodeService) Status(ctx context.Context) (*GetNodeStatus, *http.Response, error) {
var responseStruct *GetNodeStatus
resp, err := s.client.SendRequest(ctx, "GET", "node/status", nil, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", "node/status", nil, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -31,7 +31,7 @@ func (s *NodeService) Status(ctx context.Context) (*GetNodeStatus, *http.Respons
// Get the node syncing status.
func (s *NodeService) Syncing(ctx context.Context) (*GetNodeSyncing, *http.Response, error) {
var responseStruct *GetNodeSyncing
resp, err := s.client.SendRequest(ctx, "GET", "node/syncing", nil, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", "node/syncing", nil, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -43,7 +43,7 @@ func (s *NodeService) Syncing(ctx context.Context) (*GetNodeSyncing, *http.Respo
// Get the node configuration.
func (s *NodeService) Configuration(ctx context.Context) (*GetNodeConfiguration, *http.Response, error) {
var responseStruct *GetNodeConfiguration
resp, err := s.client.SendRequest(ctx, "GET", "node/configuration", nil, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", "node/configuration", nil, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -55,7 +55,7 @@ func (s *NodeService) Configuration(ctx context.Context) (*GetNodeConfiguration,
// Get the node fee statistics.
func (s *NodeService) Fees(ctx context.Context, days int) (*GetNodeFees, *http.Response, error) {
var responseStruct *GetNodeFees
resp, err := s.client.SendRequest(ctx, "GET", "node/fees", FeesRequest{days}, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", "node/fees", FeesRequest{days}, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand Down
4 changes: 2 additions & 2 deletions client/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type PeersService Service
// Get all peers.
func (s *PeersService) List(ctx context.Context, query *Pagination) (*Peers, *http.Response, error) {
var responseStruct *Peers
resp, err := s.client.SendRequest(ctx, "GET", "peers", query, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", "peers", query, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -34,7 +34,7 @@ func (s *PeersService) Get(ctx context.Context, ip string) (*GetPeer, *http.Resp
uri := fmt.Sprintf("peers/%v", ip)

var responseStruct *GetPeer
resp, err := s.client.SendRequest(ctx, "GET", uri, nil, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", uri, nil, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand Down
6 changes: 3 additions & 3 deletions client/rounds.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (s *RoundsService) Delegates(ctx context.Context, id int64) (*GetDelegates,
uri := fmt.Sprintf("rounds/%v/delegates", id)

var responseStruct *GetDelegates
resp, err := s.client.SendRequest(ctx, "GET", uri, nil, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", uri, nil, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -36,7 +36,7 @@ func (s *RoundsService) All(ctx context.Context, query *Pagination) (*GetRounds,
uri := "rounds"

var responseStruct *GetRounds
resp, err := s.client.SendRequest(ctx, "GET", uri, query, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", uri, query, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -50,7 +50,7 @@ func (s *RoundsService) Show(ctx context.Context, id int64) (*GetRound, *http.Re
uri := fmt.Sprintf("rounds/%v", id)

var responseStruct *GetRound
resp, err := s.client.SendRequest(ctx, "GET", uri, nil, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", uri, nil, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand Down
12 changes: 8 additions & 4 deletions client/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -45,11 +44,16 @@ func setupTest() (client *Client, mux *http.ServeMux, serverURL string, teardown
// server is a test HTTP server used to provide mock API responses.
server := httptest.NewServer(apiHandler)

hosts := Hosts{
API: server.URL + baseURLPath + "/",
Transactions: server.URL + "/tx/api/",
EVM: server.URL + "/evm/api/",
}


// client is the Ark client being tested and is
// configured to use test server.
client = NewClient(nil)
url, _ := url.Parse(server.URL + baseURLPath + "/")
client.BaseURL = url
client = NewClient(nil, hosts)

return client, mux, server.URL, server.Close
}
Expand Down
16 changes: 8 additions & 8 deletions client/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type TransactionsService Service
// Get all transactions.
func (s *TransactionsService) List(ctx context.Context, query *Pagination) (*Transactions, *http.Response, error) {
var responseStruct *Transactions
resp, err := s.client.SendRequest(ctx, "GET", "transactions", query, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", "transactions", query, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -32,7 +32,7 @@ func (s *TransactionsService) List(ctx context.Context, query *Pagination) (*Tra
// Create a new transaction.
func (s *TransactionsService) Create(ctx context.Context, body *CreateTransactionRequest) (*CreateTransaction, *http.Response, error) {
var responseStruct *CreateTransaction
resp, err := s.client.SendRequest(ctx, "POST", "transactions", nil, body, &responseStruct)
resp, err := s.client.SendRequest(ctx, "POST", "transactions", nil, body, &responseStruct, "transactions")

if err != nil {
return nil, resp, err
Expand All @@ -46,7 +46,7 @@ func (s *TransactionsService) Get(ctx context.Context, id string) (*GetTransacti
uri := fmt.Sprintf("transactions/%v", id)

var responseStruct *GetTransaction
resp, err := s.client.SendRequest(ctx, "GET", uri, nil, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", uri, nil, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -58,7 +58,7 @@ func (s *TransactionsService) Get(ctx context.Context, id string) (*GetTransacti
// Get all unconfirmed transactions.
func (s *TransactionsService) ListUnconfirmed(ctx context.Context, query *Pagination) (*Transactions, *http.Response, error) {
var responseStruct *Transactions
resp, err := s.client.SendRequest(ctx, "GET", "transactions/unconfirmed", query, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", "transactions/unconfirmed", query, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -72,7 +72,7 @@ func (s *TransactionsService) GetUnconfirmed(ctx context.Context, id string) (*G
uri := fmt.Sprintf("transactions/unconfirmed/%v", id)

var responseStruct *GetTransaction
resp, err := s.client.SendRequest(ctx, "GET", uri, nil, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", uri, nil, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -84,7 +84,7 @@ func (s *TransactionsService) GetUnconfirmed(ctx context.Context, id string) (*G
// Get a list of valid transaction types.
func (s *TransactionsService) Types(ctx context.Context) (*TransactionTypes, *http.Response, error) {
var responseStruct *TransactionTypes
resp, err := s.client.SendRequest(ctx, "GET", "transactions/types", nil, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", "transactions/types", nil, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -96,7 +96,7 @@ func (s *TransactionsService) Types(ctx context.Context) (*TransactionTypes, *ht
// Get a list of static transaction fees.
func (s *TransactionsService) Fees(ctx context.Context) (*TransactionFees, *http.Response, error) {
var responseStruct *TransactionFees
resp, err := s.client.SendRequest(ctx, "GET", "transactions/fees", nil, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", "transactions/fees", nil, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand All @@ -108,7 +108,7 @@ func (s *TransactionsService) Fees(ctx context.Context) (*TransactionFees, *http
// Get the list of transaction schemas.
func (s *TransactionsService) Schemas(ctx context.Context) (*TransactionSchemas, *http.Response, error) {
var responseStruct *TransactionSchemas
resp, err := s.client.SendRequest(ctx, "GET", "transactions/schemas", nil, nil, &responseStruct)
resp, err := s.client.SendRequest(ctx, "GET", "transactions/schemas", nil, nil, &responseStruct, "api")

if err != nil {
return nil, resp, err
Expand Down
Loading
Loading