-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
72 lines (59 loc) · 2.02 KB
/
request.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
package livebox
import (
"bytes"
"context"
"encoding/json"
"github.com/Tomy2e/livebox-api-client/api/request"
"github.com/Tomy2e/livebox-api-client/api/response"
)
// Request sends a request to the Livebox API. If the client is not yet
// authenticated, or the session is expired, the client will try to
// authenticate using the admin password given during the creation
// of the client.
func (c *Client) Request(ctx context.Context, req *request.Request, out interface{}) error {
// Try to authenticate if it's the first request.
if _, err := c.session.Renew(ctx, c.password, renewIfNotInitialized); err != nil {
return err
}
// Send the request, if the session is expired, the client will renew the session.
return c.request(ctx, req, out)
}
// request sends a request to the Livebox API. Before calling this function,
// the client must have been authenticated successfully in the past.
// This function handles reauthentication when the session expires.
// Reauthentication will only be attempted once.
func (c *Client) request(ctx context.Context, req *request.Request, out interface{}) error {
// Create request payload
payload, err := json.Marshal(req)
if err != nil {
return err
}
authAttempted := false
for {
// Create HTTP request with request payload
r, v, err := c.session.NewAuthenticatedRequest(ctx, bytes.NewReader(payload))
if err != nil {
return err
}
if _, err := c.client.SendRequest(ctx, r, out); err != nil {
// If reauthentication was already attempted, return error now.
if authAttempted {
return err
}
// Check if the server returned a permission denied error.
if response.IsPermissionDeniedError(err) {
// Try to renew the session if the version of the session that
// was used is still the current one.
if _, err := c.session.Renew(ctx, c.password, renewIfVersionIsCurrent(v)); err != nil {
return err
}
// Successful reauthentication. Retry request one more time.
authAttempted = true
continue
}
return err
}
break
}
return nil
}