1package twiliogo
2
3import (
4	"encoding/json"
5	"net/url"
6)
7
8type CallList struct {
9	Client          Client
10	Start           int    `json:"start"`
11	Total           int    `json:"total"`
12	NumPages        int    `json:"num_pages"`
13	Page            int    `json:"page"`
14	PageSize        int    `json:"page_size"`
15	End             int    `json:"end"`
16	Uri             string `json:"uri"`
17	FirstPageUri    string `json:"first_page_uri"`
18	LastPageUri     string `json:"last_page_uri"`
19	NextPageUri     string `json:"next_page_uri"`
20	PreviousPageUri string `json"previous_page_uri"`
21	Calls           []Call `json:"calls"`
22}
23
24func GetCallList(client Client, optionals ...Optional) (*CallList, error) {
25	var callList *CallList
26
27	params := url.Values{}
28
29	for _, optional := range optionals {
30		param, value := optional.GetParam()
31		params.Set(param, value)
32	}
33
34	body, err := client.get(nil, "/Calls.json")
35
36	if err != nil {
37		return nil, err
38	}
39
40	callList = new(CallList)
41	callList.Client = client
42	err = json.Unmarshal(body, callList)
43
44	return callList, err
45}
46
47func (callList *CallList) GetCalls() []Call {
48	return callList.Calls
49}
50
51func (currentCallList *CallList) HasNextPage() bool {
52	return currentCallList.NextPageUri != ""
53}
54
55func (currentCallList *CallList) NextPage() (*CallList, error) {
56	if !currentCallList.HasNextPage() {
57		return nil, Error{"No next page"}
58	}
59
60	return currentCallList.getPage(currentCallList.NextPageUri)
61}
62
63func (currentCallList *CallList) HasPreviousPage() bool {
64	return currentCallList.PreviousPageUri != ""
65}
66
67func (currentCallList *CallList) PreviousPage() (*CallList, error) {
68	if !currentCallList.HasPreviousPage() {
69		return nil, Error{"No previous page"}
70	}
71
72	return currentCallList.getPage(currentCallList.NextPageUri)
73}
74
75func (currentCallList *CallList) FirstPage() (*CallList, error) {
76	return currentCallList.getPage(currentCallList.FirstPageUri)
77}
78
79func (currentCallList *CallList) LastPage() (*CallList, error) {
80	return currentCallList.getPage(currentCallList.LastPageUri)
81}
82
83func (currentCallList *CallList) getPage(uri string) (*CallList, error) {
84	var callList *CallList
85
86	client := currentCallList.Client
87
88	body, err := client.get(nil, uri)
89
90	if err != nil {
91		return callList, err
92	}
93
94	callList = new(CallList)
95	callList.Client = client
96	err = json.Unmarshal(body, callList)
97
98	return callList, err
99}
100