diff --git a/README.md b/README.md index af87481..1b4c399 100644 --- a/README.md +++ b/README.md @@ -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()) } diff --git a/examples/main.go b/examples/main.go index 65e9163..d2f0221 100644 --- a/examples/main.go +++ b/examples/main.go @@ -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()) } diff --git a/main.go b/main.go index 51d711b..643df17 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "io/ioutil" + "log" "net/http" "time" @@ -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 { @@ -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") } @@ -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, } } @@ -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) @@ -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) @@ -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 } diff --git a/main_test.go b/test_utils.go similarity index 93% rename from main_test.go rename to test_utils.go index 476e6ca..f5693b6 100644 --- a/main_test.go +++ b/test_utils.go @@ -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) { diff --git a/wallet-operations-basic.go b/wallet-operations-basic.go index 5da8ec7..9f06236 100644 --- a/wallet-operations-basic.go +++ b/wallet-operations-basic.go @@ -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, diff --git a/wallet-operations-basic_test.go b/wallet-operations-basic_test.go index af2c567..afd654b 100644 --- a/wallet-operations-basic_test.go +++ b/wallet-operations-basic_test.go @@ -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 { diff --git a/wallets.go b/wallets.go index d35f99e..c859e29 100644 --- a/wallets.go +++ b/wallets.go @@ -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), @@ -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, diff --git a/wallets_test.go b/wallets_test.go index 661b429..ba764ac 100644 --- a/wallets_test.go +++ b/wallets_test.go @@ -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, }) @@ -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 { diff --git a/webhook-notifications.go b/webhook-notifications.go index 1f479a8..f20f529 100644 --- a/webhook-notifications.go +++ b/webhook-notifications.go @@ -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 diff --git a/webhook-notifications_test.go b/webhook-notifications_test.go new file mode 100644 index 0000000..6b5fad0 --- /dev/null +++ b/webhook-notifications_test.go @@ -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