Skip to content

Commit 37fed6b

Browse files
author
Doug Black
committed
cherry pick 'Use List<NameValuePair> for create'
1 parent ad82d7d commit 37fed6b

38 files changed

+440
-216
lines changed

src/main/java/com/twilio/sdk/TwilioRestClient.java

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.ArrayList;
99
import java.util.HashMap;
1010
import java.util.List;
11+
import java.util.ArrayList;
1112
import java.util.Map;
1213

1314
import org.apache.http.Header;
@@ -395,9 +396,16 @@ private URI buildUri(String path, List<NameValuePair> queryStringParams) {
395396
* @return the twilio rest response
396397
*/
397398
public TwilioRestResponse request(String path, String method,
398-
Map<String, String> vars) throws TwilioRestException {
399+
Map<String, String> paramMap) throws TwilioRestException {
400+
401+
List<NameValuePair> paramList = generateParameters(paramMap);
402+
return request(path, method, paramList);
403+
}
404+
405+
public TwilioRestResponse request(String path, String method,
406+
List<NameValuePair> paramList) throws TwilioRestException {
399407

400-
HttpUriRequest request = setupRequest(path, method, vars);
408+
HttpUriRequest request = setupRequest(path, method, paramList);
401409

402410
HttpResponse response;
403411
try {
@@ -446,7 +454,25 @@ public TwilioRestResponse request(String path, String method,
446454
public InputStream requestStream(String path, String method,
447455
Map<String, String> vars) {
448456

449-
HttpUriRequest request = setupRequest(path, method, vars);
457+
List<NameValuePair> paramList = generateParameters(vars);
458+
return requestStream(path, method, paramList);
459+
}
460+
461+
/**
462+
* Request stream.
463+
*
464+
* @param path
465+
* the path
466+
* @param method
467+
* the method
468+
* @param paramList
469+
* the list of POST params
470+
* @return the input stream
471+
*/
472+
public InputStream requestStream(String path, String method,
473+
List<NameValuePair> paramList) {
474+
475+
HttpUriRequest request = setupRequest(path, method, paramList);
450476

451477
HttpResponse response;
452478
try {
@@ -474,8 +500,7 @@ public InputStream requestStream(String path, String method,
474500
* @return the http uri request
475501
*/
476502
private HttpUriRequest setupRequest(String path, String method,
477-
Map<String, String> vars) {
478-
List<NameValuePair> params = generateParameters(vars);
503+
List<NameValuePair> params) {
479504

480505
String normalizedPath = path.toLowerCase();
481506
StringBuilder sb = new StringBuilder();
@@ -529,9 +554,32 @@ private HttpUriRequest setupRequest(String path, String method,
529554
*/
530555
public TwilioRestResponse safeRequest(String path, String method,
531556
Map<String, String> vars) throws TwilioRestException {
557+
558+
List<NameValuePair> paramList = generateParameters(vars);
559+
return safeRequest(path, method, paramList);
560+
}
561+
562+
/**
563+
* Make a request, handles retries + back-off for server/network errors
564+
*
565+
* @param path
566+
* the URL (absolute w.r.t. the endpoint URL - i.e.
567+
* /2010-04-01/Accounts)
568+
* @param method
569+
* the HTTP method to use, defaults to GET
570+
* @param paramList
571+
* for POST or PUT, a list of data to send, for GET will be
572+
* appended to the URL as querystring params
573+
* @return The response
574+
* @throws TwilioRestException
575+
* if there's an client exception returned by the TwilioApi
576+
*/
577+
public TwilioRestResponse safeRequest(String path, String method,
578+
List<NameValuePair> paramList) throws TwilioRestException {
579+
532580
TwilioRestResponse response = null;
533581
for (int retry = 0; retry < this.numRetries; retry++) {
534-
response = request(path, method, vars);
582+
response = request(path, method, paramList);
535583
if (response.isClientError()) {
536584
throw TwilioRestException.parseResponse(response);
537585
} else if (response.isServerError()) {
@@ -565,7 +613,7 @@ public TwilioRestResponse get(String fullUri) throws TwilioRestException {
565613
TwilioRestResponse response = null;
566614

567615
for (int retry = 0; retry < this.numRetries; retry++) {
568-
response = request(fullUri, "GET", null);
616+
response = request(fullUri, "GET", (Map) null);
569617
if (response.isClientError()) {
570618
throw TwilioRestException.parseResponse(response);
571619
} else if (response.isServerError()) {

src/main/java/com/twilio/sdk/examples/RestExamples.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.HashMap;
44
import java.util.Iterator;
55
import java.util.List;
6+
import java.util.ArrayList;
67
import java.util.Map;
78

89
import com.twilio.sdk.TwilioRestClient;
@@ -18,6 +19,8 @@
1819
import com.twilio.sdk.resource.list.AccountList;
1920
import com.twilio.sdk.resource.list.AvailablePhoneNumberList;
2021
import com.twilio.sdk.resource.list.ParticipantList;
22+
import org.apache.http.NameValuePair;
23+
import org.apache.http.message.BasicNameValuePair;
2124

2225
// TODO: Auto-generated Javadoc
2326
/**
@@ -33,7 +36,7 @@ public class RestExamples {
3336

3437
/**
3538
* The main method.
36-
*
39+
*
3740
* @param args
3841
* the arguments
3942
* @throws TwilioRestException
@@ -72,11 +75,11 @@ public static void main(String[] args) throws TwilioRestException {
7275
Map<String, String> callParams = new HashMap<String, String>();
7376
callParams.put("To", "5105551212"); // Replace with a valid phone number
7477
callParams.put("From", "(510) 555-1212"); // Replace with a valid phone
75-
// number in your account
78+
// number in your account
7679
callParams.put("Url", "http://demo.twilio.com/welcome/voice/");
7780
Call call = callFactory.create(callParams);
7881
System.out.println(call.getSid());
79-
82+
8083
// Send an sms
8184
SmsFactory smsFactory = mainAccount.getSmsFactory();
8285
Map<String, String> smsParams = new HashMap<String, String>();
@@ -88,7 +91,7 @@ public static void main(String[] args) throws TwilioRestException {
8891

8992
// Search for available phone numbers & then buy a random phone number
9093
AvailablePhoneNumberList phoneNumbers = mainAccount
91-
.getAvailablePhoneNumbers();
94+
.getAvailablePhoneNumbers();
9295
List<AvailablePhoneNumber> list = phoneNumbers.getPageData();
9396

9497
// Buy the first number returned
@@ -112,7 +115,7 @@ public static void main(String[] args) throws TwilioRestException {
112115

113116
// Make a raw request to the api... note, this is deprecated style
114117
TwilioRestResponse resp = client.request("/2010-04-01/Accounts", "GET",
115-
null);
118+
(Map) null);
116119
if (!resp.isError()) {
117120
System.out.println(resp.getResponseText());
118121
}

src/main/java/com/twilio/sdk/resource/InstanceResource.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
import java.util.HashMap;
44
import java.util.Map;
5+
import java.util.List;
6+
import java.util.Date;
7+
8+
import java.text.ParseException;
9+
import java.text.SimpleDateFormat;
510

611
import com.twilio.sdk.TwilioRestClient;
712
import com.twilio.sdk.TwilioRestException;
813
import com.twilio.sdk.TwilioRestResponse;
14+
import org.apache.http.NameValuePair;
915

1016
// TODO: Auto-generated Javadoc
1117
/**
@@ -106,6 +112,16 @@ public void update(Map<String, String> params) throws TwilioRestException {
106112
this.getClient().safeRequest(this.getResourceLocation(), "POST", params);
107113
}
108114

115+
/**
116+
* Update.
117+
*
118+
* @param params the params list
119+
* @throws TwilioRestException the twilio rest exception
120+
*/
121+
public void update(List<NameValuePair> params) throws TwilioRestException {
122+
this.getClient().safeRequest(this.getResourceLocation(), "POST", params);
123+
}
124+
109125
/* (non-Javadoc)
110126
* @see com.twilio.sdk.resource.Resource#parseResponse(com.twilio.sdk.TwilioRestResponse)
111127
*/
@@ -114,4 +130,14 @@ protected void parseResponse(TwilioRestResponse response) {
114130
Map<String, Object> properties = response.toMap();
115131
this.properties = new HashMap<String, Object>(properties);
116132
}
133+
134+
/**
135+
* return a date from the property string
136+
*
137+
* @return the date value of the input string
138+
*/
139+
protected Date parseDate(String inDate) {
140+
if (inDate==null) {
141+
return null;
142+
}
117143
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
package com.twilio.sdk.resource.factory;
22

33
import java.util.Map;
4+
import java.util.List;
45

56
import com.twilio.sdk.TwilioRestException;
67
import com.twilio.sdk.resource.instance.Account;
8+
import org.apache.http.NameValuePair;
79

810
// TODO: Auto-generated Javadoc
911
/**
1012
* A factory for creating Account objects.
11-
*
13+
*
1214
* For more information see <a href="http://www.twilio.com/docs/api/rest/subaccounts">http://www.twilio.com/docs/api/rest/subaccounts</a>
1315
*/
1416
public interface AccountFactory {
15-
17+
1618
/**
1719
* Creates a subaccount.
1820
*
1921
* @param params the params
2022
* @return the account
21-
* @throws TwilioRestException
23+
* @throws TwilioRestException
2224
*/
2325
public Account create(Map<String, String> params) throws TwilioRestException;
26+
public Account create(List<NameValuePair> params) throws TwilioRestException;
2427
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
package com.twilio.sdk.resource.factory;
22

33
import java.util.Map;
4+
import java.util.List;
45

56
import com.twilio.sdk.TwilioRestException;
67
import com.twilio.sdk.resource.instance.Application;
8+
import org.apache.http.NameValuePair;
79

810
// TODO: Auto-generated Javadoc
911
/**
1012
* A factory for creating Application objects.
11-
*
13+
*
1214
* For more information see <a href=" http://www.twilio.com/docs/api/rest/applications#list-post"> http://www.twilio.com/docs/api/rest/applications#list-post</a>
1315
*
1416
*/
1517
public interface ApplicationFactory {
16-
18+
1719
/**
1820
* Creates the.
1921
*
2022
* @param params the params
2123
* @return the application
22-
* @throws TwilioRestException
24+
* @throws TwilioRestException
2325
*/
2426
public Application create(Map<String, String> params) throws TwilioRestException;
27+
public Application create(List<NameValuePair> params) throws TwilioRestException;
2528
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
package com.twilio.sdk.resource.factory;
22

33
import java.util.Map;
4+
import java.util.List;
45

56
import com.twilio.sdk.TwilioRestException;
67
import com.twilio.sdk.resource.instance.Call;
8+
import org.apache.http.NameValuePair;
79

810
// TODO: Auto-generated Javadoc
911
/**
1012
* A factory for creating Call objects.
1113
*/
1214
public interface CallFactory {
13-
15+
1416
/**
1517
* Creates the.
1618
*
1719
* @param params the params
1820
* @return the call
19-
* @throws TwilioRestException
21+
* @throws TwilioRestException
2022
*/
2123
public Call create(Map<String, String> params) throws TwilioRestException;
24+
public Call create(List<NameValuePair> params) throws TwilioRestException;
2225
}
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
package com.twilio.sdk.resource.factory;
22

33
import java.util.Map;
4+
import java.util.List;
45

56
import com.twilio.sdk.TwilioRestException;
67
import com.twilio.sdk.resource.instance.IncomingPhoneNumber;
8+
import org.apache.http.NameValuePair;
79

810
// TODO: Auto-generated Javadoc
911
/**
1012
* A factory for creating IncomingPhoneNumber objects.
1113
*/
1214
public interface IncomingPhoneNumberFactory {
13-
15+
1416
/**
15-
* Creates the.
17+
* Creates the IncomingPhoneNumber
1618
*
17-
* @param params the params
19+
* @param params the params map
1820
* @return the incoming phone number
19-
* @throws TwilioRestException
21+
* @throws TwilioRestException
2022
*/
2123
public IncomingPhoneNumber create(Map<String, String> params) throws TwilioRestException;
24+
25+
/**
26+
* Creates the IncomingPhoneNumber
27+
*
28+
* @param params the param list
29+
* @return the incoming phone number
30+
* @throws TwilioRestException
31+
*/
32+
public IncomingPhoneNumber create(List<NameValuePair> params) throws TwilioRestException;
2233
}
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
package com.twilio.sdk.resource.factory;
22

33
import java.util.Map;
4+
import java.util.List;
45

56
import com.twilio.sdk.TwilioRestException;
67
import com.twilio.sdk.resource.instance.CallerIdValidation;
8+
import org.apache.http.NameValuePair;
79

810
// TODO: Auto-generated Javadoc
911
/**
1012
* A factory for creating OutgoingCallerId objects.
1113
*/
1214
public interface OutgoingCallerIdFactory {
13-
15+
1416
/**
15-
* Creates the.
17+
* Creates the OutgoingCallerId
1618
*
17-
* @param params the params
19+
* @param params the params map
1820
* @return the caller id validation
19-
* @throws TwilioRestException
21+
* @throws TwilioRestException
2022
*/
2123
public CallerIdValidation create(Map<String, String> params) throws TwilioRestException;
24+
25+
/**
26+
* Creates the OutgoingCallerId
27+
*
28+
* @param params the params
29+
* @return the caller id validation
30+
* @throws TwilioRestException
31+
*/
32+
public CallerIdValidation create(List<NameValuePair> params) throws TwilioRestException;
2233
}

0 commit comments

Comments
 (0)