Skip to content

Commit

Permalink
Merge pull request #4 from nao1215/nchika/add-standard-rate
Browse files Browse the repository at this point in the history
Add GetRate()
  • Loading branch information
nao1215 authored Aug 3, 2024
2 parents 7c8fadc + 50542a3 commit 3f1b808
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 2 deletions.
12 changes: 11 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@
"imageSize": 70,
"commit": true,
"commitConvention": "angular",
"contributors": [],
"contributors": [
{
"login": "nao1215",
"name": "CHIKAMATSU Naohiro",
"avatar_url": "https://avatars.githubusercontent.com/u/22737008?v=4",
"profile": "https://debimate.jp/",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
"linkToUsage": true
}
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## coincheck public & private API client
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

[![Go Reference](https://pkg.go.dev/badge/github.com/nao1215/coincheck.svg)](https://pkg.go.dev/github.com/nao1215/coincheck)
Expand Down Expand Up @@ -88,6 +88,7 @@ If you want to execute the Private API, you need to create a client with the API
| 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. |
| GET /api/rate/[pair] | [GetRate()](https://pkg.go.dev/github.com/nao1215/coincheck#Client.GetRate) | Get the Standard Rate of Coin. |

### Private API

Expand All @@ -111,8 +112,26 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tbody>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://debimate.jp/"><img src="https://avatars.githubusercontent.com/u/22737008?v=4?s=70" width="70px;" alt="CHIKAMATSU Naohiro"/><br /><sub><b>CHIKAMATSU Naohiro</b></sub></a><br /><a href="https://github.com/nao1215/coincheck/commits?author=nao1215" title="Code">💻</a></td>
</tr>
</tbody>
<tfoot>
<tr>
<td align="center" size="13px" colspan="7">
<img src="https://raw.githubusercontent.com/all-contributors/all-contributors-cli/1b8533af435da9854653492b1327a23a4dbd0a10/assets/logo-small.svg">
<a href="https://all-contributors.js.org/docs/en/bot/usage">Add your contributions</a>
</img>
</td>
</tr>
</tfoot>
</table>

<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
43 changes: 43 additions & 0 deletions rate.go
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
}
102 changes: 102 additions & 0 deletions rate_test.go
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)
}
})
}

0 comments on commit 3f1b808

Please sign in to comment.