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
/
Copy pathmessage_list.go
99 lines (75 loc) · 2.49 KB
/
message_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 MessageList 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"`
Messages []Message `json:"sms_messages"`
}
func GetMessageList(client Client, optionals ...Optional) (*MessageList, error) {
var messageList *MessageList
params := url.Values{}
for _, optional := range optionals {
param, value := optional.GetParam()
params.Set(param, value)
}
body, err := client.get(params, "/SMS/Messages.json")
if err != nil {
return messageList, err
}
messageList = new(MessageList)
messageList.Client = client
err = json.Unmarshal(body, messageList)
return messageList, err
}
func (m *MessageList) GetMessages() []Message {
return m.Messages
}
func (currentMessageList *MessageList) HasNextPage() bool {
return currentMessageList.NextPageUri != ""
}
func (currentMessageList *MessageList) NextPage() (*MessageList, error) {
if !currentMessageList.HasNextPage() {
return nil, Error{"No next page"}
}
return currentMessageList.getPage(currentMessageList.NextPageUri)
}
func (currentMessageList *MessageList) HasPreviousPage() bool {
return currentMessageList.PreviousPageUri != ""
}
func (currentMessageList *MessageList) PreviousPage() (*MessageList, error) {
if !currentMessageList.HasPreviousPage() {
return nil, Error{"No previous page"}
}
return currentMessageList.getPage(currentMessageList.NextPageUri)
}
func (currentMessageList *MessageList) FirstPage() (*MessageList, error) {
return currentMessageList.getPage(currentMessageList.FirstPageUri)
}
func (currentMessageList *MessageList) LastPage() (*MessageList, error) {
return currentMessageList.getPage(currentMessageList.LastPageUri)
}
func (currentMessageList *MessageList) getPage(uri string) (*MessageList, error) {
var messageList *MessageList
client := currentMessageList.Client
body, err := client.get(nil, uri)
if err != nil {
return messageList, err
}
messageList = new(MessageList)
messageList.Client = client
err = json.Unmarshal(body, messageList)
return messageList, err
}