-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
211 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/tonicpow/go-tonicpow" | ||
) | ||
|
||
func main() { | ||
|
||
// Load the api client | ||
client, err := tonicpow.NewClient( | ||
tonicpow.WithAPIKey(os.Getenv("TONICPOW_API_KEY")), | ||
tonicpow.WithEnvironmentString(os.Getenv("TONICPOW_ENVIRONMENT")), | ||
) | ||
if err != nil { | ||
log.Fatalf("error in NewClient: %s", err.Error()) | ||
} | ||
|
||
// Get current rate | ||
var rate *tonicpow.Rate | ||
rate, err = client.GetCurrentRate("usd", 1.00) | ||
if err != nil { | ||
log.Fatalf("error in GetCurrentRate: %s", err.Error()) | ||
} | ||
|
||
log.Printf("rate: %s %f is %d sats", rate.Currency, rate.CurrencyAmount, rate.PriceInSatoshis) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
package tonicpow | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// newTestRate creates a dummy profile for testing | ||
func newTestRate() *Rate { | ||
return &Rate{ | ||
Currency: testRateCurrency, | ||
CurrencyAmount: 0.01, | ||
PriceInSatoshis: 4200, | ||
} | ||
} | ||
|
||
// TestClient_GetCurrentRate will test the method GetCurrentRate() | ||
func TestClient_GetCurrentRate(t *testing.T) { | ||
// t.Parallel() (Cannot run in parallel - issues with overriding the mock client) | ||
|
||
t.Run("get current rate (success)", func(t *testing.T) { | ||
client, err := newTestClient() | ||
assert.NoError(t, err) | ||
assert.NotNil(t, client) | ||
|
||
rates := newTestRate() | ||
|
||
endpoint := fmt.Sprintf( | ||
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL, | ||
modelRates, testRateCurrency, | ||
fieldAmount, 0.0, | ||
) | ||
|
||
err = mockResponseData(http.MethodGet, endpoint, http.StatusOK, rates) | ||
assert.NoError(t, err) | ||
|
||
var currentRate *Rate | ||
currentRate, err = client.GetCurrentRate(testRateCurrency, 0.00) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, currentRate) | ||
assert.Equal(t, testRateCurrency, currentRate.Currency) | ||
}) | ||
|
||
t.Run("missing currency", func(t *testing.T) { | ||
client, err := newTestClient() | ||
assert.NoError(t, err) | ||
assert.NotNil(t, client) | ||
|
||
rates := newTestRate() | ||
|
||
endpoint := fmt.Sprintf( | ||
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL, | ||
modelRates, testRateCurrency, | ||
fieldAmount, 0.0, | ||
) | ||
|
||
err = mockResponseData(http.MethodGet, endpoint, http.StatusOK, rates) | ||
assert.NoError(t, err) | ||
|
||
var currentRate *Rate | ||
currentRate, err = client.GetCurrentRate("", 0.00) | ||
assert.Error(t, err) | ||
assert.Nil(t, currentRate) | ||
}) | ||
|
||
t.Run("error from api (status code)", func(t *testing.T) { | ||
client, err := newTestClient() | ||
assert.NoError(t, err) | ||
assert.NotNil(t, client) | ||
|
||
rates := newTestRate() | ||
|
||
endpoint := fmt.Sprintf( | ||
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL, | ||
modelRates, testRateCurrency, | ||
fieldAmount, 0.0, | ||
) | ||
|
||
err = mockResponseData(http.MethodGet, endpoint, http.StatusBadRequest, rates) | ||
assert.NoError(t, err) | ||
|
||
var currentRate *Rate | ||
currentRate, err = client.GetCurrentRate(testRateCurrency, 0.00) | ||
assert.Error(t, err) | ||
assert.Nil(t, currentRate) | ||
}) | ||
|
||
t.Run("error from api (status code)", func(t *testing.T) { | ||
client, err := newTestClient() | ||
assert.NoError(t, err) | ||
assert.NotNil(t, client) | ||
|
||
endpoint := fmt.Sprintf( | ||
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL, | ||
modelRates, testRateCurrency, | ||
fieldAmount, 0.0, | ||
) | ||
|
||
apiError := &Error{ | ||
Code: 400, | ||
Data: "field_name", | ||
IPAddress: "127.0.0.1", | ||
Message: "some error message", | ||
Method: http.MethodPut, | ||
RequestGUID: "7f3d97a8fd67ff57861904df6118dcc8", | ||
StatusCode: http.StatusBadRequest, | ||
URL: endpoint, | ||
} | ||
|
||
err = mockResponseData(http.MethodGet, endpoint, http.StatusBadRequest, apiError) | ||
assert.NoError(t, err) | ||
|
||
var currentRate *Rate | ||
currentRate, err = client.GetCurrentRate(testRateCurrency, 0.00) | ||
assert.Error(t, err) | ||
assert.Nil(t, currentRate) | ||
assert.Equal(t, apiError.Message, err.Error()) | ||
}) | ||
} | ||
|
||
// ExampleClient_GetCurrentRate example using GetCurrentRate() | ||
// | ||
// See more examples in /examples/ | ||
func ExampleClient_GetCurrentRate() { | ||
|
||
// Load the client (using test client for example only) | ||
client, err := newTestClient() | ||
if err != nil { | ||
fmt.Printf("error loading client: %s", err.Error()) | ||
return | ||
} | ||
|
||
// For mocking | ||
rates := newTestRate() | ||
|
||
// Mock response (for example only) | ||
_ = mockResponseData( | ||
http.MethodGet, | ||
fmt.Sprintf( | ||
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL, | ||
modelRates, testRateCurrency, | ||
fieldAmount, 0.0, | ||
), | ||
http.StatusOK, | ||
rates, | ||
) | ||
|
||
// Get profile (using mocking response) | ||
var currentRate *Rate | ||
if currentRate, err = client.GetCurrentRate(testRateCurrency, 0.00); err != nil { | ||
fmt.Printf("error getting profile: " + err.Error()) | ||
return | ||
} | ||
fmt.Printf("current rate: %s %f usd is %d sats", currentRate.Currency, currentRate.CurrencyAmount, currentRate.PriceInSatoshis) | ||
// Output:current rate: usd 0.010000 usd is 4200 sats | ||
} | ||
|
||
// BenchmarkClient_GetCurrentRate benchmarks the method GetCurrentRate() | ||
func BenchmarkClient_GetCurrentRate(b *testing.B) { | ||
client, _ := newTestClient() | ||
rate := newTestRate() | ||
_ = mockResponseData( | ||
http.MethodGet, | ||
fmt.Sprintf( | ||
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL, | ||
modelRates, testRateCurrency, | ||
fieldAmount, 0.0, | ||
), | ||
http.StatusOK, | ||
rate, | ||
) | ||
for i := 0; i < b.N; i++ { | ||
_, _ = client.GetCurrentRate(testRateCurrency, 0.00) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters