-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwhatsonchain.go
120 lines (96 loc) · 2.8 KB
/
whatsonchain.go
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
/*
Package whatsonchain is the unofficial golang implementation for the whatsonchain.com API
Example:
```
// Create a new client:
client := whatsonchain.NewClient(whatsonchain.NetworkMain, nil, nil)
// Get a balance for an address:
balance, _ := client.AddressBalance("16ZqP5Tb22KJuvSAbjNkoiZs13mmRmexZA")
fmt.Println("confirmed balance", balance.Confirmed)
```
*/
package whatsonchain
import (
"bytes"
"context"
"io"
"io/ioutil"
"net/http"
)
// NewClient creates a new client for WOC requests
func NewClient(network NetworkType, clientOptions *Options, customHTTPClient HTTPInterface) ClientInterface {
// Sets the network, options and custom HTTP client
return createClient(network, clientOptions, customHTTPClient)
}
// request is a generic request wrapper that can be used without constraints
func (c *Client) request(ctx context.Context, url string, method string, payload []byte) (response string, err error) {
// Set reader
var bodyReader io.Reader
// Add post data if applicable
if method == http.MethodPost || method == http.MethodPut {
bodyReader = bytes.NewBuffer(payload)
c.LastRequest().PostData = string(payload)
}
// Store for debugging purposes
c.LastRequest().Method = method
c.LastRequest().URL = url
// Start the request
var request *http.Request
if request, err = http.NewRequestWithContext(
ctx, method, url, bodyReader,
); err != nil {
return
}
// Change the header (user agent is in case they block default Go user agents)
request.Header.Set("User-Agent", c.UserAgent())
// Set the content type on Method
if method == http.MethodPost || method == http.MethodPut {
request.Header.Set("Content-Type", "application/json")
}
// Set the API key if found
if len(c.apiKey) > 0 {
request.Header.Set(apiHeaderKey, c.apiKey)
}
// Fire the http request
var resp *http.Response
if resp, err = c.httpClient.Do(request); err != nil {
if resp != nil {
c.LastRequest().StatusCode = resp.StatusCode
}
return
}
// Close the response body
defer func() {
_ = resp.Body.Close()
}()
// Set the status
c.LastRequest().StatusCode = resp.StatusCode
// Read the body
var body []byte
if body, err = ioutil.ReadAll(resp.Body); err != nil {
return
}
// Return the raw JSON response
response = string(body)
return
}
// UserAgent will return the current user agent
func (c *Client) UserAgent() string {
return c.userAgent
}
// RateLimit will return the current configured rate limit
func (c *Client) RateLimit() int {
return c.rateLimit
}
// Network will return the network
func (c *Client) Network() NetworkType {
return c.network
}
// LastRequest will return the last request information
func (c *Client) LastRequest() *LastRequest {
return c.lastRequest
}
// HTTPClient will return the current HTTP client
func (c *Client) HTTPClient() HTTPInterface {
return c.httpClient
}