-
Notifications
You must be signed in to change notification settings - Fork 1
/
dashboard.go
55 lines (44 loc) · 1.16 KB
/
dashboard.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
package action
import (
"context"
"io"
"strings"
"time"
"github.com/pkg/errors"
"github.com/wolftotem4/shaolin-ben-don/internal/client"
apperrors "github.com/wolftotem4/shaolin-ben-don/internal/errors"
"github.com/wolftotem4/shaolin-ben-don/internal/parser"
)
type DashboardAction struct {
Client client.Client
}
type DashboardData struct {
Interface int
}
func (action *DashboardAction) Update(ctx context.Context) (*DashboardData, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
res, err := action.Client.Call(ctx, "/do/", client.Webpage)
if err != nil {
return nil, errors.WithStack(err)
}
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, errors.WithStack(err)
}
defer res.Body.Close()
if strings.HasPrefix(res.Request.URL.EscapedPath(), "/do/login") {
form, err := parser.ParseLoginHTML(body)
if err != nil {
return nil, errors.WithStack(err)
}
return nil, apperrors.NewLoginRequiredError("login required", form)
}
interfaceValue, err := parser.ExtractInterface(body)
if err != nil {
return nil, errors.WithStack(err)
}
return &DashboardData{
Interface: interfaceValue,
}, nil
}