-
Notifications
You must be signed in to change notification settings - Fork 1
/
events.go
112 lines (90 loc) · 2.8 KB
/
events.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2022 Ralf Geschke. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package golrackpi
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/geschke/golrackpi/internal/timefix"
)
// EventData specifies the structure of the response returned by a request to the "events" endpoint
type EventData struct {
Description string `json:"description"`
Category string `json:"category"`
LongDescription string `json:"long_description"`
StartTime timefix.InverterTime `json:"start_time"`
Group string `json:"group"`
EndTime timefix.InverterTime `json:"end_time"`
Code int `json:"code"`
IsActive bool `json:"is_active"`
}
// EventsWithParam returns the latest events with localized descriptions. It returns a slice of EventData type. It takes as arguments
// the language string (currently available de-de, en-gb, es-es, fr-fr, hu-hu, it-it, nl-nl, pl-pl, pt-pt, cs-cz, el-gr and zh-cn) and
// the maximum number of events (default: 10)
func (c *AuthClient) EventsWithParam(language string, max int) ([]EventData, error) {
jsonResult := []EventData{}
if language == "" {
language = "en-gb"
}
if max <= 0 {
max = 10
}
payload := struct {
Language string `json:"language"`
Max int `json:"max"`
}{
Language: language,
Max: max,
}
b, err := json.Marshal(payload)
if err != nil {
return jsonResult, err
}
client := http.Client{}
request, err := http.NewRequest("POST", c.getUrl("/api/v1/events/latest"), bytes.NewBuffer(b))
if err != nil {
return jsonResult, err
}
request.Header.Add("Content-Type", "application/json")
request.Header.Add("authorization", "Session "+c.SessionId)
response, err := client.Do(request)
if err != nil {
return jsonResult, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return jsonResult, err
}
err = json.Unmarshal(body, &jsonResult)
if err != nil {
return jsonResult, err
}
return jsonResult, nil
}
// Events returns the latest events as a slice of EventData type
func (c *AuthClient) Events() ([]EventData, error) {
jsonResult := []EventData{}
client := http.Client{}
request, err := http.NewRequest("GET", c.getUrl("/api/v1/events/latest"), nil)
if err != nil {
return jsonResult, err
}
request.Header.Add("authorization", "Session "+c.SessionId)
response, err := client.Do(request)
if err != nil {
return jsonResult, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return jsonResult, err
}
err = json.Unmarshal(body, &jsonResult)
if err != nil {
return jsonResult, err
}
return jsonResult, nil
}