-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
70 lines (58 loc) · 1.42 KB
/
helpers.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
package gocardless
import (
"fmt"
"strings"
"time"
)
func RequestHeadersWithAuth(token string) map[string]string {
authHeaders := map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", token),
}
// merge authHeaders and RequestHeaders
for k, v := range RequestHeaders() {
authHeaders[k] = v
}
return authHeaders
}
func RequestHeaders() map[string]string {
return map[string]string{
"Content-Type": "application/json",
}
}
func BuildQueryURL(path string, queryParams map[string]string) string {
queryURL := path
if len(queryParams) > 0 {
queryURL += "?"
for k, v := range queryParams {
queryURL += fmt.Sprintf("%s=%s&", k, v)
}
queryURL = queryURL[:len(queryURL)-1]
}
return queryURL
}
type TimeWithTimeZoneInfo struct {
time.Time
}
const timeWithTimeZoneInfo = "2006-01-02T15:04:05.999999"
func (ct *TimeWithTimeZoneInfo) UnmarshalJSON(b []byte) error {
str := strings.Trim(string(b), `"`)
t, err := time.Parse(timeWithTimeZoneInfo, str)
if err != nil {
return fmt.Errorf("failed to parse time: %w", err)
}
ct.Time = t
return nil
}
type TimeWithTimeZoneInfoZ struct {
time.Time
}
const timeWithTimeZoneInfoZ = "2006-01-02T15:04:05.999999Z"
func (ct *TimeWithTimeZoneInfoZ) UnmarshalJSON(b []byte) error {
str := strings.Trim(string(b), `"`)
t, err := time.Parse(timeWithTimeZoneInfoZ, str)
if err != nil {
return fmt.Errorf("failed to parse time: %w", err)
}
ct.Time = t
return nil
}