Skip to content

Commit

Permalink
remove finage usage in bitcoin price
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Aug 9, 2022
1 parent 446f522 commit 3fc7537
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
4 changes: 2 additions & 2 deletions bitcoin/examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"title": "Get bitcoin price",
"description": "Returns the last traded price of bitcoin",
"request": {
"symbol": "BTCUSD"
"symbol": "USD"
},
"response": {
"symbol": "BTCUSD",
"symbol": "USD",
"price": 34682.74
}
}
Expand Down
39 changes: 11 additions & 28 deletions bitcoin/handler/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,18 @@ import (
"strings"
"time"

"github.com/micro/micro/v3/service/config"
"github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger"
pb "github.com/micro/services/bitcoin/proto"
"github.com/patrickmn/go-cache"
)

type Bitcoin struct {
Api string
Key string
Cache *cache.Cache
}

func New() *Bitcoin {
// TODO: look for "bitcoin.provider" to determine the handler
v, err := config.Get("finage.api")
if err != nil {
logger.Fatalf("finage.api config not found: %v", err)
}
api := v.String("")
if len(api) == 0 {
logger.Fatal("finage.api config not found")
}
v, err = config.Get("finage.key")
if err != nil {
logger.Fatalf("finage.key config not found: %v", err)
}
key := v.String("")
if len(key) == 0 {
logger.Fatal("finage.key config not found")
}

return &Bitcoin{
Api: api,
Key: key,
Cache: cache.New(5*time.Minute, 10*time.Minute),
}
}
Expand Down Expand Up @@ -84,11 +61,11 @@ func (b *Bitcoin) Balance(ctx context.Context, req *pb.BalanceRequest, rsp *pb.B

func (b *Bitcoin) Price(ctx context.Context, req *pb.PriceRequest, rsp *pb.PriceResponse) error {
if len(req.Symbol) <= 0 {
req.Symbol = "BTCUSD"
req.Symbol = "USD"
}

if !strings.HasPrefix(req.Symbol, "BTC") {
return errors.BadRequest("bitcoin.price", "Must be of format BTCXXX e.g BTCUSD")
if strings.HasPrefix(req.Symbol, "BTC") {
req.Symbol = strings.TrimPrefix(req.Symbol, "BTC")
}

// try the cache first
Expand All @@ -99,7 +76,8 @@ func (b *Bitcoin) Price(ctx context.Context, req *pb.PriceRequest, rsp *pb.Price
}

// get the price
uri := fmt.Sprintf("%slast/crypto/%s?apikey=%s", b.Api, req.Symbol, b.Key)

uri := "https://blockchain.info/ticker"

resp, err := http.Get(uri)
if err != nil {
Expand All @@ -122,8 +100,13 @@ func (b *Bitcoin) Price(ctx context.Context, req *pb.PriceRequest, rsp *pb.Price
return errors.InternalServerError("bitcoin.price", "failed to get price")
}

data, ok := respBody[req.Symbol]
if !ok {
return errors.InternalServerError("bitcoin.price", "unsupported symbol")
}

rsp.Symbol = req.Symbol
rsp.Price = respBody["price"].(float64)
rsp.Price = data.(map[string]interface{})["last"].(float64)

// cache the price
b.Cache.Set("price:"+req.Symbol, rsp.Price, time.Minute*5)
Expand Down

0 comments on commit 3fc7537

Please sign in to comment.