-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.go
More file actions
108 lines (86 loc) · 2.3 KB
/
utils.go
File metadata and controls
108 lines (86 loc) · 2.3 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
package glassnode
import (
"fmt"
"net/url"
"path"
)
func constructURL(api Client, options *APIOptionsList) (string, error) {
// --------
// Base URL
// --------
baseURL, err := url.Parse(api.BaseURL)
if err != nil {
return "", fmt.Errorf("[GetMetricData] couldn't parse url: %s", err.Error())
}
baseURL.Path = path.Join(baseURL.Path, MetricsPrefix)
// -------------
// Data Category
// -------------
if options.Category == "" {
return "", fmt.Errorf("APIOptionsList.Category appears to be empty but is required")
}
baseURL.Path = path.Join(baseURL.Path, options.Category)
// ---------------
// Specific metric
// ---------------
if options.Metric == "" {
return "", fmt.Errorf("APIOptionsList.Metric appears to be empty but is required")
}
baseURL.Path = path.Join(baseURL.Path, options.Metric)
finalParams, err := makeParams(api.apiKey, options)
if err != nil {
return "", fmt.Errorf("[GetMetricData] couldn't prepare parameters: %s", err.Error())
}
baseURL.RawQuery = finalParams.Encode()
return baseURL.String(), nil
}
func makeParams(apiKey string, options *APIOptionsList) (*url.Values, error) {
// ---------------
// Parsing helpers
// ---------------
unrefinedParams := make(map[string]string)
finalParams := url.Values{}
//
// apply raw params, if any
//
for key, value := range options.DirectMapping {
unrefinedParams[key] = value
}
//
// required params
//
if apiKey == "" {
return nil, fmt.Errorf("api key appears to be empty but is required")
}
unrefinedParams["api_key"] = apiKey
if options.Asset != "" {
unrefinedParams["a"] = options.Asset
}
if unrefinedParams["a"] == "" {
return nil, fmt.Errorf("parameter a (Asset) appears to be empty but is required")
}
//
// optional params
//
if options.Since != 0 {
unrefinedParams["s"] = fmt.Sprint(options.Since)
}
if options.Until != 0 {
unrefinedParams["u"] = fmt.Sprint(options.Until)
}
if options.Frequency != "" {
unrefinedParams["i"] = fmt.Sprint(options.Frequency)
}
//
// Unsupported options:
if unrefinedParams["f"] != "" {
return nil, fmt.Errorf("parameter f (Format) shouldn't be specified")
}
// -----------------------
// Construct the final URL
// -----------------------
for key, value := range unrefinedParams {
finalParams.Add(key, value)
}
return &finalParams, nil
}