-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoints.go
51 lines (40 loc) · 981 Bytes
/
endpoints.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
package pirateweather
import (
"fmt"
"net/url"
)
// Construct the forecast URL
func (r ForecastRequest) url(token string) string {
endpoint := BaseUrl + "/forecast/" + fmt.Sprintf("%s/%g,%g", token, r.Latitude, r.Longitude)
if !r.Time.IsZero() {
endpoint += fmt.Sprintf(",%d", r.Time.Unix())
}
return endpoint + encodeUrlParameters(r.Options.urlValues())
}
// Create optional url Values for encoding into query string parameters (exclude=hourly&units=si)
func (o ForecastRequestOptions) urlValues() url.Values {
q := url.Values{}
if o.Exclude != "" {
q.Add("exclude", o.Exclude)
}
if o.Extend != "" {
q.Add("extend", o.Extend)
}
if o.Lang != "" {
q.Add("lang", o.Lang)
}
if o.Units != "" {
q.Add("units", o.Units)
}
return q
}
func encodeUrlParameters(values url.Values) string {
queryString := values.Encode()
if queryString == "" {
return queryString
}
return "?" + queryString
}
type urlBuilder interface {
url(token string) string
}