Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
Refactoring for public and trade APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
onuryilmaz committed Jul 4, 2016
1 parent 8f283d9 commit 78b48c4
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 103 deletions.
6 changes: 6 additions & 0 deletions btce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package btce

type API struct {
public PublicAPI
trade TradeAPI
}
22 changes: 22 additions & 0 deletions btce_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package btce

import (
. "github.com/smartystreets/goconvey/convey"
"testing"
)

var btce = API{}

func TestBTCE(t *testing.T) {

Convey("BTCE instance created", t, func() {

Convey("Public API should be available", func() {
So(btce.public, ShouldNotBeNil)
})

Convey("Trade API should be available", func() {
So(btce.trade, ShouldNotBeNil)
})
})
}
10 changes: 6 additions & 4 deletions public.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"net/http"
)

type PublicAPI struct{}

const API_URL = "https://btc-e.com/api/3/"

func GetTicker(currency []string) (Ticker, error) {
func (api *PublicAPI) Ticker(currency []string) (Ticker, error) {

url := API_URL + "ticker/"
for _, c := range currency {
Expand All @@ -25,7 +27,7 @@ func GetTicker(currency []string) (Ticker, error) {

}

func GetInfo() (Info, error) {
func (api *PublicAPI) Info() (Info, error) {

url := API_URL + "info"
r, err := http.Get(url)
Expand All @@ -40,7 +42,7 @@ func GetInfo() (Info, error) {

}

func GetDepth(currency []string) (Depth, error) {
func (api *PublicAPI) Depth(currency []string) (Depth, error) {

url := API_URL + "depth/"
for _, c := range currency {
Expand All @@ -58,7 +60,7 @@ func GetDepth(currency []string) (Depth, error) {

}

func GetTrade(currency []string) (Trade, error) {
func (api *PublicAPI) Trade(currency []string) (Trade, error) {

url := API_URL + "trades/"
for _, c := range currency {
Expand Down
13 changes: 7 additions & 6 deletions public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"testing"
)

var api = PublicAPI{}

func TestTicker(t *testing.T) {

Convey("Ticker data for BTC-USD", t, func() {
tickers, err := GetTicker([]string{"btc_usd"})
tickers, err := api.Ticker([]string{"btc_usd"})

Convey("No error should occur", func() {
So(err, ShouldBeNil)
Expand All @@ -26,15 +28,15 @@ func TestTicker(t *testing.T) {
func TestInfo(t *testing.T) {

Convey("Information data", t, func() {
information, err := GetInfo()
information, err := api.Info()

Convey("No error should occur", func() {
So(err, ShouldBeNil)
})

Convey("Information data for 'btc_usd' should be returned", func() {
So(information, ShouldHaveSameTypeAs, Info{})
So(information.Pairs["btc_usd"], ShouldHaveSameTypeAs, PairInfo{})
So(information.Pairs["btc_usd"], ShouldHaveSameTypeAs, InfoPair{})
})
})

Expand All @@ -43,7 +45,7 @@ func TestInfo(t *testing.T) {
func TestDepth(t *testing.T) {

Convey("Depth data", t, func() {
depth, err := GetDepth([]string{"btc_usd"})
depth, err := api.Depth([]string{"btc_usd"})

Convey("No error should occur", func() {
So(err, ShouldBeNil)
Expand All @@ -60,7 +62,7 @@ func TestDepth(t *testing.T) {
func TestTrade(t *testing.T) {

Convey("Trade data", t, func() {
trade, err := GetTrade([]string{"btc_usd"})
trade, err := api.Trade([]string{"btc_usd"})

Convey("No error should occur", func() {
So(err, ShouldBeNil)
Expand All @@ -69,7 +71,6 @@ func TestTrade(t *testing.T) {
Convey("Trade data for 'btc_usd' should be returned", func() {
So(trade, ShouldHaveSameTypeAs, Trade{})
So(len(trade["btc_usd"]), ShouldBeGreaterThanOrEqualTo, 0)
//So(trade["btc_usd"][0].TID, ShouldBeGreaterThanOrEqualTo, 0)
})
})

Expand Down
18 changes: 9 additions & 9 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ type TickerPair struct {

type Info struct {
ServerTime int64 `json:"server_time"`
Pairs map[string]PairInfo `json:"pairs"`
Pairs map[string]InfoPair `json:"pairs"`
}

type PairInfo struct {
type InfoPair struct {
DecimalPlaces int `json:"decimal_places"`
MinPrice float64 `json:"min_price"`
MaxPrice float64 `json:"max_price"`
Expand Down Expand Up @@ -83,14 +83,14 @@ type ActiveOrder struct {

type ActiveOrders map[string]ActiveOrder

type TradeResponse struct {
type Trade struct {
Received float64 `json:"received"`
Remains float64 `json:"remains"`
OrderID string `json:"order_id"`
Funds map[string]float64 `json:"funds"`
}

type OrderInfo struct {
type OrderInfoItem struct {
Pair string `json:"pair"`
Type string `json:"type"`
StartAmount float64 `json:"start_amount"`
Expand All @@ -100,9 +100,9 @@ type OrderInfo struct {
Status int `json:"status"`
}

type OrderInfos map[string]OrderInfo
type OrderInfo map[string]OrderInfoItem

type CancelOrderResponse struct {
type CancelOrder struct {
OrderID int `json:"order_id"`
Funds map[string]float64 `json:"funds"`
}
Expand Down Expand Up @@ -140,19 +140,19 @@ type TransactionHistoryItem struct {

type TransactionHistory map[string]TransactionHistoryItem

type WithdrawCoinResponse struct {
type WithdrawCoin struct {
TransactionID int `json:"tId"`
AmountSent float64 `json:"amountSent"`
Funds map[string]float64 `json:"funds"`
}

type CreateCouponResponse struct {
type CreateCoupon struct {
Coupon string `json:"coupon"`
TransactionID int `json:"transID"`
Funds map[string]float64 `json:"funds"`
}

type RedeemCouponResponse struct {
type RedeemCoupon struct {
CouponAmount string `json:"couponAmount"`
CouponCurrency string `json:"couponCurrency"`
TransactionID int `json:"transID"`
Expand Down
Loading

0 comments on commit 78b48c4

Please sign in to comment.