forked from xperimental/netatmo-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.go
40 lines (32 loc) · 794 Bytes
/
weather.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
package netatmo
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// Read returns the list of stations owned by the user and their modules
func (c *Client) Read() (*DeviceCollection, error) {
if c.httpClient == nil {
return nil, ErrNotAuthenticated
}
data := url.Values{"app_type": {"app_station"}}
req, err := http.NewRequest("GET", deviceURL, nil)
if err != nil {
return nil, err
}
req.URL.RawQuery = data.Encode()
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad HTTP return code: %d", resp.StatusCode)
}
result := &DeviceCollection{}
if err = json.NewDecoder(resp.Body).Decode(result); err != nil {
return nil, err
}
return result, nil
}