Skip to content

Commit

Permalink
Merge pull request #3 from nao1215/nchika/add-public-apis
Browse files Browse the repository at this point in the history
Add GetOrderBooks()
  • Loading branch information
nao1215 authored Aug 3, 2024
2 parents da9d111 + d0fb8a7 commit 7c8fadc
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ go.work.sum

# Project coverage profile
/coverage.*
cli
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@ If you want to execute the Private API, you need to create a client with the API

| API | Method Name |Description |
| :--- | :--- | :--- |
| GET /api/ticker | GetTicker() | Check latest ticker information. |
| GET /api/trades | GetTrades() | You can get current order transactions. |
| GET /api/ticker | [GetTicker()](https://pkg.go.dev/github.com/nao1215/coincheck#Client.GetTicker) | Check latest ticker information. |
| GET /api/trades | [GetTrades()](https://pkg.go.dev/github.com/nao1215/coincheck#Client.GetTrades) | You can get current order transactions. |
| GET /api/order_books | [GetOrderBooks()](https://pkg.go.dev/github.com/nao1215/coincheck#Client.GetOrderBooks) | Fetch order book information. |

### Private API

| API | Method Name |Description |
| :--- | :--- | :--- |
| GET /api/bank_accounts | GetBankAccounts() | Display list of bank account you registered (withdrawal).|
| GET /api/bank_accounts | [GetBankAccounts()](https://pkg.go.dev/github.com/nao1215/coincheck#Client.GetBankAccounts) | Display list of bank account you registered (withdrawal).|

## License

Expand Down
42 changes: 42 additions & 0 deletions order.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package coincheck

import (
"context"
"net/http"
)

// OrderType represents the order type.
type OrderType string

Expand All @@ -9,3 +14,40 @@ const (
// OrderTypeSell is the order type of sell.
OrderTypeSell OrderType = "sell"
)

// SellOrderStatus represents the sell order status.
// It has the only two elements.
// e.g. [ "27330", "1.25" ]
type SellOrderStatus []string

// BuyOrderStatus represents the buy order status.
// It has the only two elements.
// e.g. [ "12100", "0.12" ]
type BuyOrderStatus []string

// GetOrderBooksResponse represents the structure of the API response.
type GetOrderBooksResponse struct {
// Asks is the sell order status.
Asks []SellOrderStatus `json:"asks"`
// Bids is the buy order status.
Bids []BuyOrderStatus `json:"bids"`
}

// GetOrderBooks fetch order book information.
// API: GET /api/order_books
// Visibility: Public
func (c *Client) GetOrderBooks(ctx context.Context) (*GetOrderBooksResponse, error) {
req, err := c.createRequest(ctx, createRequestInput{
method: http.MethodGet,
path: "/api/order_books",
})
if err != nil {
return nil, err
}

var output GetOrderBooksResponse
if err := c.do(req, &output); err != nil {
return nil, err
}
return &output, nil
}
79 changes: 79 additions & 0 deletions order_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package coincheck

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestClientGetOrderBooksResponse(t *testing.T) {
t.Run("GetOrderBooksResponse returns a list of order books", 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/order_books"
if got := r.URL.Path; got != wantEndpoint {
t.Errorf("Endpoint: got %v, want %v", got, wantEndpoint)
}

result := GetOrderBooksResponse{
Asks: []SellOrderStatus{
{"27330", "2.25"},
{"27340", "0.1"},
},
Bids: []BuyOrderStatus{
{"27320", "0.2"},
},
}
if err := json.NewEncoder(w).Encode(result); err != nil {
t.Fatal(err)
}
}))

// Create a new client
client, err := NewClient(WithBaseURL(testServer.URL))
if err != nil {
t.Fatal(err)
}

got, err := client.GetOrderBooks(context.Background())
if err != nil {
t.Fatal(err)
}
want := &GetOrderBooksResponse{
Asks: []SellOrderStatus{
{"27330", "2.25"},
{"27340", "0.1"},
},
Bids: []BuyOrderStatus{
{"27320", "0.2"},
},
}
if diff := cmp.Diff(want, got); diff != "" {
printDiff(t, diff)
}
})

t.Run("GetOrderBooksResponse returns an error", func(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer testServer.Close()

client, err := NewClient(WithBaseURL(testServer.URL))
if err != nil {
t.Fatal(err)
}

if _, err = client.GetOrderBooks(context.Background()); err == nil {
t.Error("want error, but got nil")
}
})
}

0 comments on commit 7c8fadc

Please sign in to comment.