Skip to content
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 5 commits into from
Jul 27, 2015
Merged
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
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ dependencies {
exclude group: 'org.apache.commons', module: 'commons-io'
}
compile "com.splunk.mint:mint:4.2.1"
compile 'com.squareup.okhttp:okhttp:2.4.0'

testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.9.5'
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/com/mapzen/erasermap/CommonModule.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapzen.erasermap;

import com.mapzen.erasermap.model.ManifestModel;
import com.mapzen.erasermap.presenter.MainPresenter;
import com.mapzen.erasermap.presenter.MainPresenterImpl;
import com.mapzen.helpers.RouteEngine;
Expand All @@ -26,7 +27,7 @@ public class CommonModule {
return new Bus();
}

@Provides RouteEngine provideRouteEngine() {
return new RouteEngine();
}
@Provides @Singleton ManifestModel provideManifestModel() { return new ManifestModel(); }

@Provides RouteEngine provideRouteEngine() { return new RouteEngine(); }
}
40 changes: 40 additions & 0 deletions app/src/main/java/com/mapzen/erasermap/model/ManifestModel.java
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;
}
}
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()
}
}

30 changes: 26 additions & 4 deletions app/src/main/kotlin/com/mapzen/erasermap/view/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import com.mapzen.erasermap.BuildConfig
import com.mapzen.erasermap.CrashReportService
import com.mapzen.erasermap.EraserMapApplication
import com.mapzen.erasermap.R
import com.mapzen.erasermap.model.ManifestDownLoader
import com.mapzen.erasermap.model.ManifestModel
import com.mapzen.erasermap.presenter.MainPresenter
import com.mapzen.pelias.Pelias
import com.mapzen.pelias.PeliasLocationProvider
Expand Down Expand Up @@ -65,6 +67,8 @@ public class MainActivity : AppCompatActivity(), MainViewController, Router.Call
@Inject set
var crashReportService: CrashReportService? = null
@Inject set
var apiKeys: ManifestModel? = null
@Inject set
Copy link
Collaborator

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 by ManifestDownloader?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


var app: EraserMapApplication? = null
var mapController : MapController? = null
Expand All @@ -79,7 +83,7 @@ public class MainActivity : AppCompatActivity(), MainViewController, Router.Call
super<AppCompatActivity>.onCreate(savedInstanceState)
app = getApplication() as EraserMapApplication
app?.component()?.inject(this)
crashReportService?.initAndStartSession(this)
initCrashReportService()
setContentView(R.layout.activity_main)
presenter?.mainViewController = this
presenter?.bus = bus
Expand All @@ -90,6 +94,7 @@ public class MainActivity : AppCompatActivity(), MainViewController, Router.Call
initReverseButton()
centerMapOnCurrentLocation()
presenter?.onRestoreViewState()
getApiKeys()
}

override fun onStart() {
Expand Down Expand Up @@ -139,6 +144,23 @@ public class MainActivity : AppCompatActivity(), MainViewController, Router.Call
findViewById(R.id.find_me).setOnClickListener({ centerMapOnCurrentLocation() })
}


private fun initCrashReportService() {
crashReportService?.initAndStartSession(this)
}

private fun getApiKeys() {
var dl: ManifestDownLoader = ManifestDownLoader()
dl.download(apiKeys, {
if(apiKeys?.getValhallaApiKey()== null) {
apiKeys?.setValhallaApiKey(BuildConfig.VALHALLA_API_KEY)
}
if(apiKeys?.getVectorTileApiKeyReleaseProp()== null) {
apiKeys?.setVectorTileApiKeyReleaseProp(BuildConfig.VECTOR_TILE_API_KEY)
}
})
}

private fun initLocationUpdates() {
if (locationClient?.isConnected() == false) {
locationClient?.connect()
Expand Down Expand Up @@ -594,9 +616,9 @@ public class MainActivity : AppCompatActivity(), MainViewController, Router.Call

private fun getInitializedRouter(): Router {
when(type) {
Router.Type.DRIVING -> return Router().setApiKey(BuildConfig.VALHALLA_API_KEY).setDriving()
Router.Type.WALKING -> return Router().setApiKey(BuildConfig.VALHALLA_API_KEY).setWalking()
Router.Type.BIKING -> return Router().setApiKey(BuildConfig.VALHALLA_API_KEY).setBiking()
Router.Type.DRIVING -> return Router().setApiKey(apiKeys?.getValhallaApiKey() as String).setDriving()
Router.Type.WALKING -> return Router().setApiKey(apiKeys?.getValhallaApiKey() as String).setWalking()
Router.Type.BIKING -> return Router().setApiKey(apiKeys?.getValhallaApiKey() as String).setBiking()
}
}

Expand Down
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)
}
}