-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from nao1215/nchika/add-standard-rate
Add GetRate()
- Loading branch information
Showing
4 changed files
with
176 additions
and
2 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
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,43 @@ | ||
package coincheck | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
) | ||
|
||
// GetRateInput represents the input for the GetStandardRate function. | ||
type GetRateInput struct { | ||
// Pair is the pair of the currency. e.g. btc_jpy. | ||
Pair Pair | ||
} | ||
|
||
// GetRateResponse represents the output from GetStandardRate. | ||
type GetRateResponse struct { | ||
// Rate is the standard rate. | ||
Rate string `json:"rate"` | ||
} | ||
|
||
// GetRate returns the standard rate. | ||
// API: GET /api/rate/[pair] | ||
// Visibility: Public | ||
// If you set don't set the pair, this function will return the standard rate of BTC/JPY. | ||
func (c *Client) GetRate(ctx context.Context, input GetRateInput) (*GetRateResponse, error) { | ||
pair := PairBTCJPY | ||
if input.Pair != "" { | ||
pair = input.Pair | ||
} | ||
|
||
req, err := c.createRequest(ctx, createRequestInput{ | ||
method: http.MethodGet, | ||
path: "/api/rate/" + pair.String(), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var output GetRateResponse | ||
if err := c.do(req, &output); err != nil { | ||
return nil, err | ||
} | ||
return &output, nil | ||
} |
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,102 @@ | ||
package coincheck | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestClient_GetStandardRate(t *testing.T) { | ||
t.Run("GetStandardRate returns the standard rate", func(t *testing.T) { | ||
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
wantMethod := http.MethodGet | ||
if got := r.Method; got != wantMethod { | ||
t.Errorf("Method: got %v, want %v", got, wantMethod) | ||
} | ||
|
||
wantEndpoint := "/api/rate/etc_jpy" | ||
if got := r.URL.Path; got != wantEndpoint { | ||
t.Errorf("Endpoint: got %v, want %v", got, wantEndpoint) | ||
} | ||
|
||
result := GetRateResponse{ | ||
Rate: "1000000", | ||
} | ||
if err := json.NewEncoder(w).Encode(result); err != nil { | ||
t.Fatal(err) | ||
} | ||
})) | ||
|
||
client, err := NewClient(WithBaseURL(testServer.URL)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
got, err := client.GetRate(context.Background(), GetRateInput{Pair: PairETCJPY}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
want := &GetRateResponse{ | ||
Rate: "1000000", | ||
} | ||
if diff := cmp.Diff(want, got); diff != "" { | ||
printDiff(t, diff) | ||
} | ||
}) | ||
|
||
t.Run("GetStandardRate returns an error", func(t *testing.T) { | ||
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
http.Error(w, "Internal Server Error", http.StatusInternalServerError) | ||
})) | ||
|
||
client, err := NewClient(WithBaseURL(testServer.URL)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if _, err = client.GetRate(context.Background(), GetRateInput{Pair: PairETCJPY}); err == nil { | ||
t.Fatal("expected an error, but got nil") | ||
} | ||
}) | ||
|
||
t.Run("If the pair is empty, GetStandardRate returns the btc_jpy rate", func(t *testing.T) { | ||
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
wantMethod := http.MethodGet | ||
if got := r.Method; got != wantMethod { | ||
t.Errorf("Method: got %v, want %v", got, wantMethod) | ||
} | ||
|
||
wantEndpoint := "/api/rate/btc_jpy" | ||
if got := r.URL.Path; got != wantEndpoint { | ||
t.Errorf("Endpoint: got %v, want %v", got, wantEndpoint) | ||
} | ||
|
||
result := GetRateResponse{ | ||
Rate: "1000000", | ||
} | ||
if err := json.NewEncoder(w).Encode(result); err != nil { | ||
t.Fatal(err) | ||
} | ||
})) | ||
|
||
client, err := NewClient(WithBaseURL(testServer.URL)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
got, err := client.GetRate(context.Background(), GetRateInput{Pair: ""}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
want := &GetRateResponse{ | ||
Rate: "1000000", | ||
} | ||
if diff := cmp.Diff(want, got); diff != "" { | ||
printDiff(t, diff) | ||
} | ||
}) | ||
} |