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
24 changes: 24 additions & 0 deletions src/main/java/com/facebook/ads/sdk/APIContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public class APIContext {
private String version;
protected boolean isDebug = false;
protected PrintStream logger = System.out;
/**
* @see java.net.URLConnection#setConnectTimeout(int)
*/
private int connectTimeout;
/**
* @see java.net.URLConnection#setReadTimeout(int)
*/
private int readTimeout;

public APIContext(String endpointBase, String videoEndpointBase, String version, String accessToken, String appSecret, String appID, boolean logCrash) {
this.version = version;
Expand Down Expand Up @@ -180,4 +188,20 @@ public String getAppID() {
public static void disableCrashReport() {
CrashReporter.disable();
}

public int getConnectTimeout() {
return connectTimeout;
}

public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}

public int getReadTimeout() {
return readTimeout;
}

public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
}
13 changes: 12 additions & 1 deletion src/main/java/com/facebook/ads/sdk/APIRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
package com.facebook.ads.sdk;

import java.nio.file.Files;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -535,19 +534,30 @@ public ResponseWrapper sendGet(String apiUrl, Map<String, Object> allParams, API
context.log("GET: " + url.toString());
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

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

return readResponse(con);
}

/**
* set timeout for connection
* @param con connection
* @param context context
*/
public void setTimeout(HttpsURLConnection con, APIContext context){
con.setConnectTimeout(Math.max(context.getConnectTimeout(), 0));
con.setReadTimeout(Math.max(context.getReadTimeout(), 0));
}
public ResponseWrapper sendPost(String apiUrl, Map<String, Object> allParams, APIContext context) throws APIException, IOException {
String boundary = "--------------------------" + new Random().nextLong();
URL url = new URL(apiUrl);
context.log("Post: " + url.toString());
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

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

setTimeout(con, context);
con.setRequestMethod("DELETE");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Content-Type","multipart/form-data; boundary=" + boundary);
Expand Down