Skip to content

Commit

Permalink
apiclient: simplify constructor calls, renamed to apiclient.New()
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui authored and p4u committed Mar 7, 2024
1 parent 5c3db8f commit cf44db5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 28 deletions.
19 changes: 17 additions & 2 deletions apiclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,23 @@ type HTTPclient struct {
retries int
}

// NewHTTPclient creates a new HTTP(s) API Vocdoni client.
func NewHTTPclient(addr *url.URL, bearerToken *uuid.UUID) (*HTTPclient, error) {
// New connects to the API host with a random bearer token and returns the handle
func New(host string) (*HTTPclient, error) {
token := uuid.New()
return NewWithBearer(host, &token)
}

// NewWithBearer connects to the API host with a random bearer token and returns the handle
func NewWithBearer(host string, bearerToken *uuid.UUID) (*HTTPclient, error) {
hostURL, err := url.Parse(host)
if err != nil {
return nil, err
}
return NewWithURLAndBearer(hostURL, bearerToken)
}

// NewWithURLAndBearer creates a new HTTP(s) API Vocdoni client.
func NewWithURLAndBearer(addr *url.URL, bearerToken *uuid.UUID) (*HTTPclient, error) {
tr := &http.Transport{
IdleConnTimeout: DefaultTimeout,
DisableCompression: false,
Expand Down
6 changes: 1 addition & 5 deletions cmd/cli/vocdonicli.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,8 @@ func NewVocdoniCLI(configFile, host string) (*VocdoniCLI, error) {
if cfg.Host == nil {
return nil, fmt.Errorf("no API server host configured")
}
hostURL, err := url.Parse(host)
if err != nil {
return nil, err
}

api, err := apiclient.NewHTTPclient(hostURL, cfg.Token)
api, err := apiclient.NewWithBearer(host, cfg.Token)
if err != nil {
return nil, err
}
Expand Down
16 changes: 1 addition & 15 deletions cmd/end2endtest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package main
import (
"encoding/hex"
"fmt"
"net/url"
"os"
"path/filepath"
"sync"
"time"

"github.com/google/uuid"
flag "github.com/spf13/pflag"
vapi "go.vocdoni.io/dvote/api"
"go.vocdoni.io/dvote/apiclient"
Expand Down Expand Up @@ -206,18 +204,6 @@ func privKeyToSigner(key string) (*ethereum.SignKeys, error) {
return skey, nil
}

// NewAPIclient connects to the API host and returns the handle
func NewAPIclient(host string) (*apiclient.HTTPclient, error) {
hostURL, err := url.Parse(host)
if err != nil {
log.Fatal(err)
}
log.Debugf("connecting to %s", hostURL.String())
token := uuid.New()

return apiclient.NewHTTPclient(hostURL, &token)
}

func main() {
fmt.Fprintf(os.Stderr, "vocdoni version %q\n", internal.Version)

Expand All @@ -233,7 +219,7 @@ func main() {
if err := createSignKeys(c); err != nil {
log.Fatal(err)
}
return NewAPIclient(c.host)
return apiclient.New(c.host)
}

runTests := func(op operation, c *config, wg *sync.WaitGroup) {
Expand Down
7 changes: 1 addition & 6 deletions vocone/vocone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package vocone
import (
"context"
"fmt"
"net/url"
"testing"
"time"

qt "github.com/frankban/quicktest"
"github.com/google/uuid"
"go.vocdoni.io/dvote/apiclient"
"go.vocdoni.io/dvote/crypto/ethereum"
"go.vocdoni.io/dvote/test/testcommon/testvoteproof"
Expand Down Expand Up @@ -44,10 +42,7 @@ func TestVocone(t *testing.T) {
err = vc.SetBulkTxCosts(0, true)
qt.Assert(t, err, qt.IsNil)

u, err := url.Parse(fmt.Sprintf("http://127.0.0.1:%d/v2", port))
qt.Assert(t, err, qt.IsNil)
token := uuid.New()
cli, err := apiclient.NewHTTPclient(u, &token)
cli, err := apiclient.New(fmt.Sprintf("http://127.0.0.1:%d/v2", port))
qt.Assert(t, err, qt.IsNil)
err = cli.SetAccount(fmt.Sprintf("%x", account.PrivateKey()))
qt.Assert(t, err, qt.IsNil)
Expand Down

0 comments on commit cf44db5

Please sign in to comment.