Skip to content

Commit 90cc0cc

Browse files
author
childish-sambino
authored
fix: refactor limit enforcement and fetching next page (#148)
1 parent b250c83 commit 90cc0cc

File tree

294 files changed

+4118
-6251
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

294 files changed

+4118
-6251
lines changed

client/page_util.go

Lines changed: 16 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ package client
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
7-
"log"
8-
"reflect"
9-
"regexp"
106
"strings"
117
)
128

@@ -29,91 +25,46 @@ func ReadLimits(pageSize *int, limit *int) int {
2925
}
3026
}
3127

32-
func GetNext(baseUrl string, response interface{}, curRecord *int, limit *int, getNextPage func(nextPageUri string) (interface{}, error)) (interface{}, error) {
33-
nextPageUrl, err := getNextPageAddress(baseUrl, response, curRecord, limit)
28+
func GetNext(baseUrl string, response interface{}, getNextPage func(nextPageUri string) (interface{}, error)) (interface{}, error) {
29+
nextPageUrl, err := getNextPageUrl(baseUrl, response)
3430
if err != nil {
3531
return nil, err
3632
}
3733

3834
return getNextPage(nextPageUrl)
3935
}
4036

41-
func GetPayload(baseUrl string, response interface{}) ([]interface{}, string, error) {
42-
payload := toMap(response)
43-
var data [][]interface{}
44-
for _, v := range payload {
45-
if v != nil {
46-
kind := reflect.TypeOf(v).Kind()
47-
switch kind {
48-
//look for non metadata info
49-
case reflect.Slice:
50-
if len(data) > 0 {
51-
//we expect this to be exactly 1
52-
return nil, "", errors.New("payload contains more than 1 record of type array")
53-
}
54-
data = append(data, v.([]interface{}))
55-
}
56-
}
57-
}
58-
59-
if len(data) == 1 {
60-
return data[0], getNextPageUrl(baseUrl, payload), nil
61-
}
62-
return nil, "", errors.New("could not retrieve payload from response")
63-
}
64-
65-
func toMap(s interface{}) map[string]interface{} {
37+
func toMap(s interface{}) (map[string]interface{}, error) {
6638
var payload map[string]interface{}
67-
test, errMarshal := json.Marshal(s)
68-
if errMarshal != nil {
69-
return nil
39+
data, err := json.Marshal(s)
40+
if err != nil {
41+
return nil, err
7042
}
7143

72-
errUnmarshal := json.Unmarshal(test, &payload)
73-
if errUnmarshal != nil {
74-
log.Print("Map creation error: ", errUnmarshal)
75-
return nil
44+
err = json.Unmarshal(data, &payload)
45+
if err != nil {
46+
return nil, err
7647
}
77-
return payload
48+
49+
return payload, err
7850
}
7951

80-
func getNextPageAddress(baseUrl string, response interface{}, curRecord *int, limit *int) (string, error) {
81-
//get just the non metadata info and the next page url
82-
payload, nextPageUrl, err := GetPayload(baseUrl, response)
52+
func getNextPageUrl(baseUrl string, response interface{}) (string, error) {
53+
payload, err := toMap(response)
8354
if err != nil {
8455
return "", err
8556
}
8657

87-
*curRecord += len(payload)
88-
89-
if limit != nil {
90-
//we have reached the desired limit
91-
if *limit <= *curRecord {
92-
return "", nil
93-
}
94-
95-
remaining := *limit - *curRecord
96-
if remaining > 0 {
97-
pageSize := min(len(payload), remaining)
98-
re := regexp.MustCompile(`PageSize=\d+`)
99-
nextPageUrl = re.ReplaceAllString(nextPageUrl, fmt.Sprintf("PageSize=%d", pageSize))
100-
}
101-
}
102-
103-
return nextPageUrl, err
104-
}
105-
106-
func getNextPageUrl(baseUrl string, payload map[string]interface{}) string {
10758
if payload != nil && payload["meta"] != nil && payload["meta"].(map[string]interface{})["next_page_url"] != nil {
108-
return payload["meta"].(map[string]interface{})["next_page_url"].(string)
59+
return payload["meta"].(map[string]interface{})["next_page_url"].(string), nil
10960
}
11061

11162
if payload != nil && payload["next_page_uri"] != nil {
11263
// remove any leading and trailing '/'
113-
return fmt.Sprintf("%s/%s", strings.Trim(baseUrl, "/"), strings.Trim(payload["next_page_uri"].(string), "/"))
64+
return fmt.Sprintf("%s/%s", strings.Trim(baseUrl, "/"), strings.Trim(payload["next_page_uri"].(string), "/")), nil
11465
}
11566

116-
return ""
67+
return "", nil
11768
}
11869

11970
func min(a int, b int) int {

client/page_util_test.go

Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,19 @@ func TestPageUtil_GetNextPageUri(t *testing.T) {
3535
"page_size": 50,
3636
}
3737
baseUrl := "https://api.twilio.com/"
38-
nextPageUrl := getNextPageUrl(baseUrl, payload)
38+
nextPageUrl, err := getNextPageUrl(baseUrl, payload)
39+
assert.Nil(t, err)
3940
assert.Equal(t, "https://api.twilio.com/2010-04-01/Accounts/ACXX/IncomingPhoneNumbers.json?PageSize=50&Page=1", nextPageUrl)
4041

4142
payload["next_page_uri"] = "2010-04-01/Accounts/ACXX/IncomingPhoneNumbers.json?PageSize=50&Page=1"
4243
baseUrl = "https://api.twilio.com"
43-
nextPageUrl = getNextPageUrl(baseUrl, payload)
44+
nextPageUrl, err = getNextPageUrl(baseUrl, payload)
45+
assert.Nil(t, err)
4446
assert.Equal(t, "https://api.twilio.com/2010-04-01/Accounts/ACXX/IncomingPhoneNumbers.json?PageSize=50&Page=1", nextPageUrl)
4547

4648
payload = map[string]interface{}{}
47-
nextPageUrl = getNextPageUrl(baseUrl, payload)
49+
nextPageUrl, err = getNextPageUrl(baseUrl, payload)
50+
assert.Nil(t, err)
4851
assert.Equal(t, "", nextPageUrl)
4952
}
5053

@@ -56,7 +59,8 @@ func TestPageUtil_GetNextPageUrl(t *testing.T) {
5659
},
5760
}
5861

59-
nextPageUrl := getNextPageUrl("https://apitest.twilio.com", payload)
62+
nextPageUrl, err := getNextPageUrl("https://apitest.twilio.com", payload)
63+
assert.Nil(t, err)
6064
assert.Equal(t, "https://api.twilio.com/2010-04-01/Accounts/ACXX/IncomingPhoneNumbers.json?PageSize=50&Page=1", nextPageUrl)
6165
}
6266

@@ -147,27 +151,18 @@ func TestPageUtil_GetNext(t *testing.T) {
147151
ps := &testResponse{}
148152
_ = json.NewDecoder(response.Body).Decode(ps)
149153

150-
curRecord := 0
151-
limit := 10
152-
153-
nextPageUrl, err := GetNext(baseUrl, ps, &curRecord, &limit, getSomething)
154+
nextPageUrl, err := GetNext(baseUrl, ps, getSomething)
154155
assert.Equal(t, "https://api.twilio.com/2010-04-01/Accounts/ACXX/Messages.json?From=9999999999&PageNumber=&To=4444444444&PageSize=2&Page=1&PageToken=PASMXX", nextPageUrl)
155156
assert.Nil(t, err)
156157

157-
curRecord = 15
158-
nextPageUrl, err = GetNext(baseUrl, ps, &curRecord, &limit, getSomething)
158+
nextPageUrl, err = GetNext(baseUrl, nil, getSomething)
159159
assert.Empty(t, nextPageUrl)
160160
assert.Nil(t, err)
161161
}
162162

163-
func TestPageUtil_GetNextWithErr(t *testing.T) {
164-
nextPageUrl, err := GetNext("baseUrl", nil, nil, nil, getSomething)
165-
assert.Nil(t, nextPageUrl)
166-
assert.NotNil(t, err)
167-
}
168-
169163
func TestPageUtil_ToMap(t *testing.T) {
170-
testMap := toMap("invalid")
164+
testMap, err := toMap("invalid")
165+
assert.NotNil(t, err)
171166
assert.Nil(t, testMap)
172167

173168
valid := testResponse{
@@ -181,42 +176,7 @@ func TestPageUtil_ToMap(t *testing.T) {
181176
Start: 0,
182177
Uri: "uri",
183178
}
184-
testMap = toMap(valid)
179+
testMap, err = toMap(valid)
180+
assert.Nil(t, err)
185181
assert.NotNil(t, testMap)
186182
}
187-
188-
func TestPageUtil_GetNextPageAddress(t *testing.T) {
189-
nextPageAddress, _ := getNextPageAddress("baseUrl", nil, nil, nil)
190-
assert.Empty(t, nextPageAddress)
191-
}
192-
193-
func TestPageUtil_GetPayload(t *testing.T) {
194-
response := map[string]interface{}{
195-
"end": 4,
196-
"first_page_uri": "/2010-04-01/Accounts/ACXX/Messages.json?From=9999999999&PageNumber=&To=4444444444&PageSize=2&Page=0",
197-
"messages": []map[string]interface{}{
198-
{
199-
"direction": "outbound-api",
200-
"from": "4444444444",
201-
"to": "9999999999",
202-
"body": "Message 0",
203-
"status": "delivered",
204-
},
205-
},
206-
"messages2": []map[string]interface{}{
207-
{
208-
"direction": "outbound-api",
209-
"from": "4444444444",
210-
"to": "9999999999",
211-
"body": "Message 0",
212-
"status": "delivered",
213-
},
214-
},
215-
}
216-
217-
payload, nextPageUrl, err := GetPayload("baseUrl", response)
218-
assert.Nil(t, payload)
219-
assert.Empty(t, nextPageUrl)
220-
assert.NotNil(t, err)
221-
assert.Equal(t, "payload contains more than 1 record of type array", err.Error())
222-
}

rest/accounts/v1/credentials_aws.go

Lines changed: 14 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rest/accounts/v1/credentials_public_keys.go

Lines changed: 14 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)