-
-
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 #2 from nao1215/nchika/add-public-apis
Add GET /api/trades
- Loading branch information
Showing
8 changed files
with
257 additions
and
32 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,11 @@ | ||
package coincheck | ||
|
||
// OrderType represents the order type. | ||
type OrderType string | ||
|
||
const ( | ||
// OrderTypeBuy is the order type of buy. | ||
OrderTypeBuy OrderType = "buy" | ||
// OrderTypeSell is the order type of sell. | ||
OrderTypeSell OrderType = "sell" | ||
) |
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,26 @@ | ||
package coincheck | ||
|
||
// Pagination represents the pagination of coincheck API. | ||
// It is possible to get by dividing the data. | ||
type Pagination struct { | ||
// Limit is the number of data to get. | ||
Limit int `json:"limit"` | ||
// PaginationOrder is the order of the data. You can specify "desc" or "asc". | ||
PaginationOrder PaginationOrder `json:"order"` | ||
// StartingAfter is the ID of the data to start getting. | ||
// Greater than the specified ID. For example, if you specify 3, you will get data from ID 4. | ||
StartingAfter int `json:"starting_after,omitempty"` | ||
// EndingBefore is the ID of the data to end getting. | ||
// Less than the specified ID. For example, if you specify 3, you will get data up to ID 2. | ||
EndingBefore int `json:"ending_before,omitempty"` | ||
} | ||
|
||
// PaginationOrder represents the order of the pagination. | ||
type PaginationOrder string | ||
|
||
const ( | ||
// PaginationOrderDesc is the order of the pagination in descending order. | ||
PaginationOrderDesc PaginationOrder = "desc" | ||
// PaginationOrderAsc is the order of the pagination in ascending order. | ||
PaginationOrderAsc PaginationOrder = "asc" | ||
) |
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,60 @@ | ||
package coincheck | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
) | ||
|
||
// GetTradesInput represents the input parameter for GetTrades | ||
type GetTradesInput struct { | ||
// Pair is the pair of the currency. e.g. btc_jpy. | ||
Pair Pair | ||
} | ||
|
||
// GetTradesResponse represents the output from GetTrades | ||
type GetTradesResponse struct { | ||
// Success is a boolean value that indicates the success of the API call. | ||
Success bool `json:"success"` | ||
// Pagination is the pagination of the data. | ||
Pagination Pagination `json:"pagination"` | ||
// Data is a list of trades. | ||
Data []Trade `json:"data"` | ||
} | ||
|
||
// Trade represents a trade. | ||
type Trade struct { | ||
// ID is the trade ID. | ||
ID int `json:"id"` | ||
// Amount is the amount of the trade. | ||
Amount float64 `json:"amount"` | ||
// Rate is the rate of the trade. | ||
Rate float64 `json:"rate"` | ||
// Pair is the pair of the currency. | ||
Pair Pair `json:"pair"` | ||
// OrderType is the order type. | ||
OrderType OrderType `json:"order_type"` | ||
// CreatedAt is the creation time of the trade. | ||
CreatedAt string `json:"created_at"` | ||
} | ||
|
||
// GetTrades returns a list of trades (order transactions). | ||
// API: GET /api/trades | ||
// Visibility: Public | ||
func (c *Client) GetTrades(ctx context.Context, input GetTradesInput) (*GetTradesResponse, error) { | ||
req, err := c.createRequest(ctx, createRequestInput{ | ||
method: http.MethodGet, | ||
path: "/api/trades", | ||
queryParam: map[string]string{ | ||
"pair": string(input.Pair), | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var output GetTradesResponse | ||
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,125 @@ | ||
package coincheck | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestClient_GetTrades(t *testing.T) { | ||
t.Run("In the case of a successful GET /api/trades request", 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/trades" | ||
if got := r.URL.Path; got != wantEndpoint { | ||
t.Errorf("Endpoint: got %v, want %v", got, wantEndpoint) | ||
} | ||
|
||
wantPair := PairETCJPY | ||
if got := r.URL.Query().Get("pair"); got != wantPair.String() { | ||
t.Errorf("Pair: got %v, want %v", got, wantPair) | ||
} | ||
|
||
result := GetTradesResponse{ | ||
Success: true, | ||
Pagination: Pagination{ | ||
Limit: 1, | ||
PaginationOrder: "desc", | ||
StartingAfter: 0, | ||
EndingBefore: 0, | ||
}, | ||
Data: []Trade{ | ||
{ | ||
ID: 1, | ||
Amount: 1, | ||
Rate: 1000000, | ||
Pair: PairETCJPY, | ||
OrderType: OrderTypeBuy, | ||
CreatedAt: "2021-01-01T00:00:00Z", | ||
}, | ||
{ | ||
ID: 2, | ||
Amount: 2, | ||
Rate: 2000000, | ||
Pair: PairETCJPY, | ||
OrderType: OrderTypeSell, | ||
CreatedAt: "2021-01-02T00:00:00Z", | ||
}, | ||
}, | ||
} | ||
if err := json.NewEncoder(w).Encode(result); err != nil { | ||
t.Fatal(err) | ||
} | ||
})) | ||
|
||
client, err := NewClient(WithBaseURL(testServer.URL)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
input := GetTradesInput{ | ||
Pair: PairETCJPY, | ||
} | ||
got, err := client.GetTrades(context.Background(), input) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
want := &GetTradesResponse{ | ||
Success: true, | ||
Pagination: Pagination{ | ||
Limit: 1, | ||
PaginationOrder: PaginationOrderDesc, | ||
StartingAfter: 0, | ||
EndingBefore: 0, | ||
}, | ||
Data: []Trade{ | ||
{ | ||
ID: 1, | ||
Amount: 1, | ||
Rate: 1000000, | ||
Pair: PairETCJPY, | ||
OrderType: OrderTypeBuy, | ||
CreatedAt: "2021-01-01T00:00:00Z", | ||
}, | ||
{ | ||
ID: 2, | ||
Amount: 2, | ||
Rate: 2000000, | ||
Pair: PairETCJPY, | ||
OrderType: OrderTypeSell, | ||
CreatedAt: "2021-01-02T00:00:00Z", | ||
}, | ||
}, | ||
} | ||
if diff := cmp.Diff(want, got); diff != "" { | ||
printDiff(t, diff) | ||
} | ||
}) | ||
|
||
t.Run("In the case of a failed GET /api/trades request", func(t *testing.T) { | ||
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
})) | ||
|
||
client, err := NewClient(WithBaseURL(testServer.URL)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
input := GetTradesInput{ | ||
Pair: PairETCJPY, | ||
} | ||
if _, err = client.GetTrades(context.Background(), input); err == nil { | ||
t.Fatal("err must not be 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