From 4ca74af2fa516465692fd826ca3c04620c04bbd4 Mon Sep 17 00:00:00 2001 From: Frozen Date: Wed, 14 Nov 2018 13:52:59 +0300 Subject: [PATCH] Fix client path (#52) --- pkg/client/addresses.go | 66 ++++++++++++++++++++---- pkg/client/addresses_test.go | 20 +++---- pkg/client/alias.go | 28 ++++++++-- pkg/client/alias_test.go | 2 +- pkg/client/assets.go | 63 ++++++++++++++++++---- pkg/client/assets_test.go | 2 +- pkg/client/blocks.go | 34 ++++++++++-- pkg/client/client.go | 15 ++++++ pkg/client/client_test.go | 6 +++ pkg/client/consensus.go | 42 ++++++++++++--- pkg/client/consensus_integration_test.go | 4 +- pkg/client/consensus_test.go | 4 +- pkg/client/peers.go | 43 ++++++++++++--- pkg/client/transactions.go | 35 +++++++++++-- pkg/client/utils.go | 56 +++++++++++++++++--- pkg/client/wallet.go | 8 ++- 16 files changed, 356 insertions(+), 72 deletions(-) diff --git a/pkg/client/addresses.go b/pkg/client/addresses.go index 533c9b552..d27826a30 100644 --- a/pkg/client/addresses.go +++ b/pkg/client/addresses.go @@ -31,9 +31,14 @@ type AddressesBalance struct { // Balance returns account's balance by its address func (a *Addresses) Balance(ctx context.Context, address proto.Address) (*AddressesBalance, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("addresses/balance/%s", address.String())) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/addresses/balance/%s", a.options.BaseUrl, address.String()), + url.String(), nil) if err != nil { return nil, nil, err @@ -58,9 +63,14 @@ type AddressesBalanceDetails struct { // BalanceDetails returns account's detail balance by its address func (a *Addresses) BalanceDetails(ctx context.Context, address proto.Address) (*AddressesBalanceDetails, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/addresses/balance/details/%s", address.String())) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/addresses/balance/details/%s", a.options.BaseUrl, address.String()), + url.String(), nil) if err != nil { return nil, nil, err @@ -83,9 +93,14 @@ type AddressesScriptInfo struct { // ScriptInfo gets account's script information func (a *Addresses) ScriptInfo(ctx context.Context, address proto.Address) (*AddressesScriptInfo, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/addresses/scriptInfo/%s", address.String())) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/addresses/scriptInfo/%s", a.options.BaseUrl, address.String()), + url.String(), nil) if err != nil { return nil, nil, err @@ -102,9 +117,13 @@ func (a *Addresses) ScriptInfo(ctx context.Context, address proto.Address) (*Add // Get wallet accounts addresses func (a *Addresses) Addresses(ctx context.Context) ([]proto.Address, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, "/addresses") + if err != nil { + return nil, nil, err + } req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/addresses", a.options.BaseUrl), + url.String(), nil) if err != nil { return nil, nil, err @@ -126,9 +145,13 @@ type AddressesValidate struct { // Check whether address is valid or not func (a *Addresses) Validate(ctx context.Context, address proto.Address) (*AddressesValidate, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/addresses/validate/%s", address.String())) + if err != nil { + return nil, nil, err + } req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/addresses/validate/%s", a.options.BaseUrl, address.String()), + url.String(), nil) if err != nil { return nil, nil, err @@ -151,9 +174,13 @@ type AddressesEffectiveBalance struct { // Account's balance func (a *Addresses) EffectiveBalance(ctx context.Context, address proto.Address) (*AddressesEffectiveBalance, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/addresses/effectiveBalance/%s", address.String())) + if err != nil { + return nil, nil, err + } req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/addresses/effectiveBalance/%s", a.options.BaseUrl, address.String()), + url.String(), nil) if err != nil { return nil, nil, err @@ -174,9 +201,13 @@ type addressesPublicKey struct { // Generate address from public key func (a *Addresses) PublicKey(ctx context.Context, publicKey string) (*proto.Address, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/addresses/publicKey/%s", publicKey)) + if err != nil { + return nil, nil, err + } req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/addresses/publicKey/%s", a.options.BaseUrl, publicKey), + url.String(), nil) if err != nil { return nil, nil, err @@ -207,9 +238,14 @@ func (a *Addresses) SignText(ctx context.Context, address proto.Address, message return nil, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/addresses/signText/%s", address.String())) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "POST", - fmt.Sprintf("%s/addresses/signText/%s", a.options.BaseUrl, address.String()), + url.String(), strings.NewReader(message)) if err != nil { return nil, nil, err @@ -242,6 +278,11 @@ func (a *Addresses) VerifyText(ctx context.Context, address proto.Address, body return false, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/addresses/verifyText/%s", address.String())) + if err != nil { + return false, nil, err + } + bodyBytes, err := json.Marshal(body) if err != nil { return false, nil, err @@ -249,7 +290,7 @@ func (a *Addresses) VerifyText(ctx context.Context, address proto.Address, body req, err := http.NewRequest( "POST", - fmt.Sprintf("%s/addresses/verifyText/%s", a.options.BaseUrl, address.String()), + url.String(), bytes.NewReader(bodyBytes)) if err != nil { return false, nil, err @@ -277,9 +318,14 @@ type BalanceAfterConfirmations struct { func (a *Addresses) BalanceAfterConfirmations( ctx context.Context, address proto.Address, confirmations uint64) (*BalanceAfterConfirmations, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/addresses/balance/%s/%d", address.String(), confirmations)) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/addresses/balance/%s/%d", a.options.BaseUrl, address.String(), confirmations), + url.String(), nil) if err != nil { return nil, nil, err diff --git a/pkg/client/addresses_test.go b/pkg/client/addresses_test.go index 8fe30b45a..e8236c70b 100644 --- a/pkg/client/addresses_test.go +++ b/pkg/client/addresses_test.go @@ -21,7 +21,7 @@ var addressesBalanceDetailsJson = ` func TestAddresses_BalanceDetails(t *testing.T) { address, _ := proto.NewAddressFromString("3NBVqYXrapgJP9atQccdBPAgJPwHDKkh6A8") client, err := NewClient(Options{ - BaseUrl: "https://testnode1.wavesnodes.com", + BaseUrl: "https://testnode1.wavesnodes.com/", Client: NewMockHttpRequestFromString(addressesBalanceDetailsJson, 200), }) require.NoError(t, err) @@ -51,7 +51,7 @@ var addressesScriptInfoJson = ` func TestAddresses_ScriptInfo(t *testing.T) { address, _ := proto.NewAddressFromString("3NBVqYXrapgJP9atQccdBPAgJPwHDKkh6A8") client, err := NewClient(Options{ - BaseUrl: "https://testnode1.wavesnodes.com", + BaseUrl: "https://testnode1.wavesnodes.com/", Client: NewMockHttpRequestFromString(addressesScriptInfoJson, 200), }) require.NoError(t, err) @@ -75,7 +75,7 @@ var addressesAddressesJson = ` func TestAddresses_Addresses(t *testing.T) { address, _ := proto.NewAddressFromString("3MzemqBzJ9h844PparHU1EzGC5SQmtH5pNp") client, err := NewClient(Options{ - BaseUrl: "https://testnode1.wavesnodes.com", + BaseUrl: "https://testnode1.wavesnodes.com/", Client: NewMockHttpRequestFromString(addressesAddressesJson, 200), }) require.NoError(t, err) @@ -96,7 +96,7 @@ var addressesValidateJson = ` func TestAddresses_Validate(t *testing.T) { address, _ := proto.NewAddressFromString("3P3oWUH9oXRqiByBG7g9hYSDpCFxcT2wTBS") client, err := NewClient(Options{ - BaseUrl: "https://testnode1.wavesnodes.com", + BaseUrl: "https://testnode1.wavesnodes.com/", Client: NewMockHttpRequestFromString(addressesValidateJson, 200), }) require.NoError(t, err) @@ -121,7 +121,7 @@ var addressesBalanceJson = ` func TestAddresses_Balance(t *testing.T) { address, _ := proto.NewAddressFromString("3NBVqYXrapgJP9atQccdBPAgJPwHDKkh6A8") client, err := NewClient(Options{ - BaseUrl: "https://testnode1.wavesnodes.com", + BaseUrl: "https://testnode1.wavesnodes.com/", Client: NewMockHttpRequestFromString(addressesBalanceJson, 200), }) require.NoError(t, err) @@ -147,7 +147,7 @@ var addressesEffectiveBalance = ` func TestAddresses_EffectiveBalance(t *testing.T) { address, _ := proto.NewAddressFromString("3NBVqYXrapgJP9atQccdBPAgJPwHDKkh6A8") client, err := NewClient(Options{ - BaseUrl: "https://testnode1.wavesnodes.com", + BaseUrl: "https://testnode1.wavesnodes.com/", Client: NewMockHttpRequestFromString(addressesEffectiveBalance, 200), }) require.NoError(t, err) @@ -173,7 +173,7 @@ func TestAddresses_PublicKey(t *testing.T) { pubKey := "AF9HLq2Rsv2fVfLPtsWxT7Y3S9ZTv6Mw4ZTp8K8LNdEp" address, _ := proto.NewAddressFromString("3Mr5af3Y7r7gQej3tRtugYbKaPr5qYps2ei") client, err := NewClient(Options{ - BaseUrl: "https://testnode1.wavesnodes.com", + BaseUrl: "https://testnode1.wavesnodes.com/", Client: NewMockHttpRequestFromString(addressPublicKeyJson, 200), }) body, resp, err := @@ -199,7 +199,7 @@ func TestAddresses_SignText(t *testing.T) { sign, _ := crypto.NewSignatureFromBase58("RP742bUjfrzWcXhnmkbim2dWk9mSUcPcmn77EcsD5t2TBUZqZGe8Vk211hAYbW4FNxWtWqcCmR1Trv8gUXKN6if") text := "some-text" client, err := NewClient(Options{ - BaseUrl: "https://testnode1.wavesnodes.com", + BaseUrl: "https://testnode1.wavesnodes.com/", ApiKey: "ApiKey", Client: NewMockHttpRequestFromString(addressSignTextJson, 200), }) @@ -234,7 +234,7 @@ func TestAddresses_VerifyText(t *testing.T) { Signature: sign, } client, err := NewClient(Options{ - BaseUrl: "https://testnode1.wavesnodes.com", + BaseUrl: "https://testnode1.wavesnodes.com/", ApiKey: "apiKey", Client: NewMockHttpRequestFromString(addressVerifyTextJson, 200), }) @@ -258,7 +258,7 @@ func TestAddresses_BalanceAfterConfirmations(t *testing.T) { address, _ := proto.NewAddressFromString("3NBVqYXrapgJP9atQccdBPAgJPwHDKkh6A8") confirmations := uint64(1) client, err := NewClient(Options{ - BaseUrl: "https://testnode1.wavesnodes.com", + BaseUrl: "https://testnode1.wavesnodes.com/", Client: NewMockHttpRequestFromString(addressBalanceAfterConfirmationsJson, 200), }) require.NoError(t, err) diff --git a/pkg/client/alias.go b/pkg/client/alias.go index d43ec69b8..7244f4ace 100644 --- a/pkg/client/alias.go +++ b/pkg/client/alias.go @@ -22,9 +22,14 @@ func NewAlias(options Options) *Alias { } func (a *Alias) Get(ctx context.Context, alias string) (proto.Address, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/alias/by-alias/%s", alias)) + if err != nil { + return proto.Address{}, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/alias/by-alias/%s", a.options.BaseUrl, alias), + url.String(), nil) if err != nil { return proto.Address{}, nil, err @@ -42,9 +47,14 @@ func (a *Alias) Get(ctx context.Context, alias string) (proto.Address, *Response } func (a *Alias) GetByAddress(ctx context.Context, address proto.Address) ([]*proto.Alias, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/alias/by-address/%s", address.String())) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/alias/by-address/%s", a.options.BaseUrl, address.String()), + url.String(), nil) if err != nil { return nil, nil, err @@ -82,6 +92,11 @@ func (a *Alias) Create(ctx context.Context, createReq AliasCreateReq) (*CreateAl return nil, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/alias/create") + if err != nil { + return nil, nil, err + } + bts, err := json.Marshal(createReq) if err != nil { return nil, nil, err @@ -89,7 +104,7 @@ func (a *Alias) Create(ctx context.Context, createReq AliasCreateReq) (*CreateAl req, err := http.NewRequest( "POST", - fmt.Sprintf("%s/alias/create", a.options.BaseUrl), + url.String(), bytes.NewReader(bts)) if err != nil { return nil, nil, err @@ -120,9 +135,14 @@ func (a *Alias) Broadcast(ctx context.Context, broadcastReq AliasBroadcastReq) ( return nil, nil, err } + url, err := joinUrl(a.options.BaseUrl, "/alias/broadcast/create") + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "POST", - fmt.Sprintf("%s/alias/broadcast/create", a.options.BaseUrl), + url.String(), bytes.NewReader(bts)) if err != nil { return nil, nil, err diff --git a/pkg/client/alias_test.go b/pkg/client/alias_test.go index 40763bea6..09fe99c1f 100644 --- a/pkg/client/alias_test.go +++ b/pkg/client/alias_test.go @@ -136,7 +136,7 @@ func TestAlias_Broadcast(t *testing.T) { require.Nil(t, err) client, err := NewClient(Options{ - BaseUrl: "https://testnode1.wavesnodes.com", + BaseUrl: "https://testnode1.wavesnodes.com/", Client: NewMockHttpRequestFromString(broadcastResp, 200), }) diff --git a/pkg/client/assets.go b/pkg/client/assets.go index 16823e740..8c21cb2ca 100644 --- a/pkg/client/assets.go +++ b/pkg/client/assets.go @@ -39,9 +39,14 @@ type AssetsBalance struct { // Provides detailed information about given asset func (a *Assets) BalanceByAddress(ctx context.Context, address proto.Address) (*AssetsBalances, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/assets/balance/%s", address.String())) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/assets/balance/%s", a.options.BaseUrl, address.String()), + url.String(), nil) if err != nil { return nil, nil, err @@ -64,9 +69,14 @@ type AssetsBalanceAndAsset struct { // Account's balance by given asset func (a *Assets) BalanceByAddressAndAsset(ctx context.Context, address proto.Address, assetId crypto.Digest) (*AssetsBalanceAndAsset, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/assets/balance/%s/%s", address.String(), assetId.String())) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/assets/balance/%s/%s", a.options.BaseUrl, address.String(), assetId.String()), + url.String(), nil) if err != nil { return nil, nil, err @@ -96,9 +106,14 @@ type AssetsDetail struct { // Provides detailed information about given asset func (a *Assets) Details(ctx context.Context, assetId crypto.Digest) (*AssetsDetail, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/assets/details/%s", assetId.String())) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/assets/details/%s", a.options.BaseUrl, assetId.String()), + url.String(), nil) if err != nil { return nil, nil, err @@ -117,9 +132,14 @@ type AssetsDistribution map[string]uint64 // Asset balance distribution by account func (a *Assets) Distribution(ctx context.Context, assetId crypto.Digest) (AssetsDistribution, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/assets/%s/distribution", assetId.String())) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/assets/%s/distribution", a.options.BaseUrl, assetId.String()), + url.String(), nil) if err != nil { return nil, nil, err @@ -162,6 +182,11 @@ func (a *Assets) Issue(ctx context.Context, issueReq AssetsIssueReq) (*AssetsIss return nil, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/assets/issue") + if err != nil { + return nil, nil, err + } + if issueReq.Timestamp == 0 { issueReq.Timestamp = NewTimestampFromTime(time.Now()) } @@ -173,7 +198,7 @@ func (a *Assets) Issue(ctx context.Context, issueReq AssetsIssueReq) (*AssetsIss req, err := http.NewRequest( "POST", - fmt.Sprintf("%s/assets/issue", a.options.BaseUrl), + url.String(), bytes.NewReader(bts)) if err != nil { return nil, nil, err @@ -211,6 +236,11 @@ func (a *Assets) MassTransfer(ctx context.Context, transfersReq AssetsMassTransf return nil, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/assets/masstransfer") + if err != nil { + return nil, nil, err + } + if transfersReq.Timestamp == 0 { transfersReq.Timestamp = NewTimestampFromTime(time.Now()) } @@ -225,7 +255,7 @@ func (a *Assets) MassTransfer(ctx context.Context, transfersReq AssetsMassTransf req, err := http.NewRequest( "POST", - fmt.Sprintf("%s/assets/masstransfer", a.options.BaseUrl), + url.String(), bytes.NewReader(bts)) if err != nil { return nil, nil, err @@ -256,6 +286,11 @@ func (a *Assets) Sponsor(ctx context.Context, sponsorReq AssetsSponsorReq) (*pro return nil, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/assets/sponsor") + if err != nil { + return nil, nil, err + } + bts, err := json.Marshal(sponsorReq) if err != nil { return nil, nil, err @@ -263,7 +298,7 @@ func (a *Assets) Sponsor(ctx context.Context, sponsorReq AssetsSponsorReq) (*pro req, err := http.NewRequest( "POST", - fmt.Sprintf("%s/assets/sponsor", a.options.BaseUrl), + url.String(), bytes.NewReader(bts)) if err != nil { return nil, nil, err @@ -298,6 +333,11 @@ func (a *Assets) Transfer(ctx context.Context, transferReq AssetsTransferReq) (* return nil, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/assets/transfer") + if err != nil { + return nil, nil, err + } + bts, err := json.Marshal(transferReq) if err != nil { return nil, nil, err @@ -305,7 +345,7 @@ func (a *Assets) Transfer(ctx context.Context, transferReq AssetsTransferReq) (* req, err := http.NewRequest( "POST", - fmt.Sprintf("%s/assets/transfer", a.options.BaseUrl), + url.String(), bytes.NewReader(bts)) if err != nil { return nil, nil, err @@ -341,9 +381,14 @@ func (a *Assets) Burn(ctx context.Context, burnReq AssetsBurnReq) (*proto.BurnV1 return nil, nil, err } + url, err := joinUrl(a.options.BaseUrl, "/assets/burn") + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "POST", - fmt.Sprintf("%s/assets/burn", a.options.BaseUrl), + url.String(), bytes.NewReader(bts)) if err != nil { return nil, nil, err diff --git a/pkg/client/assets_test.go b/pkg/client/assets_test.go index 31f036a0d..437265be7 100644 --- a/pkg/client/assets_test.go +++ b/pkg/client/assets_test.go @@ -312,7 +312,7 @@ var assetsBurnJson = ` func TestAssets_Burn(t *testing.T) { client, err := NewClient(Options{ Client: NewMockHttpRequestFromString(assetsBurnJson, 200), - BaseUrl: "https://testnode1.wavesnodes.com", + BaseUrl: "https://testnode1.wavesnodes.com/", ApiKey: "apiKey", }) require.Nil(t, err) diff --git a/pkg/client/blocks.go b/pkg/client/blocks.go index 7704dc752..61bc3c4b3 100644 --- a/pkg/client/blocks.go +++ b/pkg/client/blocks.go @@ -23,7 +23,12 @@ type BlocksHeight struct { } func (a *Blocks) Height(ctx context.Context) (*BlocksHeight, *Response, error) { - req, err := http.NewRequest("GET", fmt.Sprintf("%s/blocks/height", a.options.BaseUrl), nil) + url, err := joinUrl(a.options.BaseUrl, "/blocks/height") + if err != nil { + return nil, nil, err + } + + req, err := http.NewRequest("GET", url.String(), nil) if err != nil { return nil, nil, err } @@ -36,9 +41,13 @@ func (a *Blocks) Height(ctx context.Context) (*BlocksHeight, *Response, error) { } func (a *Blocks) HeightBySignature(ctx context.Context, signature string) (*BlocksHeight, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/blocks/height/%s", signature)) + if err != nil { + return nil, nil, err + } req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/blocks/height/%s", a.options.BaseUrl, signature), + url.String(), nil) if err != nil { return nil, nil, err @@ -72,9 +81,14 @@ type NxtConsensus struct { } func (a *Blocks) HeadersAt(ctx context.Context, height uint64) (*Headers, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/blocks/headers/at/%d", height)) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/blocks/headers/at/%d", a.options.BaseUrl, height), + url.String(), nil) if err != nil { return nil, nil, err @@ -90,9 +104,14 @@ func (a *Blocks) HeadersAt(ctx context.Context, height uint64) (*Headers, *Respo } func (a *Blocks) HeadersLast(ctx context.Context) (*Headers, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, "/blocks/headers/last") + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/blocks/headers/last", a.options.BaseUrl), + url.String(), nil) if err != nil { return nil, nil, err @@ -108,9 +127,14 @@ func (a *Blocks) HeadersLast(ctx context.Context) (*Headers, *Response, error) { } func (a *Blocks) HeadersSeq(ctx context.Context, from uint64, to uint64) ([]*Headers, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/blocks/headers/seq/%d/%d", from, to)) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/blocks/headers/seq/%d/%d", a.options.BaseUrl, from, to), + url.String(), nil) if err != nil { return nil, nil, err diff --git a/pkg/client/client.go b/pkg/client/client.go index 0edd12b9a..c42c6120f 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -7,6 +7,7 @@ import ( "io" "io/ioutil" "net/http" + "net/url" "time" ) @@ -146,3 +147,17 @@ func doHttp(ctx context.Context, options Options, req *http.Request, v interface return response, err } + +func joinUrl(baseRaw string, pathRaw string) (*url.URL, error) { + base, err := url.Parse(baseRaw) + if err != nil { + return nil, err + } + + path, err := url.Parse(pathRaw) + if err != nil { + return nil, err + } + + return base.ResolveReference(path), nil +} diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index ca2f3a46e..138950f8f 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -68,3 +68,9 @@ func TestMockHttpRequest(t *testing.T) { assert.Equal(t, url, resp.Request.URL.String()) assert.Equal(t, "123456", resp.Request.Header.Get("ApiKey")) } + +func TestJoinUrl(t *testing.T) { + url, err := joinUrl("https://wavesplatform.com", "path") + require.NoError(t, err) + assert.Equal(t, "https://wavesplatform.com/path", url.String()) +} diff --git a/pkg/client/consensus.go b/pkg/client/consensus.go index ee9184b52..1c13f5069 100644 --- a/pkg/client/consensus.go +++ b/pkg/client/consensus.go @@ -30,9 +30,14 @@ func (a Consensus) GeneratingBalance(ctx context.Context, address proto.Address) return nil, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/consensus/generatingbalance/%s", address.String())) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/consensus/generatingbalance/%s", a.options.BaseUrl, address.String()), + url.String(), nil) if err != nil { return nil, nil, err @@ -55,9 +60,14 @@ func (a *Consensus) GenerationSignatureByBlock(ctx context.Context, blockID stri return "", nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/consensus/generationsignature/%s", blockID)) + if err != nil { + return "", nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/consensus/generationsignature/%s", a.options.BaseUrl, blockID), + url.String(), nil) if err != nil { return "", nil, err @@ -80,9 +90,14 @@ func (a *Consensus) BaseTargetByBlock(ctx context.Context, blockID string) (uint return 0, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/consensus/basetarget/%s", blockID)) + if err != nil { + return 0, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/consensus/basetarget/%s", a.options.BaseUrl, blockID), + url.String(), nil) if err != nil { return 0, nil, err @@ -110,9 +125,14 @@ func (a *Consensus) BaseTarget(ctx context.Context) (*ConsensusBaseTarget, *Resp return nil, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/consensus/basetarget") + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/consensus/basetarget", a.options.BaseUrl), + url.String(), nil) if err != nil { return nil, nil, err @@ -135,9 +155,14 @@ func (a *Consensus) Algo(ctx context.Context) (string, *Response, error) { return "", nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/consensus/algo") + if err != nil { + return "", nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/consensus/algo", a.options.BaseUrl), + url.String(), nil) if err != nil { return "", nil, err @@ -160,9 +185,14 @@ func (a *Consensus) GenerationSignature(ctx context.Context) (string, *Response, return "", nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/consensus/generationsignature") + if err != nil { + return "", nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/consensus/generationsignature", a.options.BaseUrl), + url.String(), nil) if err != nil { return "", nil, err diff --git a/pkg/client/consensus_integration_test.go b/pkg/client/consensus_integration_test.go index 50faef640..2c6d9e744 100644 --- a/pkg/client/consensus_integration_test.go +++ b/pkg/client/consensus_integration_test.go @@ -42,7 +42,7 @@ func TestConsensusIntegration_GenerationSignatureByBlock(t *testing.T) { ApiKey: apiKey, }) _, resp, err := - client.Consensus.GenerationSignatureByBlock(context.Background(), "3Z9W6dX3iAqyhv2gsE1WRRd5yLYdtjojLzNSXEFZNuVs21hkuNUmhqTNLqrcGnERJMaPtrfvag4AjQpjykvQM13a") + client.Consensus.GenerationSignatureByBlock(context.Background(), "4rnYtWNE8WV4heso4q86Uwbcf1XZR5ShfszaKzyRg7aELP2Su3sFUhcCrQCyBA9SbE4T8pkd2AnLKnwBHwhUKaDq") require.Nil(t, err) require.NotNil(t, resp) assert.Equal(t, 200, resp.StatusCode) @@ -59,7 +59,7 @@ func TestConsensusIntegration_BaseTargetByBlock(t *testing.T) { ApiKey: apiKey, }) _, resp, err := - client.Consensus.BaseTargetByBlock(context.Background(), "3Z9W6dX3iAqyhv2gsE1WRRd5yLYdtjojLzNSXEFZNuVs21hkuNUmhqTNLqrcGnERJMaPtrfvag4AjQpjykvQM13a") + client.Consensus.BaseTargetByBlock(context.Background(), "4rnYtWNE8WV4heso4q86Uwbcf1XZR5ShfszaKzyRg7aELP2Su3sFUhcCrQCyBA9SbE4T8pkd2AnLKnwBHwhUKaDq") require.Nil(t, err) require.NotNil(t, resp) assert.Equal(t, 200, resp.StatusCode) diff --git a/pkg/client/consensus_test.go b/pkg/client/consensus_test.go index e3d53a7ab..00a37cb17 100644 --- a/pkg/client/consensus_test.go +++ b/pkg/client/consensus_test.go @@ -100,7 +100,7 @@ func TestConsensus_Algo(t *testing.T) { client, err := NewClient(Options{ Client: NewMockHttpRequestFromString(consensusAlgoJson, 200), ApiKey: "ApiKey", - BaseUrl: "https://testnode1.wavesnodes.com", + BaseUrl: "https://testnode1.wavesnodes.com/", }) require.Nil(t, err) body, resp, err := @@ -120,7 +120,7 @@ func TestConsensus_GenerationSignature(t *testing.T) { client, _ := NewClient(Options{ Client: NewMockHttpRequestFromString(consensusGenerationSignatureJson, 200), ApiKey: "ApiKey", - BaseUrl: "https://testnode1.wavesnodes.com", + BaseUrl: "https://testnode1.wavesnodes.com/", }) body, resp, err := client.Consensus.GenerationSignature(context.Background()) diff --git a/pkg/client/peers.go b/pkg/client/peers.go index 4fdb3b12a..36ed7cafa 100644 --- a/pkg/client/peers.go +++ b/pkg/client/peers.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "fmt" "net/http" "github.com/wavesplatform/gowaves/pkg/proto" @@ -34,9 +33,14 @@ func (a *Peers) All(ctx context.Context) ([]*PeerAllRow, *Response, error) { return nil, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/peers/all") + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/peers/all", a.options.BaseUrl), + url.String(), nil) if err != nil { return nil, nil, err @@ -71,9 +75,14 @@ func (a *Peers) Connected(ctx context.Context) ([]*PeersConnectedRow, *Response, return nil, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/peers/connected") + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/peers/connected", a.options.BaseUrl), + url.String(), nil) if err != nil { return nil, nil, err @@ -101,9 +110,14 @@ func (a *Peers) Blacklisted(ctx context.Context) ([]*PeersBlacklistedRow, *Respo return nil, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/peers/blacklisted") + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/peers/blacklisted", a.options.BaseUrl), + url.String(), nil) if err != nil { return nil, nil, err @@ -130,9 +144,14 @@ func (a *Peers) Suspended(ctx context.Context) ([]*PeersSuspendedRow, *Response, return nil, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/peers/suspended") + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/peers/suspended", a.options.BaseUrl), + url.String(), nil) if err != nil { return nil, nil, err @@ -159,6 +178,11 @@ func (a *Peers) Connect(ctx context.Context, host string, port uint16) (*PeersCo return nil, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/peers/connect") + if err != nil { + return nil, nil, err + } + bts, err := json.Marshal(map[string]interface{}{"host": host, "port": port}) if err != nil { return nil, nil, err @@ -166,7 +190,7 @@ func (a *Peers) Connect(ctx context.Context, host string, port uint16) (*PeersCo req, err := http.NewRequest( "POST", - fmt.Sprintf("%s/peers/connect", a.options.BaseUrl), + url.String(), bytes.NewReader(bts)) if err != nil { return nil, nil, err @@ -188,9 +212,14 @@ func (a *Peers) ClearBlacklist(ctx context.Context) (string, *Response, error) { return "", nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/peers/clearblacklist") + if err != nil { + return "", nil, err + } + req, err := http.NewRequest( "POST", - fmt.Sprintf("%s/peers/clearblacklist", a.options.BaseUrl), + url.String(), nil) if err != nil { return "", nil, err diff --git a/pkg/client/transactions.go b/pkg/client/transactions.go index 9d42f6484..f33d2fe4e 100644 --- a/pkg/client/transactions.go +++ b/pkg/client/transactions.go @@ -23,9 +23,14 @@ func NewTransactions(options Options) *Transactions { // Get transaction that is in the UTX func (a *Transactions) UnconfirmedInfo(ctx context.Context, id crypto.Digest) (proto.Transaction, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/transactions/unconfirmed/info/%s", id.String())) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/transactions/unconfirmed/info/%s", a.options.BaseUrl, id.String()), + url.String(), nil) if err != nil { return nil, nil, err @@ -60,9 +65,14 @@ func (a *Transactions) UnconfirmedInfo(ctx context.Context, id crypto.Digest) (p // Get the number of unconfirmed transactions in the UTX pool func (a *Transactions) UnconfirmedSize(ctx context.Context) (uint64, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, "/transactions/unconfirmed/size") + if err != nil { + return 0, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/transactions/unconfirmed/size", a.options.BaseUrl), + url.String(), nil) if err != nil { return 0, nil, err @@ -79,9 +89,14 @@ func (a *Transactions) UnconfirmedSize(ctx context.Context) (uint64, *Response, // Get the number of unconfirmed transactions in the UTX pool func (a *Transactions) Unconfirmed(ctx context.Context) ([]proto.Transaction, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, "/transactions/unconfirmed") + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/transactions/unconfirmed", a.options.BaseUrl), + url.String(), nil) if err != nil { return nil, nil, err @@ -127,9 +142,14 @@ type TransactionTypeVersion struct { // Get transaction info func (a *Transactions) Info(ctx context.Context, id crypto.Digest) (proto.Transaction, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/transactions/info/%s", id.String())) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/transactions/info/%s", a.options.BaseUrl, id.String()), + url.String(), nil) if err != nil { return nil, nil, err @@ -203,9 +223,14 @@ func GuessTransactionType(t *TransactionTypeVersion) (proto.Transaction, error) // Get list of transactions where specified address has been involved func (a *Transactions) Address(ctx context.Context, address proto.Address, limit uint) ([]proto.Transaction, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/transactions/address/%s/limit/%d", address.String(), limit)) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/transactions/address/%s/limit/%d", a.options.BaseUrl, address.String(), limit), + url.String(), nil) if err != nil { return nil, nil, err diff --git a/pkg/client/utils.go b/pkg/client/utils.go index 5ab6b9048..9f77b5b5f 100644 --- a/pkg/client/utils.go +++ b/pkg/client/utils.go @@ -25,9 +25,14 @@ func (a *Utils) Seed(ctx context.Context) (string, *Response, error) { return "", nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/utils/seed") + if err != nil { + return "", nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/utils/seed", a.options.BaseUrl), + url.String(), nil) if err != nil { return "", nil, err @@ -55,9 +60,14 @@ func (a *Utils) HashSecure(ctx context.Context, message string) (*UtilsHashSecur return nil, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/utils/hash/secure") + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "POST", - fmt.Sprintf("%s/utils/hash/secure", a.options.BaseUrl), + url.String(), strings.NewReader(message)) if err != nil { return nil, nil, err @@ -85,9 +95,14 @@ func (a *Utils) HashFast(ctx context.Context, message string) (*UtilsHashFast, * return nil, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/utils/hash/fast") + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "POST", - fmt.Sprintf("%s/utils/hash/fast", a.options.BaseUrl), + url.String(), strings.NewReader(message)) if err != nil { return nil, nil, err @@ -115,9 +130,14 @@ func (a *Utils) Time(ctx context.Context) (*UtilsTime, *Response, error) { return nil, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/utils/time") + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/utils/time", a.options.BaseUrl), + url.String(), nil) if err != nil { return nil, nil, err @@ -145,9 +165,14 @@ func (a *Utils) Sign(ctx context.Context, secretKey crypto.SecretKey, message st return nil, nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/utils/sign/%s", secretKey.String())) + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "POST", - fmt.Sprintf("%s/utils/sign/%s", a.options.BaseUrl, secretKey.String()), + url.String(), strings.NewReader(message)) if err != nil { return nil, nil, err @@ -170,9 +195,14 @@ func (a *Utils) SeedByLength(ctx context.Context, length uint16) (string, *Respo return "", nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, fmt.Sprintf("/utils/seed/%d", length)) + if err != nil { + return "", nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/utils/seed/%d", a.options.BaseUrl, length), + url.String(), nil) if err != nil { return "", nil, err @@ -197,9 +227,14 @@ type UtilsScriptCompile struct { // Compiles string code to base64 script representation func (a *Utils) ScriptCompile(ctx context.Context, code string) (*UtilsScriptCompile, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, "/utils/script/compile") + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "POST", - fmt.Sprintf("%s/utils/script/compile", a.options.BaseUrl), + url.String(), strings.NewReader(code)) if err != nil { return nil, nil, err @@ -223,9 +258,14 @@ type UtilsScriptEstimate struct { // Estimates compiled code in Base64 representation func (a *Utils) ScriptEstimate(ctx context.Context, base64code string) (*UtilsScriptEstimate, *Response, error) { + url, err := joinUrl(a.options.BaseUrl, "/utils/script/estimate") + if err != nil { + return nil, nil, err + } + req, err := http.NewRequest( "POST", - fmt.Sprintf("%s/utils/script/estimate", a.options.BaseUrl), + url.String(), strings.NewReader(base64code)) if err != nil { return nil, nil, err diff --git a/pkg/client/wallet.go b/pkg/client/wallet.go index 8d1f1a596..ce8a3289b 100644 --- a/pkg/client/wallet.go +++ b/pkg/client/wallet.go @@ -2,7 +2,6 @@ package client import ( "context" - "fmt" "net/http" ) @@ -21,9 +20,14 @@ func (a *Wallet) Seed(ctx context.Context) (string, *Response, error) { return "", nil, NoApiKeyError } + url, err := joinUrl(a.options.BaseUrl, "/wallet/seed") + if err != nil { + return "", nil, err + } + req, err := http.NewRequest( "GET", - fmt.Sprintf("%s/wallet/seed", a.options.BaseUrl), + url.String(), nil) if err != nil { return "", nil, err