-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Peter/key get #24
Merged
Merged
Peter/key get #24
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c240fea
grabs kes from server
kingofirony 366f21f
adds unit tests in kotlin
kingofirony dbe806d
Adds @Test notations and removes mintKey from manifest
kingofirony 1fb9fe6
optimizes imports
kingofirony b871082
removes version from manifest and cleans key downloading logic
kingofirony File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
40 changes: 40 additions & 0 deletions
40
app/src/main/java/com/mapzen/erasermap/model/ManifestModel.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,40 @@ | ||
package com.mapzen.erasermap.model; | ||
|
||
public class ManifestModel { | ||
private double minVersion; | ||
private String vectorTileApiKeyReleaseProp; | ||
private String valhallaApiKey; | ||
private String peliasApiKey; | ||
|
||
public double getMinVersion() { | ||
return minVersion; | ||
} | ||
|
||
public void setMinVersion(double minVersion) { | ||
this.minVersion = minVersion; | ||
} | ||
|
||
public String getVectorTileApiKeyReleaseProp() { | ||
return vectorTileApiKeyReleaseProp; | ||
} | ||
|
||
public void setVectorTileApiKeyReleaseProp(String vectorTileApiKeyReleaseProp) { | ||
this.vectorTileApiKeyReleaseProp = vectorTileApiKeyReleaseProp; | ||
} | ||
|
||
public String getValhallaApiKey() { | ||
return valhallaApiKey; | ||
} | ||
|
||
public void setValhallaApiKey(String valhallaApiKey) { | ||
this.valhallaApiKey = valhallaApiKey; | ||
} | ||
|
||
public String getPeliasApiKey() { | ||
return peliasApiKey; | ||
} | ||
|
||
public void setPeliasApiKey(String peliasApiKey) { | ||
this.peliasApiKey = peliasApiKey; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
app/src/main/kotlin/com/mapzen/erasermap/model/ManifestDownLoader.kt
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,66 @@ | ||
package com.mapzen.erasermap.model | ||
|
||
import com.google.common.io.Files | ||
|
||
import android.content.Context | ||
import android.os.AsyncTask | ||
import android.util.Log | ||
import com.google.gson.Gson | ||
import com.google.gson.JsonParseException | ||
import com.mapzen.erasermap.model.ManifestModel | ||
import com.squareup.okhttp.* | ||
|
||
import java.io.BufferedInputStream | ||
import java.io.File | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.net.HttpURLConnection | ||
import java.net.MalformedURLException | ||
import java.net.URL | ||
import java.util.concurrent.TimeUnit | ||
import javax.inject.Inject | ||
|
||
public class ManifestDownLoader() { | ||
private var client: OkHttpClient? = null | ||
public var host: String? = "http://android.mapzen.com/" | ||
init { | ||
client = OkHttpClient() | ||
} | ||
|
||
public fun download(manifest: ManifestModel?, callback : () -> Unit) { | ||
(object : AsyncTask<Void, Void, ManifestModel>() { | ||
override fun doInBackground(vararg params: Void): ManifestModel? { | ||
try { | ||
var request: Request = Request.Builder() | ||
.url(URL(host + "erasermap_manifest")) | ||
.build() | ||
val response: Response = client!!.newCall(request).execute() | ||
val responseString: String = response.body().string() | ||
var gson: Gson = Gson() | ||
try { | ||
var model: ManifestModel = gson.fromJson(responseString, | ||
javaClass<ManifestModel>()) | ||
return model | ||
} | ||
catch (e: Exception) { | ||
return null | ||
} | ||
} catch (ioe: IOException) { | ||
Log.d("Error", "Unable to get api keys") | ||
} | ||
return null | ||
} | ||
|
||
override fun onPostExecute(model : ManifestModel?) { | ||
if(model != null) { | ||
manifest?.setValhallaApiKey(model.getValhallaApiKey()) | ||
manifest?.setMinVersion(model.getMinVersion()) | ||
manifest?.setVectorTileApiKeyReleaseProp(model.getVectorTileApiKeyReleaseProp()) | ||
manifest?.setPeliasApiKey(model.getPeliasApiKey()) | ||
} | ||
callback() | ||
} | ||
}).execute().get() | ||
} | ||
} | ||
|
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
63 changes: 63 additions & 0 deletions
63
app/src/test/java/com/mapzen/erasermap/model/ManifestDownLoaderTest.kt
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,63 @@ | ||
package com.mapzen.erasermap.model | ||
|
||
import com.mapzen.erasermap.BuildConfig | ||
import com.mapzen.erasermap.PrivateMapsTestRunner | ||
import com.squareup.okhttp.mockwebserver.MockResponse | ||
import com.squareup.okhttp.mockwebserver.MockWebServer | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.annotation.Config | ||
import java.util.concurrent.TimeUnit | ||
|
||
RunWith(PrivateMapsTestRunner::class) | ||
Config(constants = BuildConfig::class, sdk=intArrayOf(21)) | ||
public class ManifestDownLoaderTest { | ||
var downLoader: ManifestDownLoader? = null | ||
var server: MockWebServer? = null | ||
var sampleResponse: String = "{\"minVersion\": 0.1,\r\n" + | ||
" \"vectorTileApiKeyReleaseProp\": \"vectorKey\",\r\n " + | ||
" \"valhallaApiKey\": \"routeKey\",\r\n " + | ||
"\"peliasApiKey\": \"peliasKey\"}\r\n" | ||
|
||
@Before | ||
throws(Exception::class) | ||
public fun setup() { | ||
downLoader = ManifestDownLoader() | ||
server = MockWebServer() | ||
server?.play() | ||
downLoader?.host = server?.getUrl("/").toString() | ||
|
||
} | ||
|
||
@After | ||
throws(Exception::class) | ||
public fun teardown() { | ||
server?.shutdown() | ||
} | ||
|
||
@Test | ||
throws(Exception::class) | ||
public fun shouldRequestManifest() { | ||
server?.enqueue(MockResponse().setBody(sampleResponse)) | ||
downLoader?.download(ManifestModel(), {}) | ||
var request = server?.takeRequest(1000, TimeUnit.MILLISECONDS); | ||
assertThat(request?.getPath().toString()).isEqualTo("/erasermap_manifest") | ||
} | ||
|
||
@Test | ||
throws(Exception::class) | ||
public fun shouldSetManifestModelObject() { | ||
var keys: ManifestModel = ManifestModel() | ||
server?.enqueue(MockResponse().setBody(sampleResponse)) | ||
downLoader?.download(keys, {}) | ||
server?.takeRequest(1000, TimeUnit.MILLISECONDS); | ||
assertThat(keys.getValhallaApiKey()).isEqualTo("routeKey") | ||
assertThat(keys.getVectorTileApiKeyReleaseProp()).isEqualTo("vectorKey") | ||
assertThat(keys.getPeliasApiKey()).isEqualTo("peliasKey") | ||
assertThat(keys.getMinVersion()).isEqualTo(0.1) | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to inject an empty
ManifestModel
here when a new instance is created byManifestDownloader
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done