-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfritzgo.go
63 lines (51 loc) · 1.37 KB
/
fritzgo.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
// Package fritzgo contains CLI tools to access fritz data.
package fritzgo
import (
"context"
"fmt"
"github.com/antiphp/fritzgo/pkg/fritztypes"
)
// Client represents an HTTP client to access fritz data.
type Client interface {
ListUsers(context.Context) ([]fritztypes.User, error)
Info(context.Context) (fritztypes.Info, error)
}
// Renderer render fritz data.
type Renderer interface {
ListUsers([]fritztypes.User) error
Info(fritztypes.Info) error
}
// FritzGo is the core application to retrieve, manage and render fritz data.
type FritzGo struct {
fritzBox Client
render Renderer
}
// New returns a new application.
func New(client Client, render Renderer) *FritzGo {
return &FritzGo{
fritzBox: client,
render: render,
}
}
// ListUsers retrieves and renders fritz users.
func (f *FritzGo) ListUsers(ctx context.Context) error {
users, err := f.fritzBox.ListUsers(ctx)
if err != nil {
return fmt.Errorf("getting users: %w", err)
}
if err = f.render.ListUsers(users); err != nil {
return fmt.Errorf("rendering users: %w", err)
}
return nil
}
// Info retrieves and renders basic information.
func (f *FritzGo) Info(ctx context.Context) error {
info, err := f.fritzBox.Info(ctx)
if err != nil {
return fmt.Errorf("retrieving info: %w", err)
}
if err = f.render.Info(info); err != nil {
return fmt.Errorf("retrieving info: %w", err)
}
return nil
}