This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
call_list.go
99 lines (75 loc) · 2.29 KB
/
call_list.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
package twiliogo
import (
"encoding/json"
"net/url"
)
type CallList struct {
Client Client
Start int `json:"start"`
Total int `json:"total"`
NumPages int `json:"num_pages"`
Page int `json:"page"`
PageSize int `json:"page_size"`
End int `json:"end"`
Uri string `json:"uri"`
FirstPageUri string `json:"first_page_uri"`
LastPageUri string `json:"last_page_uri"`
NextPageUri string `json:"next_page_uri"`
PreviousPageUri string `json"previous_page_uri"`
Calls []Call `json:"calls"`
}
func GetCallList(client Client, optionals ...Optional) (*CallList, error) {
var callList *CallList
params := url.Values{}
for _, optional := range optionals {
param, value := optional.GetParam()
params.Set(param, value)
}
body, err := client.get(nil, "/Calls.json")
if err != nil {
return nil, err
}
callList = new(CallList)
callList.Client = client
err = json.Unmarshal(body, callList)
return callList, err
}
func (callList *CallList) GetCalls() []Call {
return callList.Calls
}
func (currentCallList *CallList) HasNextPage() bool {
return currentCallList.NextPageUri != ""
}
func (currentCallList *CallList) NextPage() (*CallList, error) {
if !currentCallList.HasNextPage() {
return nil, Error{"No next page"}
}
return currentCallList.getPage(currentCallList.NextPageUri)
}
func (currentCallList *CallList) HasPreviousPage() bool {
return currentCallList.PreviousPageUri != ""
}
func (currentCallList *CallList) PreviousPage() (*CallList, error) {
if !currentCallList.HasPreviousPage() {
return nil, Error{"No previous page"}
}
return currentCallList.getPage(currentCallList.NextPageUri)
}
func (currentCallList *CallList) FirstPage() (*CallList, error) {
return currentCallList.getPage(currentCallList.FirstPageUri)
}
func (currentCallList *CallList) LastPage() (*CallList, error) {
return currentCallList.getPage(currentCallList.LastPageUri)
}
func (currentCallList *CallList) getPage(uri string) (*CallList, error) {
var callList *CallList
client := currentCallList.Client
body, err := client.get(nil, uri)
if err != nil {
return callList, err
}
callList = new(CallList)
callList.Client = client
err = json.Unmarshal(body, callList)
return callList, err
}