forked from sfreiberg/gotwilio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
60 lines (50 loc) · 1.3 KB
/
common.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
package gotwilio
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
// ListResources contains Twilio paging information.
// See https://www.twilio.com/docs/usage/twilios-response#response-formats-list-paging-information
type ListResources struct {
Uri string `json:"uri"`
FirstPageUri string `json:"first_page_uri"`
NextPageUri string `json:"next_page_uri"`
PreviousPageUri string `json:"previous_page_uri"`
Page uint `json:"page"`
PageSize uint `json:"page_size"`
Faxes []*FaxResource `json:"faxes"`
Messages []*SmsResponse `json:"messages"`
t *Twilio
}
func (t *Twilio) newListResources() *ListResources {
lr := new(ListResources)
lr.t = t
return lr
}
func (l *ListResources) hasNext() bool {
return l.NextPageUri != ""
}
func (l *ListResources) next() (*Exception, error) {
resp, err := l.t.get(l.NextPageUri)
if err != nil {
return nil, err
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
exc := new(Exception)
err = json.Unmarshal(respBody, exc)
return exc, err
}
return nil, json.Unmarshal(respBody, l)
}
func setPageSize(uri string, size int) string {
q := &url.Values{}
q.Set("PageSize", strconv.Itoa(size))
return uri + "?" + q.Encode()
}