-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfritz.go
111 lines (91 loc) · 3.46 KB
/
fritz.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
package fritz
import (
"github.com/fabiankachlock/fritz-api/pkg/api"
"github.com/fabiankachlock/fritz-api/pkg/request"
"github.com/fabiankachlock/fritz-api/pkg/response"
)
type FritzBoxClient struct {
client *api.Client
}
// NewClient creates a new FritzBoxClient
func NewClient(boxUrl string) FritzBoxClient {
return FritzBoxClient{client: api.NewClient(boxUrl)}
}
// Login logs in to the FritzBox
func (f *FritzBoxClient) Login(username string, password string) error {
return f.client.Login(username, password)
}
// Logout logs out of the FritzBox
func (f *FritzBoxClient) Logout() error {
return f.client.Logout()
}
// CheckLogin checks if the client is logged in
func (f *FritzBoxClient) CheckLogin() error {
return f.client.CheckLogin()
}
// GetSession returns the current session
func (f *FritzBoxClient) GetSession() (response.SessionInfo, error) {
return f.client.GetSession()
}
// GetData returns the response body for a custom data request
func (f *FritzBoxClient) GetData(request request.DataRequest) ([]byte, error) {
return f.client.RequestData(request)
}
// GetDataJson returns the response body as marshalled json for a custom data request as a map
func (f *FritzBoxClient) GetDataJson(request request.DataRequest) (map[string]interface{}, error) {
return PerformRequestRaw(f, request)
}
// GetHomeNet returns the data for the home network page
func (f *FritzBoxClient) GetHomeNet() (response.DataResponse[response.HomeNet], error) {
return PerformRequest[response.HomeNet](f, request.MeshRequest)
}
func (f *FritzBoxClient) GetNetworkDevices() (response.DataResponse[response.NetDev], error) {
return PerformRequest[response.NetDev](f, request.NetworkDevicesRequest)
}
func (f *FritzBoxClient) GetNetworkUsage() (response.NetCnt, error) {
return PerformRequestCustom[response.NetCnt](f, request.NetworkUsageRequest)
}
func (f *FritzBoxClient) GetEnergyUsage() (response.Energy, error) {
return PerformRequestCustom[response.Energy](f, request.EnergyUsageRequest)
}
func (f *FritzBoxClient) GetSystemLogs(filter response.LogFilter) (response.DataResponse[response.Log], error) {
params := map[string]string{request.RequestParamFilter: string(filter)}
return PerformRequest[response.Log](f, request.WithParams(request.SystemLogsRequest, params))
}
// PerformRequest is a wrapper that performs a request and unmarshals the response as data response
func PerformRequest[T any](f *FritzBoxClient, req request.DataRequest) (response.DataResponse[T], error) {
body, err := f.client.RequestData(req)
if err != nil {
return response.DataResponse[T]{}, err
}
json, err := response.UnmarshalAs[T](body)
if err != nil {
return response.DataResponse[T]{}, err
}
return json, nil
}
// PerformRequestCustom is a wrapper that performs a request and unmarshals the response
func PerformRequestCustom[T any](f *FritzBoxClient, req request.DataRequest) (T, error) {
var empty T
body, err := f.client.RequestData(req)
if err != nil {
return empty, err
}
json, err := response.UnmarshalCustomAs[T](body)
if err != nil {
return empty, err
}
return json, nil
}
// PerformRequestRaw is a wrapper that performs a request and unmarshals the response as raw json
func PerformRequestRaw(f *FritzBoxClient, req request.DataRequest) (map[string]interface{}, error) {
body, err := f.client.RequestData(req)
if err != nil {
return map[string]interface{}{}, err
}
rawJson, err := response.UnmarshalRaw(body)
if err != nil {
return map[string]interface{}{}, err
}
return rawJson, nil
}