-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
95 lines (72 loc) · 1.77 KB
/
util.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
package climacell
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
)
func validLatitude(latitude float64) bool {
return -59.9 <= latitude && latitude <= 59.9
}
func validLongitude(longitude float64) bool {
return -180 <= longitude && longitude <= 180
}
func getURL(baseURL, endpoint string) (*url.URL, error) {
u, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
e, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
return u.ResolveReference(e), nil
}
func floatToString(number float64) string {
return fmt.Sprintf("%g", number)
}
func joinFields(fields []field, sep string) string {
var fieldNames []string
for _, f := range fields {
fieldNames = append(fieldNames, f.String())
}
return strings.Join(fieldNames, sep)
}
func checkHTTPError(resp *http.Response, endpoint string) error {
if resp.StatusCode == 200 {
return nil
}
var httpError HTTPError
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&httpError); err != nil {
return err
}
if resp.StatusCode/100 == 4 || resp.StatusCode/100 == 5 {
if resp.StatusCode == 400 {
// Bad request
return newBadRequestError(endpoint, httpError.Message)
}
if resp.StatusCode == 401 {
// Unauthorized
return newUnauthorizedError(endpoint, httpError.Message)
}
if resp.StatusCode == 403 {
// Forbidden
return newForbiddenError(endpoint, httpError.Message)
}
if resp.StatusCode == 404 {
// Not found
return newNotFoundError(endpoint, httpError.Message)
}
if resp.StatusCode == 429 {
// Too many requests
return newTooManyRequestError(endpoint, httpError.Message)
}
if resp.StatusCode == 500 {
// Internal server error
return newInternalServerError(endpoint, httpError.Message)
}
}
return &httpError
}