Skip to content

Commit 8be19cb

Browse files
committed
Migrate http methods to predefined enum values
1 parent 7d3f1f8 commit 8be19cb

File tree

5 files changed

+52
-16
lines changed

5 files changed

+52
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ This situation can occasionally happen if new fields are added to server respons
305305
Most of Marketing API can be found in SDK classes. If you don't find the one you want to access, it is possible to construct an Ad-hoc APIRequest:
306306

307307
```java
308-
APIRequest<AdAccount> request = new APIRequest<AdAccount>(context, "me", "/adaccounts", "GET", AdAccount.getParser());
308+
APIRequest<AdAccount> request = new APIRequest<AdAccount>(context, "me", "/adaccounts", HttpMethods.GET, AdAccount.getParser());
309309
APINodeList<AdAccount> accounts = (APINodeList<AdAccount>)(request.execute());
310310
```
311311

src/main/java/com/facebook/ads/sdk/APIContext.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.util.Map;
3131
import javax.crypto.Mac;
3232
import javax.crypto.spec.SecretKeySpec;
33+
34+
import com.facebook.ads.utils.HttpMethods;
3335
import com.google.gson.JsonParser;
3436
import com.google.gson.JsonElement;
3537

@@ -159,7 +161,7 @@ public String getAppID() {
159161
params.put("access_token", this.accessToken);
160162
params.put("fields", "app_id");
161163

162-
APIRequest.ResponseWrapper response = executor.execute("GET", apiUrl, params, this);
164+
APIRequest.ResponseWrapper response = executor.execute(HttpMethods.GET, apiUrl, params, this);
163165
JsonParser parser = new JsonParser();
164166
this.appID = parser.parse(response.getBody())
165167
.getAsJsonObject()

src/main/java/com/facebook/ads/sdk/APIRequest.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.net.URL;
3232
import java.net.URLEncoder;
3333
import javax.net.ssl.HttpsURLConnection;
34+
35+
import com.facebook.ads.utils.HttpMethods;
3436
import org.omg.CORBA.Request;
3537
import java.io.BufferedReader;
3638
import 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

src/main/java/com/facebook/ads/sdk/URL.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.List;
3232
import java.util.Map;
3333

34+
import com.facebook.ads.utils.HttpMethods;
3435
import com.google.common.base.Function;
3536
import com.google.common.util.concurrent.Futures;
3637
import com.google.common.util.concurrent.ListenableFuture;
@@ -114,7 +115,7 @@ public static ListenableFuture<URL> fetchByIdAsync(String id, APIContext context
114115

115116
public static APINodeList<URL> fetchByIds(List<String> ids, List<String> fields, APIContext context) throws APIException {
116117
return (APINodeList<URL>)(
117-
new APIRequest<URL>(context, "", "/", "GET", URL.getParser())
118+
new APIRequest<URL>(context, "", "/", HttpMethods.GET, URL.getParser())
118119
.setParam("ids", APIRequest.joinStringList(ids))
119120
.requestFields(fields)
120121
.execute()
@@ -123,7 +124,7 @@ public static APINodeList<URL> fetchByIds(List<String> ids, List<String> fields,
123124

124125
public static ListenableFuture<APINodeList<URL>> fetchByIdsAsync(List<String> ids, List<String> fields, APIContext context) throws APIException {
125126
return
126-
new APIRequest(context, "", "/", "GET", URL.getParser())
127+
new APIRequest(context, "", "/", HttpMethods.GET, URL.getParser())
127128
.setParam("ids", APIRequest.joinStringList(ids))
128129
.requestFields(fields)
129130
.executeAsyncBase();
@@ -380,7 +381,7 @@ public URL apply(ResponseWrapper result) {
380381
};
381382

382383
public APIRequestGet(String nodeId, APIContext context) {
383-
super(context, nodeId, "/", "GET", Arrays.asList(PARAMS));
384+
super(context, nodeId, "/", HttpMethods.GET, Arrays.asList(PARAMS));
384385
}
385386

386387
@Override
@@ -538,7 +539,7 @@ public URL apply(ResponseWrapper result) {
538539
};
539540

540541
public APIRequestUpdate(String nodeId, APIContext context) {
541-
super(context, nodeId, "/", "POST", Arrays.asList(PARAMS));
542+
super(context, nodeId, "/", HttpMethods.POST, Arrays.asList(PARAMS));
542543
}
543544

544545
@Override
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.facebook.ads.utils;
2+
3+
/**
4+
* HTTP Request Methods collections
5+
*/
6+
public enum HttpMethods {
7+
GET,
8+
HEAD,
9+
POST,
10+
PUT,
11+
PATCH,
12+
DELETE,
13+
OPTIONS,
14+
TRACE
15+
}

0 commit comments

Comments
 (0)