This repository has been archived by the owner on May 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
118 lines (95 loc) · 2.88 KB
/
api.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
113
114
115
116
117
118
package enedis
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/ViBiOh/httputils/pkg/errors"
"github.com/ViBiOh/httputils/pkg/request"
)
const (
oneDay = 24 * time.Hour
)
// Login triggers login
func (a *App) Login() error {
if a.email == "" || a.password == "" {
return errors.New("no credentials provided")
}
values := url.Values{}
values.Add("IDToken1", a.email)
values.Add("IDToken2", a.password)
values.Add("SunQueryParamsString", "cmVhbG09cGFydGljdWxpZXJz")
values.Add("encoded", "true")
values.Add("gx_charset", "UTF-8")
ctx := context.Background()
_, _, headers, err := request.PostForm(ctx, loginURL, values, nil)
if err != nil {
return err
}
authCookies := strings.Builder{}
for _, cookie := range headers["Set-Cookie"] {
if !strings.Contains(cookie, "Domain=.enedis.fr") {
continue
}
if strings.Contains(cookie, "Expires=Thu, 01-Jan-1970 00:00:10 GMT") {
continue
}
if authCookies.Len() != 0 {
safeWrite(&authCookies, ";")
}
safeWrite(&authCookies, getCookieValue(cookie))
}
a.cookie = authCookies.String()
return nil
}
// GetData retrieve data
func (a *App) GetData(ctx context.Context, startDate string, first bool) (*Consumption, error) {
header := http.Header{}
header.Set("Cookie", a.cookie)
params := url.Values{}
params.Add("p_p_id", "lincspartdisplaycdc_WAR_lincspartcdcportlet")
params.Add("p_p_lifecycle", "2")
params.Add("p_p_state", "normal")
params.Add("p_p_mode", "view")
params.Add("p_p_resource_id", "urlCdcHeure")
params.Add("p_p_cacheability", "cacheLevelPage")
params.Add("p_p_col_id", "column-1")
params.Add("p_p_col_count", "2")
startTime, err := time.ParseInLocation(frenchDateFormat, startDate, a.location)
if err != nil {
return nil, errors.WithStack(err)
}
endDate := startTime.Add(oneDay).Format(frenchDateFormat)
values := url.Values{}
params.Add("_lincspartdisplaycdc_WAR_lincspartcdcportlet_dateDebut", startDate)
params.Add("_lincspartdisplaycdc_WAR_lincspartcdcportlet_dateFin", endDate)
body, status, headers, err := request.PostForm(ctx, fmt.Sprintf("%s%s", consumeURL, params.Encode()), values, header)
if err != nil || status == http.StatusFound {
if first {
a.appendSessionCookie(headers)
return a.GetData(ctx, startDate, false)
}
if err == nil {
return nil, errors.New("unable to authent to enedis on the second try")
}
return nil, err
}
payload, err := request.ReadBody(body)
if err != nil {
return nil, err
}
var response Consumption
if err := json.Unmarshal(payload, &response); err != nil {
return nil, errors.WithStack(err)
}
if response.Etat != nil && response.Etat.Valeur == "erreur" {
return nil, errors.New("API error: %s", response.Etat.ErreurText)
}
for _, value := range response.Graphe.Data {
value.Timestamp = startTime.Add(time.Duration(30*(value.Ordre-1)) * time.Minute).Unix()
}
return &response, nil
}