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

Commit

Permalink
Documentation & Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
onuryilmaz committed Jul 5, 2016
1 parent 3a35fac commit 36ce76d
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 150 deletions.
1 change: 1 addition & 0 deletions btce.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package btce

// API allows to use public and trade APIs of BTC-E
type API struct {
public PublicAPI
trade TradeAPI
Expand Down
45 changes: 27 additions & 18 deletions public.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,57 @@ package btce
import (
"encoding/json"
"net/http"
"strconv"
)

// PublicAPI provides access to such information as tickers of currency pairs, active orders on different pairs, the latest trades for each pair etc.
type PublicAPI struct{}

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

func (api *PublicAPI) Ticker(currency []string) (Ticker, error) {
// Info provides all the information about currently active pairs, such as the maximum number of digits after the decimal point, the minimum price, the maximum price, the minimum transaction size, whether the pair is hidden, the commission for each pair.
func (api *PublicAPI) Info() (Info, error) {

url := API_URL + "ticker/"
for _, c := range currency {
url = url + c + "-"
}
url := API_URL + "info"
r, err := http.Get(url)

if err == nil {
data := make(Ticker, len(currency))
data := Info{}
json.NewDecoder(r.Body).Decode(&data)
return data, nil
}

return nil, err

return Info{}, err
}

func (api *PublicAPI) Info() (Info, error) {
// Ticker provides all the information about currently active pairs, such as: the maximum price, the minimum price, average price, trade volume, trade volume in currency, the last trade, Buy and Sell price.
// All information is provided over the past 24 hours.
func (api *PublicAPI) Ticker(currency []string) (Ticker, error) {

url := API_URL + "info"
url := API_URL + "ticker/"
for _, c := range currency {
url = url + c + "-"
}
r, err := http.Get(url)

if err == nil {
data := Info{}
data := make(Ticker, len(currency))
json.NewDecoder(r.Body).Decode(&data)
return data, nil
}

return Info{}, err

return nil, err
}

func (api *PublicAPI) Depth(currency []string) (Depth, error) {
// Depth provides the information about active orders on the pair.
func (api *PublicAPI) Depth(currency []string, limit int) (Depth, error) {

url := API_URL + "depth/"
for _, c := range currency {
url = url + c + "-"
}
if limit > 0 {
url = url + "?limit=" + strconv.Itoa(limit)
}
r, err := http.Get(url)

if err == nil {
Expand All @@ -60,20 +66,23 @@ func (api *PublicAPI) Depth(currency []string) (Depth, error) {

}

func (api *PublicAPI) Trade(currency []string) (Trade, error) {
// Trades provides the information about the last trades.
func (api *PublicAPI) Trades(currency []string, limit int) (Trades, error) {

url := API_URL + "trades/"
for _, c := range currency {
url = url + c + "-"
}
if limit > 0 {
url = url + "?limit=" + strconv.Itoa(limit)
}
r, err := http.Get(url)

if err == nil {
data := make(Trade, len(currency))
data := make(Trades, len(currency))
json.NewDecoder(r.Body).Decode(&data)
return data, nil
}

return nil, err

}
9 changes: 3 additions & 6 deletions public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ func TestInfo(t *testing.T) {
So(information.Pairs["btc_usd"], ShouldHaveSameTypeAs, InfoPair{})
})
})

}

func TestDepth(t *testing.T) {

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

Convey("No error should occur", func() {
So(err, ShouldBeNil)
Expand All @@ -56,22 +55,20 @@ func TestDepth(t *testing.T) {
So(depth["btc_usd"], ShouldHaveSameTypeAs, DepthPair{})
})
})

}

func TestTrade(t *testing.T) {

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

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

Convey("Trade data for 'btc_usd' should be returned", func() {
So(trade, ShouldHaveSameTypeAs, Trade{})
So(trade, ShouldHaveSameTypeAs, Trades{})
So(len(trade["btc_usd"]), ShouldBeGreaterThanOrEqualTo, 0)
})
})

}
2 changes: 1 addition & 1 deletion structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type DepthPair struct {

type DepthItem []float64

type Trade map[string]TradePair
type Trades map[string]TradePair
type TradePair []TradeItem
type TradeItem struct {
Type string `json:"type"`
Expand Down
Loading

0 comments on commit 36ce76d

Please sign in to comment.