forked from blockfrost/blockfrost-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_metrics.go
79 lines (66 loc) · 1.82 KB
/
api_metrics.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
package blockfrost
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// Metric describes the Blockfrost usage metrics
type Metric struct {
// Sum of all calls for a particular day
Calls int `json:"calls"`
// Starting time of the call count interval (ends midnight UTC) in UNIX time
Time int `json:"time"`
}
// MetricsEndpoint
type MetricsEndpoint struct {
// Sum of all calls for a particular day and endpoint
Calls int `json:"calls"`
// Endpoint parent name
Endpoint string `json:"endpoint"`
// Starting time of the call count interval (ends midnight UTC) in UNIX time
Time int `json:"time"`
}
// Metrics returns the history of your Blockfrost usage metrics in the past 30 days.
func (c *apiClient) Metrics(ctx context.Context) (mes []Metric, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s", c.server, resourceMetrics))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&mes); err != nil {
return
}
return mes, nil
}
// MetricsEndpoints returns history of your blockfrost usage metrics
// History of your Blockfrost usage metrics per endpoint in the past 30 days.
func (c *apiClient) MetricsEndpoints(ctx context.Context) (mes []MetricsEndpoint, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s", c.server, resourceMetricsEndpoint))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&mes)
if err != nil {
return
}
return mes, nil
}