-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhist.go
118 lines (95 loc) · 2.71 KB
/
hist.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
// Copyright (c) 2024 Neomantra Corp
package dbn_hist
import (
"encoding/base64"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
// A **half**-closed date interval with an inclusive start date and an exclusive end date.
type DateRange struct {
// The start date (inclusive).
Start time.Time `json:"start"`
// The end date (exclusive).
End time.Time `json:"end"`
}
type RequestError struct {
Case string `json:"case"`
Message string `json:"message"`
StatusCode int `json:"status_code"`
Docs string `json:"docs,omitempty"`
Payload string `json:"payload,omitempty"`
}
type RequestErrorResp struct {
Detail RequestError `json:"detail"`
}
//////////////////////////////////////////////////////////////////////////////
func databentoGetRequest(urlStr string, apiKey string) ([]byte, error) {
apiUrl, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", apiUrl.String(), nil)
if err != nil {
return nil, err
}
auth := base64.StdEncoding.EncodeToString([]byte(apiKey + ":"))
req.Header.Add("Authorization", "Basic "+auth)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
badStatusCode := (resp.StatusCode != http.StatusOK)
body, err := io.ReadAll(resp.Body)
if err != nil {
if badStatusCode {
return nil, fmt.Errorf("HTTP %d %s %s %w", resp.StatusCode, resp.Status, string(body), err)
}
return nil, err
}
if badStatusCode {
return nil, fmt.Errorf("HTTP %d %s %s", resp.StatusCode, resp.Status, string(body))
}
return body, nil
}
//////////////////////////////////////////////////////////////////////////////
func databentoPostFormRequest(urlStr string, apiKey string, form url.Values, accept string) ([]byte, error) {
apiUrl, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
formBody := strings.NewReader(form.Encode())
req, err := http.NewRequest("POST", apiUrl.String(), formBody)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if accept != "" {
req.Header.Set("Accept-Encoding", accept)
}
auth := base64.StdEncoding.EncodeToString([]byte(apiKey + ":"))
req.Header.Add("Authorization", "Basic "+auth)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
badStatusCode := (resp.StatusCode != http.StatusOK)
body, err := io.ReadAll(resp.Body)
if err != nil {
if badStatusCode {
return nil, fmt.Errorf("HTTP %d %s %s %w", resp.StatusCode, resp.Status, string(body), err)
}
return nil, err
}
if badStatusCode {
return nil, fmt.Errorf("HTTP %d %s %s", resp.StatusCode, resp.Status, string(body))
}
return body, nil
}