3131import java .net .URL ;
3232import java .net .URLEncoder ;
3333import javax .net .ssl .HttpsURLConnection ;
34+
35+ import com .facebook .ads .utils .HttpMethods ;
3436import org .omg .CORBA .Request ;
3537import java .io .BufferedReader ;
3638import java .lang .reflect .Modifier ;
@@ -87,14 +89,26 @@ public static IAsyncRequestExecutor getAsyncExecutor() {
8789 return asyncExecutor ;
8890 }
8991
92+ public APIRequest (APIContext context , String nodeId , String endpoint , HttpMethods method ) {
93+ this (context , nodeId , endpoint , method .toString (), null , null );
94+ }
95+
9096 public APIRequest (APIContext context , String nodeId , String endpoint , String method ) {
9197 this (context , nodeId , endpoint , method , null , null );
9298 }
9399
100+ public APIRequest (APIContext context , String nodeId , String endpoint , HttpMethods method , ResponseParser <T > parser ) {
101+ this (context , nodeId , endpoint , method .toString (), null , parser );
102+ }
103+
94104 public APIRequest (APIContext context , String nodeId , String endpoint , String method , ResponseParser <T > parser ) {
95105 this (context , nodeId , endpoint , method , null , parser );
96106 }
97107
108+ public APIRequest (APIContext context , String nodeId , String endpoint , HttpMethods method , List <String > paramNames ) {
109+ this (context , nodeId , endpoint , method .toString (), paramNames , null );
110+ }
111+
98112 public APIRequest (APIContext context , String nodeId , String endpoint , String method , List <String > paramNames ) {
99113 this (context , nodeId , endpoint , method , paramNames , null );
100114 }
@@ -372,7 +386,7 @@ BatchRequest.BatchModeRequestInfo getBatchModeRequestInfo() throws IOException {
372386 if (returnFields != null ) allParams .put ("fields" , joinStringList (returnFields ));
373387 info .method = this .method ;
374388 StringBuilder relativeUrl = new StringBuilder (context .getVersion () + "/" + nodeId + endpoint );
375- if (this .method .equals (" POST" )) {
389+ if (this .method .equals (HttpMethods . POST )) {
376390 info .files = new HashMap <String , File >();
377391 info .relativeUrl = relativeUrl .toString ();
378392 StringBuilder body = new StringBuilder ();
@@ -514,19 +528,23 @@ public void onResponse(okhttp3.Call call, final okhttp3.Response response) throw
514528 public static class DefaultRequestExecutor implements IRequestExecutor {
515529
516530 public ResponseWrapper execute (String method , String apiUrl , Map <String , Object > allParams , APIContext context ) throws APIException , IOException {
517- if (" GET" .equals (method )) return sendGet (apiUrl , allParams , context );
518- else if (" POST" .equals (method )) return sendPost (apiUrl , allParams , context );
519- else if (" DELETE" .equals (method )) return sendDelete (apiUrl , allParams , context );
531+ if (HttpMethods . GET .equals (method )) return sendGet (apiUrl , allParams , context );
532+ else if (HttpMethods . POST .equals (method )) return sendPost (apiUrl , allParams , context );
533+ else if (HttpMethods . DELETE .equals (method )) return sendDelete (apiUrl , allParams , context );
520534 else throw new IllegalArgumentException ("Unsupported http method. Currently only GET, POST, and DELETE are supported" );
521535 }
522536
537+ public ResponseWrapper execute (HttpMethods method , String apiUrl , Map <String , Object > allParams , APIContext context ) throws APIException , IOException {
538+ return execute (method .toString (), apiUrl , allParams , context );
539+ }
540+
523541 public ResponseWrapper sendGet (String apiUrl , Map <String , Object > allParams , APIContext context ) throws APIException , IOException {
524542 URL url = new URL (RequestHelper .constructUrlString (apiUrl , allParams ));
525543 context .log ("Request:" );
526544 context .log ("GET: " + url .toString ());
527545 HttpsURLConnection con = (HttpsURLConnection ) url .openConnection ();
528546
529- con .setRequestMethod (" GET" );
547+ con .setRequestMethod (HttpMethods . GET . toString () );
530548 con .setRequestProperty ("User-Agent" , USER_AGENT );
531549 con .setRequestProperty ("Content-Type" ,"application/x-www-form-urlencoded" );
532550
@@ -539,7 +557,7 @@ public ResponseWrapper sendPost(String apiUrl, Map<String, Object> allParams, AP
539557 context .log ("Post: " + url .toString ());
540558 HttpsURLConnection con = (HttpsURLConnection ) url .openConnection ();
541559
542- con .setRequestMethod (" POST" );
560+ con .setRequestMethod (HttpMethods . POST . toString () );
543561 con .setRequestProperty ("User-Agent" , USER_AGENT );
544562 con .setRequestProperty ("Content-Type" ,"multipart/form-data; boundary=" + boundary );
545563 con .setDoOutput (true );
@@ -591,7 +609,7 @@ public ResponseWrapper sendDelete(String apiUrl, Map<String, Object> allParams,
591609 context .log ("Delete: " + url .toString ());
592610 HttpsURLConnection con = (HttpsURLConnection ) url .openConnection ();
593611
594- con .setRequestMethod (" DELETE" );
612+ con .setRequestMethod (HttpMethods . DELETE . toString () );
595613 con .setRequestProperty ("User-Agent" , USER_AGENT );
596614
597615 return readResponse (con );
@@ -612,9 +630,9 @@ static void init() {
612630 }
613631
614632 public ListenableFuture <ResponseWrapper > execute (String method , String apiUrl , Map <String , Object > allParams , APIContext context ) throws APIException , IOException {
615- if (" GET" .equals (method )) return sendGet (apiUrl , allParams , context );
616- else if (" POST" .equals (method )) return sendPost (apiUrl , allParams , context );
617- else if (" DELETE" .equals (method )) return sendDelete (apiUrl , allParams , context );
633+ if (HttpMethods . GET .equals (method )) return sendGet (apiUrl , allParams , context );
634+ else if (HttpMethods . POST .equals (method )) return sendPost (apiUrl , allParams , context );
635+ else if (HttpMethods . DELETE .equals (method )) return sendDelete (apiUrl , allParams , context );
618636 else throw new IllegalArgumentException ("Unsupported http method. Currently only GET, POST, and DELETE are supported" );
619637 }
620638
0 commit comments