-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved setupUrlConnection() to the utils library
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
WordPressUtils/src/main/java/org/wordpress/android/util/HTTPUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.wordpress.android.util; | ||
|
||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.Map; | ||
|
||
public class HTTPUtils { | ||
public static final int REQUEST_TIMEOUT_MS = 30000; | ||
|
||
/** | ||
* Builds an HttpURLConnection from a URL and header map. Will force HTTPS usage if given an Authorization header. | ||
* @throws IOException | ||
*/ | ||
public static HttpURLConnection setupUrlConnection(String url, Map<String, String> headers) throws IOException { | ||
// Force HTTPS usage if an authorization header was specified | ||
if (headers.keySet().contains("Authorization")) { | ||
url = UrlUtils.makeHttps(url); | ||
} | ||
|
||
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); | ||
conn.setReadTimeout(REQUEST_TIMEOUT_MS); | ||
conn.setConnectTimeout(REQUEST_TIMEOUT_MS); | ||
|
||
for (Map.Entry<String, String> entry : headers.entrySet()) { | ||
conn.setRequestProperty(entry.getKey(), entry.getValue()); | ||
} | ||
|
||
return conn; | ||
} | ||
} |