forked from bpicode/fritzctl
/
api_phone.go
59 lines (52 loc) · 1.39 KB
/
api_phone.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
package fritz
import (
"github.com/bpicode/fritzctl/httpread"
"github.com/pkg/errors"
)
// NewPhone creates a client for interaction with the FB phone sector.
func NewPhone(client *Client) Phone {
return &phone{client: client}
}
// Phone describes the supported operations.
type Phone interface {
Calls() ([]Call, error)
}
// Call contains the data for one phone call record.
// codebeat:disable[TOO_MANY_IVARS]
type Call struct {
Type string // "1"=incoming, "2"=missed call, "3"=unknown, "4"=outgoing.
Date string
Caller string
PhoneNumber string
Extension string
OwnPhoneNumber string
Duration string
}
// codebeat:enable[TOO_MANY_IVARS]
type phone struct {
client *Client
}
// Calls reads the phone call record list from FB.
func (p *phone) Calls() ([]Call, error) {
url := p.client.query().path(phoneListURI).query("csv", "").build()
records, err := httpread.Csv(p.client.getf(url), ';')
if err != nil {
return nil, errors.Wrap(err, "unable read data for phone calls")
}
var calls []Call
for _, r := range records[2:] { // Skip first two lines.
calls = append(calls, convertRecordToCall(r))
}
return calls, err
}
func convertRecordToCall(r []string) Call {
return Call{
Type: r[0],
Date: r[1],
Caller: r[2],
PhoneNumber: r[3],
Extension: r[4],
OwnPhoneNumber: r[5],
Duration: r[6],
}
}