-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretrieve_wallet.go
More file actions
128 lines (103 loc) · 3.67 KB
/
retrieve_wallet.go
File metadata and controls
128 lines (103 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (C) 2021 Creditor Corp. Group.
// See LICENSE for copying information.
package venly
import (
"context"
"encoding/json"
"net/http"
"github.com/zeebo/errs"
)
// RetrieveWalletRequest fields that required for retrieve wallet request.
type RetrieveWalletRequest struct {
WalletID string `json:"wallet_id"`
}
// RetrieveWalletResponse fields that returns from retrieve wallet.
type RetrieveWalletResponse struct {
Success bool `json:"success"`
Result struct {
ID string `json:"id"`
Address string `json:"address"`
WalletType string `json:"walletType"`
SecretType string `json:"secretType"`
CreatedAt string `json:"createdAt"`
Archived bool `json:"archived"`
Alias string `json:"alias"`
Description string `json:"description"`
Primary bool `json:"primary"`
HasCustomPin bool `json:"hasCustomPin"`
Identifier string `json:"identifier"`
Balance struct {
Available bool `json:"available"`
SecretType string `json:"secretType"`
Balance float64 `json:"balance"`
GasBalance float64 `json:"gasBalance"`
Symbol string `json:"symbol"`
GasSymbol string `json:"gasSymbol"`
RawBalance string `json:"rawBalance"`
RawGasBalance string `json:"rawGasBalance"`
Decimals int `json:"decimals"`
} `json:"balance"`
} `json:"result"`
}
// RetrieveWallet retrieves Venly wallet.
func (client *Client) RetrieveWallet(ctx context.Context, accessToken string, r RetrieveWalletRequest) (response RetrieveWalletResponse, err error) {
req, err := http.NewRequest(http.MethodGet, client.config.DefaultURL+"wallets/"+r.WalletID, nil)
if err != nil {
return RetrieveWalletResponse{}, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+accessToken)
resp, err := client.http.Do(req.WithContext(ctx))
if err != nil {
return RetrieveWalletResponse{}, err
}
defer func() {
err = errs.Combine(err, resp.Body.Close())
}()
if resp.StatusCode != http.StatusOK {
errorResp := ErrorResponse{}
if err = json.NewDecoder(resp.Body).Decode(&errorResp); err != nil {
return RetrieveWalletResponse{}, err
}
if !errorResp.Success {
return RetrieveWalletResponse{}, errs.New(errorResp.Errors[0].Code)
}
return RetrieveWalletResponse{}, errs.New(resp.Status)
}
var retrieveWalletResponse RetrieveWalletResponse
if err = json.NewDecoder(resp.Body).Decode(&retrieveWalletResponse); err != nil {
return RetrieveWalletResponse{}, err
}
return retrieveWalletResponse, nil
}
// RetrieveWallets retrieves Venly wallets.
func (client *Client) RetrieveWallets(ctx context.Context, accessToken string) (response RetrieveWalletResponse, err error) {
req, err := http.NewRequest(http.MethodGet, client.config.DefaultURL+"wallets", nil)
if err != nil {
return RetrieveWalletResponse{}, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+accessToken)
resp, err := client.http.Do(req.WithContext(ctx))
if err != nil {
return RetrieveWalletResponse{}, err
}
defer func() {
err = errs.Combine(err, resp.Body.Close())
}()
if resp.StatusCode != http.StatusOK {
errorResp := ErrorResponse{}
if err = json.NewDecoder(resp.Body).Decode(&errorResp); err != nil {
return RetrieveWalletResponse{}, err
}
if !errorResp.Success {
return RetrieveWalletResponse{}, errs.New(errorResp.Errors[0].Code)
}
return RetrieveWalletResponse{}, errs.New(resp.Status)
}
var retrieveWalletResponse RetrieveWalletResponse
if err = json.NewDecoder(resp.Body).Decode(&retrieveWalletResponse); err != nil {
return RetrieveWalletResponse{}, err
}
return retrieveWalletResponse, nil
}