Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ This situation can occasionally happen if new fields are added to server respons
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:

```java
APIRequest<AdAccount> request = new APIRequest<AdAccount>(context, "me", "/adaccounts", "GET", AdAccount.getParser());
APIRequest<AdAccount> request = new APIRequest<AdAccount>(context, "me", "/adaccounts", HttpMethods.GET, AdAccount.getParser());
APINodeList<AdAccount> accounts = (APINodeList<AdAccount>)(request.execute());
```

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/facebook/ads/sdk/APIContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import javax.crypto.spec.SecretKeySpec;
import com.google.gson.JsonParser;
import com.google.gson.JsonElement;
import com.facebook.ads.utils.HttpMethods;

public class APIContext {
public static final String DEFAULT_API_BASE = APIConfig.DEFAULT_API_BASE;
Expand Down Expand Up @@ -159,7 +160,7 @@ public String getAppID() {
params.put("access_token", this.accessToken);
params.put("fields", "app_id");

APIRequest.ResponseWrapper response = executor.execute("GET", apiUrl, params, this);
APIRequest.ResponseWrapper response = executor.execute(HttpMethods.GET, apiUrl, params, this);
JsonParser parser = new JsonParser();
this.appID = parser.parse(response.getBody())
.getAsJsonObject()
Expand Down
47 changes: 37 additions & 10 deletions src/main/java/com/facebook/ads/sdk/APIRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import com.facebook.ads.utils.HttpMethods;

public class APIRequest<T extends APINode> {

public static final String USER_AGENT = APIConfig.USER_AGENT;
Expand Down Expand Up @@ -87,14 +89,26 @@ public static IAsyncRequestExecutor getAsyncExecutor() {
return asyncExecutor;
}

public APIRequest(APIContext context, String nodeId, String endpoint, HttpMethods method) {
this(context, nodeId, endpoint, method.toString(), null, null);
}

public APIRequest(APIContext context, String nodeId, String endpoint, String method) {
this(context, nodeId, endpoint, method, null, null);
}

public APIRequest(APIContext context, String nodeId, String endpoint, HttpMethods method, ResponseParser<T> parser) {
this(context, nodeId, endpoint, method.toString(), null, parser);
}

public APIRequest(APIContext context, String nodeId, String endpoint, String method, ResponseParser<T> parser) {
this(context, nodeId, endpoint, method, null, parser);
}

public APIRequest(APIContext context, String nodeId, String endpoint, HttpMethods method, List<String> paramNames) {
this(context, nodeId, endpoint, method.toString(), paramNames, null);
}

public APIRequest(APIContext context, String nodeId, String endpoint, String method, List<String> paramNames) {
this(context, nodeId, endpoint, method, paramNames, null);
}
Expand Down Expand Up @@ -372,7 +386,7 @@ BatchRequest.BatchModeRequestInfo getBatchModeRequestInfo() throws IOException {
if (returnFields != null) allParams.put("fields", joinStringList(returnFields));
info.method = this.method;
StringBuilder relativeUrl = new StringBuilder(context.getVersion() + "/" + nodeId + endpoint);
if (this.method.equals("POST")) {
if (this.method.equals(HttpMethods.POST)) {
info.files = new HashMap<String, File>();
info.relativeUrl = relativeUrl.toString();
StringBuilder body = new StringBuilder();
Expand Down Expand Up @@ -514,19 +528,32 @@ public void onResponse(okhttp3.Call call, final okhttp3.Response response) throw
public static class DefaultRequestExecutor implements IRequestExecutor {

public ResponseWrapper execute(String method, String apiUrl, Map<String, Object> allParams, APIContext context) throws APIException, IOException {
if ("GET".equals(method)) return sendGet(apiUrl, allParams, context);
else if ("POST".equals(method)) return sendPost(apiUrl, allParams, context);
else if ("DELETE".equals(method)) return sendDelete(apiUrl, allParams, context);
if (HttpMethods.GET.equals(method)) return sendGet(apiUrl, allParams, context);
else if (HttpMethods.POST.equals(method)) return sendPost(apiUrl, allParams, context);
else if (HttpMethods.DELETE.equals(method)) return sendDelete(apiUrl, allParams, context);
else throw new IllegalArgumentException("Unsupported http method. Currently only GET, POST, and DELETE are supported");
}

public ResponseWrapper execute(HttpMethods method, String apiUrl, Map<String, Object> allParams, APIContext context) throws APIException, IOException {
switch (method) {
case GET:
return sendGet(apiUrl, allParams, context);
case POST:
return sendPost(apiUrl, allParams, context);
case DELETE:
return sendDelete(apiUrl, allParams, context);
default:
throw new IllegalArgumentException("Unsupported http method. Currently only GET, POST, and DELETE are supported");
}
}

public ResponseWrapper sendGet(String apiUrl, Map<String, Object> allParams, APIContext context) throws APIException, IOException {
URL url = new URL(RequestHelper.constructUrlString(apiUrl, allParams));
context.log("Request:");
context.log("GET: " + url.toString());
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

con.setRequestMethod("GET");
con.setRequestMethod(HttpMethods.GET.toString());
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

Expand All @@ -539,7 +566,7 @@ public ResponseWrapper sendPost(String apiUrl, Map<String, Object> allParams, AP
context.log("Post: " + url.toString());
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

con.setRequestMethod("POST");
con.setRequestMethod(HttpMethods.POST.toString());
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Content-Type","multipart/form-data; boundary=" + boundary);
con.setDoOutput(true);
Expand Down Expand Up @@ -591,7 +618,7 @@ public ResponseWrapper sendDelete(String apiUrl, Map<String, Object> allParams,
context.log("Delete: " + url.toString());
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

con.setRequestMethod("DELETE");
con.setRequestMethod(HttpMethods.DELETE.toString());
con.setRequestProperty("User-Agent", USER_AGENT);

return readResponse(con);
Expand All @@ -612,9 +639,9 @@ static void init() {
}

public ListenableFuture<ResponseWrapper> execute(String method, String apiUrl, Map<String, Object> allParams, APIContext context) throws APIException, IOException {
if ("GET".equals(method)) return sendGet(apiUrl, allParams, context);
else if ("POST".equals(method)) return sendPost(apiUrl, allParams, context);
else if ("DELETE".equals(method)) return sendDelete(apiUrl, allParams, context);
if (HttpMethods.GET.equals(method)) return sendGet(apiUrl, allParams, context);
else if (HttpMethods.POST.equals(method)) return sendPost(apiUrl, allParams, context);
else if (HttpMethods.DELETE.equals(method)) return sendDelete(apiUrl, allParams, context);
else throw new IllegalArgumentException("Unsupported http method. Currently only GET, POST, and DELETE are supported");
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/facebook/ads/sdk/URL.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.google.gson.JsonParser;

import com.facebook.ads.sdk.APIException.MalformedResponseException;
import com.facebook.ads.utils.HttpMethods;

/**
* This class is auto-generated.
Expand Down Expand Up @@ -114,7 +115,7 @@ public static ListenableFuture<URL> fetchByIdAsync(String id, APIContext context

public static APINodeList<URL> fetchByIds(List<String> ids, List<String> fields, APIContext context) throws APIException {
return (APINodeList<URL>)(
new APIRequest<URL>(context, "", "/", "GET", URL.getParser())
new APIRequest<URL>(context, "", "/", HttpMethods.GET, URL.getParser())
.setParam("ids", APIRequest.joinStringList(ids))
.requestFields(fields)
.execute()
Expand All @@ -123,7 +124,7 @@ public static APINodeList<URL> fetchByIds(List<String> ids, List<String> fields,

public static ListenableFuture<APINodeList<URL>> fetchByIdsAsync(List<String> ids, List<String> fields, APIContext context) throws APIException {
return
new APIRequest(context, "", "/", "GET", URL.getParser())
new APIRequest(context, "", "/", HttpMethods.GET, URL.getParser())
.setParam("ids", APIRequest.joinStringList(ids))
.requestFields(fields)
.executeAsyncBase();
Expand Down Expand Up @@ -380,7 +381,7 @@ public URL apply(ResponseWrapper result) {
};

public APIRequestGet(String nodeId, APIContext context) {
super(context, nodeId, "/", "GET", Arrays.asList(PARAMS));
super(context, nodeId, "/", HttpMethods.GET, Arrays.asList(PARAMS));
}

@Override
Expand Down Expand Up @@ -538,7 +539,7 @@ public URL apply(ResponseWrapper result) {
};

public APIRequestUpdate(String nodeId, APIContext context) {
super(context, nodeId, "/", "POST", Arrays.asList(PARAMS));
super(context, nodeId, "/", HttpMethods.POST, Arrays.asList(PARAMS));
}

@Override
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/facebook/ads/utils/HttpMethods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.facebook.ads.utils;

/**
* HTTP Request Methods
*/
public enum HttpMethods {
GET,
HEAD,
POST,
PUT,
PATCH,
DELETE,
OPTIONS,
TRACE
}