-
Notifications
You must be signed in to change notification settings - Fork 71
/
current_group.go
98 lines (80 loc) · 1.99 KB
/
current_group.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
package openweathermap
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
)
// maximum count of several identifiers
// used in CurrentByIDs
const maxCityIDs = 20
// CurrentWeatherGroup struct contains list of the CurrentWeatherData
// structs for JSON to be unmarshaled into.
type CurrentWeatherGroup struct {
Count int `json:"count"`
List []*CurrentWeatherData `json:"list,omitempty"`
Unit string
Lang string
Key string
*Settings
}
// NewCurrentGroup returns a new CurrentWeatherGroup pointer with the supplied parameters
func NewCurrentGroup(unit, lang, key string, options ...Option) (*CurrentWeatherGroup, error) {
unitChoice := strings.ToUpper(unit)
langChoice := strings.ToUpper(lang)
g := &CurrentWeatherGroup{
Settings: NewSettings(),
}
if ValidDataUnit(unitChoice) {
g.Unit = DataUnits[unitChoice]
} else {
return nil, errUnitUnavailable
}
if ValidLangCode(langChoice) {
g.Lang = langChoice
} else {
return nil, errLangUnavailable
}
var err error
g.Key, err = setKey(key)
if err != nil {
return nil, err
}
if err := setOptions(g.Settings, options); err != nil {
return nil, err
}
return g, nil
}
// CurrentByIDs will provide the current weather as a list
// by the specified location identifiers
func (g *CurrentWeatherGroup) CurrentByIDs(ids ...int) error {
n := len(ids)
if n > maxCityIDs {
return errCountOfCityIDs
}
strIDs := make([]string, n)
for i, id := range ids {
strIDs[i] = strconv.Itoa(id)
}
id := strings.Join(strIDs, ",")
uri := fmt.Sprintf(groupURL, "appid=%s&id=%s&units=%s&lang=%s")
response, err := g.client.Get(fmt.Sprintf(uri, g.Key, id, g.Unit, g.Lang))
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode == http.StatusUnauthorized {
return errInvalidKey
}
if err = json.NewDecoder(response.Body).Decode(&g); err != nil {
return err
}
for _, w := range g.List {
w.Settings = g.Settings
w.Unit = g.Unit
w.Lang = g.Lang
w.Key = g.Key
}
return nil
}