-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Send User-Agent for MapzenMap requests & create generic http handler * Send User-Agent for MapzenSearch requests * Send User-Agent for all MapzenRouter requests * Rm trailing ;
- Loading branch information
1 parent
670556c
commit 0a40f84
Showing
26 changed files
with
877 additions
and
215 deletions.
There are no files selected for viewing
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
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
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
28 changes: 28 additions & 0 deletions
28
core/src/main/java/com/mapzen/android/core/GenericHttpHandler.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,28 @@ | ||
package com.mapzen.android.core; | ||
|
||
import com.mapzen.BuildConfig; | ||
|
||
import android.os.Build; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Generic SDK interface for service-specific handlers to implement. | ||
*/ | ||
public interface GenericHttpHandler { | ||
|
||
String HEADER_USER_AGENT = "User-Agent"; | ||
String USER_AGENT = "android-sdk;" + BuildConfig.SDK_VERSION + ";" + Build.VERSION.RELEASE; | ||
|
||
/** | ||
* Return query parameters to be appended to every request. | ||
* @return | ||
*/ | ||
Map<String, String> queryParamsForRequest(); | ||
|
||
/** | ||
* Return headers to be added to every request. | ||
* @return | ||
*/ | ||
Map<String, String> headersForRequest(); | ||
} |
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
98 changes: 98 additions & 0 deletions
98
core/src/main/java/com/mapzen/android/graphics/MapzenMapHttpHandler.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,98 @@ | ||
package com.mapzen.android.graphics; | ||
|
||
import com.mapzen.android.core.GenericHttpHandler; | ||
import com.mapzen.tangram.HttpHandler; | ||
|
||
import java.io.File; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import okhttp3.Callback; | ||
import okhttp3.Headers; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
|
||
/** | ||
* Base class for HTTP requests made by {@link MapzenMap}. | ||
*/ | ||
public abstract class MapzenMapHttpHandler implements GenericHttpHandler { | ||
|
||
private HttpHandler httpHandler; | ||
RequestEnqueuer requestEnqueuer; | ||
|
||
/** | ||
* Public constructor with no cache configured. | ||
*/ | ||
public MapzenMapHttpHandler() { | ||
this(null, 0); | ||
} | ||
|
||
/** | ||
* Public constructor with cache configured. | ||
* @param directory cache directory | ||
* @param maxSize max cache directory size in bytes | ||
*/ | ||
public MapzenMapHttpHandler(File directory, long maxSize) { | ||
httpHandler = new InternalHttpHandler(directory, maxSize); | ||
requestEnqueuer = new RequestEnqueuer(); | ||
} | ||
|
||
/** | ||
* Underlying Tangram handler. | ||
* @return | ||
*/ | ||
HttpHandler httpHandler() { | ||
return httpHandler; | ||
} | ||
|
||
private class InternalHttpHandler extends TmpHttpHandler { | ||
|
||
public InternalHttpHandler(File directory, long maxSize) { | ||
super(directory, maxSize); | ||
} | ||
|
||
@Override public boolean onRequest(String url, Callback cb) { | ||
Map<String, String> customParams = queryParamsForRequest(); | ||
if (customParams != null) { | ||
for (String key : customParams.keySet()) { | ||
url = url.concat(key + "=" + customParams.get(key) + "&"); | ||
} | ||
} | ||
|
||
Map<String, String> headers = new HashMap<>(); | ||
headers.put(HEADER_USER_AGENT, USER_AGENT); | ||
Map<String, String> customHeaders = headersForRequest(); | ||
if (customHeaders != null) { | ||
headers.putAll(customHeaders); | ||
} | ||
|
||
requestEnqueuer.enqueueRequest(okClient, cb, url, headers); | ||
return true; | ||
} | ||
|
||
@Override public void onCancel(String url) { | ||
super.onCancel(url); | ||
} | ||
} | ||
|
||
/** | ||
* Class to handle creating and enqueuing requests. | ||
*/ | ||
class RequestEnqueuer { | ||
/** | ||
* Creates and enqueues a {@link Request}. | ||
* @param client | ||
* @param callback | ||
* @param url | ||
* @param headers | ||
*/ | ||
void enqueueRequest(OkHttpClient client, Callback callback, String url, | ||
Map<String, String> headers) { | ||
Request request = new Request.Builder() | ||
.url(url) | ||
.headers(Headers.of(headers)) | ||
.build(); | ||
client.newCall(request).enqueue(callback); | ||
} | ||
} | ||
} |
51 changes: 0 additions & 51 deletions
51
core/src/main/java/com/mapzen/android/graphics/TileHttpHandler.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.