Skip to content

Commit

Permalink
#1
Browse files Browse the repository at this point in the history
  • Loading branch information
eslavyansky committed May 19, 2018
1 parent 86ec9c1 commit e3d501b
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 24 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ View [API Documentation](https://www.bitgo.com/api/v2).

## List Wallets
```go
b, err := bitgo.New("test", "{Access token}")
b, err := bitgo.New("test", "{Access token}", time.Minute)
if err != nil {
log.Fatal(err.Error())
}

list, err := b.Coin("btc").ListWallets(nil)
list, err := b.Coin("tbtc").Debug(true).ListWallets(bitgo.ListParams{
AllTokens: true,
})
if err != nil {
log.Fatal(err.Error())
}
Expand Down
7 changes: 5 additions & 2 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package main

import (
"log"
"time"

"github.com/jazzserve/bitgo"
)

func main() {
b, err := bitgo.New("test", "{Access token}")
b, err := bitgo.New("test", "{Access token}", time.Minute)
if err != nil {
log.Fatal(err.Error())
}

list, err := b.Coin("tbtc").ListWallets(nil)
list, err := b.Coin("tbtc").Debug(true).ListWallets(bitgo.ListParams{
AllTokens: true,
})
if err != nil {
log.Fatal(err.Error())
}
Expand Down
40 changes: 30 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"time"

Expand All @@ -13,9 +14,11 @@ import (
)

type BitGo struct {
host string
token string
coin string
debug bool
timeout time.Duration
host string
token string
coin string
}

type ListParams struct {
Expand All @@ -24,7 +27,7 @@ type ListParams struct {
AllTokens bool `url:"allTokens,omitempty"`
}

func New(env string, token string) (b *BitGo, err error) {
func New(env string, token string, timeout time.Duration) (b *BitGo, err error) {
if env == "" {
return nil, errors.New("empty env")
}
Expand All @@ -35,16 +38,19 @@ func New(env string, token string) (b *BitGo, err error) {
env = "https://www.bitgo.com"
}
return &BitGo{
host: env + "/api/v2",
token: token,
host: env + "/api/v2",
token: token,
timeout: timeout,
}, nil
}

func (b *BitGo) clone() *BitGo {
return &BitGo{
host: b.host,
token: b.token,
coin: b.coin,
host: b.host,
token: b.token,
coin: b.coin,
timeout: b.timeout,
debug: b.debug,
}
}

Expand All @@ -54,6 +60,12 @@ func (b *BitGo) Coin(coin string) *BitGo {
return c
}

func (b *BitGo) Debug(debug bool) *BitGo {
c := b.clone()
c.debug = debug
return c
}

func (b *BitGo) get(url string, params interface{}, responce interface{}) (err error) {
if params != nil {
_, err = valid.ValidateStruct(params)
Expand Down Expand Up @@ -117,7 +129,11 @@ func (b *BitGo) request(req *http.Request, responce interface{}) (err error) {
req.Header.Add("Authorization", "Bearer "+b.token)

client := &http.Client{
Timeout: time.Minute,
Timeout: b.timeout,
}

if b.debug {
log.Println(req.Method, req.URL.EscapedPath())
}

r, err := client.Do(req)
Expand All @@ -142,6 +158,10 @@ func (b *BitGo) request(req *http.Request, responce interface{}) (err error) {
return berr
}

if b.debug {
log.Println(r.Status, string(resp))
}

err = json.Unmarshal(resp, &responce)
return
}
5 changes: 3 additions & 2 deletions main_test.go → test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ func getTestParams() (params *TestParams) {
func getTestBitGo(t *testing.T) (b *BitGo, params *TestParams) {
params = getTestParams()

b, err := New(params.Env, params.Token)
b, err := New(params.Env, params.Token, time.Minute*5)
if err != nil {
t.Fatal(err)
}
return

return b.Debug(true), params
}

func getTestCoin(t *testing.T) (coin *BitGo, params *TestParams) {
Expand Down
2 changes: 1 addition & 1 deletion wallet-operations-basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type TransactionDescription struct {

// List Wallet Transfers

func (b *BitGo) ListWalletTransfers(walletId string, params *ListParams) (list TransferList, err error) {
func (b *BitGo) ListWalletTransfers(walletId string, params ListParams) (list TransferList, err error) {
err = b.get(
fmt.Sprintf("%s/wallet/%s/transfer",
b.coin,
Expand Down
2 changes: 1 addition & 1 deletion wallet-operations-basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestListWalletTransfers(t *testing.T) {
coin, params := getTestCoin(t)

_, err := coin.ListWalletTransfers(params.WalletId, &ListParams{
_, err := coin.ListWalletTransfers(params.WalletId, ListParams{
Limit: 5,
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions wallets.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ type GeneratedWallet struct {

// List Wallets

func (b *BitGo) ListWallets(params *ListParams) (list ListWallets, err error) {
func (b *BitGo) ListWallets(params ListParams) (list ListWallets, err error) {
err = b.get(
fmt.Sprintf("%s/wallet",
b.coin),
Expand Down Expand Up @@ -171,7 +171,7 @@ type GetWalletParams struct {
AllTokens bool `url:"allTokens,omitempty"`
}

func (b *BitGo) GetWallet(walletId string, params *GetWalletParams) (wallet Wallet, err error) {
func (b *BitGo) GetWallet(walletId string, params GetWalletParams) (wallet Wallet, err error) {
err = b.get(
fmt.Sprintf("%s/wallet/%s",
b.coin,
Expand Down
4 changes: 2 additions & 2 deletions wallets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestListWallets(t *testing.T) {
coin, _ := getTestCoin(t)

_, err := coin.ListWallets(&ListParams{
_, err := coin.ListWallets(ListParams{
Limit: 3,
AllTokens: true,
})
Expand Down Expand Up @@ -39,7 +39,7 @@ func TestGenerateWallet(t *testing.T) {
func TestGetWallet(t *testing.T) {
coin, params := getTestCoin(t)

_, err := coin.GetWallet(params.WalletId, &GetWalletParams{
_, err := coin.GetWallet(params.WalletId, GetWalletParams{
AllTokens: true,
})
if err != nil {
Expand Down
51 changes: 49 additions & 2 deletions webhook-notifications.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,58 @@
package bitgo

// TODO
import (
"fmt"
"time"
)

type Webhook struct {
type WebhookEvent struct {
Hash string `json:"hash"`
Transfer string `json:"transfer"`
Coin string `json:"coin"`
Type string `json:"type"`
Wallet string `json:"wallet"`
}

type Webhook struct {
ID string `json:"id"`
Label string `json:"label"`
Created time.Time `json:"created"`
WalletID string `json:"walletId"`
Coin string `json:"coin"`
Type string `json:"type"`
URL string `json:"url"`
Version int `json:"version"`
}

type Webhooks []Webhook

type ListWebhooks struct {
Webhooks Webhooks `json:"webhooks"`
}

// List Wallet Webhooks

func (b *BitGo) ListWalletWebhooks(walletId string, params GetWalletParams) (webhooks Webhooks, err error) {
responce := ListWebhooks{}
err = b.get(
fmt.Sprintf("%s/wallet/%s/webhooks",
b.coin,
walletId),
nil,
&responce)
return responce.Webhooks, err
}

// Add Wallet Webhook

// Remove Wallet Webhook

// Simulate Wallet Webhook

// List User Webhooks

// Add User Webhook

// Remove User Webhook

// Simulate User Webhook
30 changes: 30 additions & 0 deletions webhook-notifications_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package bitgo

import (
"testing"
)

// List Wallet Webhooks

func TestListWalletWebhooks(t *testing.T) {
coin, params := getTestCoin(t)

_, err := coin.ListWalletWebhooks(params.WalletId, GetWalletParams{AllTokens: true})
if err != nil {
t.Fatal(err.Error())
}
}

// Add Wallet Webhook

// Remove Wallet Webhook

// Simulate Wallet Webhook

// List User Webhooks

// Add User Webhook

// Remove User Webhook

// Simulate User Webhook

0 comments on commit e3d501b

Please sign in to comment.