Skip to content
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
34 changes: 17 additions & 17 deletions library/src/main/java/io/constructor/core/ConstructorIo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package io.constructor.core

import android.annotation.SuppressLint
import android.content.Context
import io.constructor.BuildConfig
import io.constructor.data.ConstructorData
import io.constructor.data.DataManager
import io.constructor.data.local.PreferencesHelper
import io.constructor.data.memory.ConfigMemoryHolder
import io.constructor.data.model.Suggestion
import io.constructor.data.model.SuggestionViewModel
import io.constructor.injection.component.AppComponent
import io.constructor.injection.component.DaggerAppComponent
Expand All @@ -15,6 +16,7 @@ import io.constructor.util.broadcastIntent
import io.constructor.util.d
import io.constructor.util.e
import io.constructor.util.urlEncode
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import java.util.*
Expand Down Expand Up @@ -49,19 +51,19 @@ object ConstructorIo {
}))
}

fun init(context: Context?, apiKey: String, autocompleteResultCount: Map<String, Int> = mapOf(Constants.QueryValues.SEARCH_SUGGESTIONS to 10,
Constants.QueryValues.PRODUCTS to 0), defaultItemSection: String = BuildConfig.AUTOCOMPLETE_SECTION) {
fun init(context: Context?, constructorIoConfig: ConstructorIoConfig) {
if (context == null) {
throw IllegalStateException("context is null, please init library using ConstructorIo.with(context)")
}
this.context = context.applicationContext
dataManager = component.dataManager()
preferenceHelper = component.preferenceHelper()
configMemoryHolder = component.configMemoryHolder()
configMemoryHolder.autocompleteResultCount = autocompleteResultCount
preferenceHelper.token = apiKey
configMemoryHolder.autocompleteResultCount = constructorIoConfig.autocompleteResultCount
configMemoryHolder.testCellParams = constructorIoConfig.testCells
preferenceHelper.token = constructorIoConfig.apiKey

preferenceHelper.defaultItemSection = defaultItemSection
preferenceHelper.defaultItemSection = constructorIoConfig.defaultItemSection
if (preferenceHelper.id.isBlank()) {
preferenceHelper.id = UUID.randomUUID().toString()
}
Expand All @@ -71,29 +73,27 @@ object ConstructorIo {

fun getClientId() = preferenceHelper.id

fun setTestCellValues(pair1: Pair<String, String>, pair2: Pair<String, String>? = null, pair3: Pair<String, String>? = null) {
configMemoryHolder.testCellParams = listOf(pair1, pair2, pair3)
}

fun clearTestCellValues() {
configMemoryHolder.testCellParams = emptyList()
}

internal fun testInit(context: Context?, apiKey: String, dataManager: DataManager, preferenceHelper: PreferencesHelper, configMemoryHolder: ConfigMemoryHolder) {
internal fun testInit(context: Context?, constructorIoConfig: ConstructorIoConfig, dataManager: DataManager, preferenceHelper: PreferencesHelper, configMemoryHolder: ConfigMemoryHolder) {
if (context == null) {
throw IllegalStateException("Context is null, please init library using ConstructorIo.with(context)")
}
this.context = context.applicationContext
this.dataManager = dataManager
this.preferenceHelper = preferenceHelper
this.configMemoryHolder = configMemoryHolder
preferenceHelper.token = apiKey
preferenceHelper.token = constructorIoConfig.apiKey
if (preferenceHelper.id.isBlank()) {
preferenceHelper.id = UUID.randomUUID().toString()
}
}

fun getAutocompleteResults(query: String) = dataManager.getAutocompleteResults(query)
fun getAutocompleteResults(query: String): Observable<ConstructorData<List<Suggestion>?>> {
val params = mutableListOf<Pair<String, String>>()
configMemoryHolder.autocompleteResultCount?.entries?.forEach {
params.add(Pair(Constants.QueryConstants.NUM_RESULTS+it.key, it.value.toString()))
}
return dataManager.getAutocompleteResults(query, params.toTypedArray())
}

fun trackSelect(query: String, suggestion: SuggestionViewModel, errorCallback: ConstructorError = null) {
val sessionId = preferenceHelper.getSessionId(sessionIncrementEventHandler)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.constructor.core

import io.constructor.BuildConfig

data class ConstructorIoConfig(val apiKey: String,
val autocompleteResultCount: Map<String, Int> = mapOf(Constants.QueryValues.SEARCH_SUGGESTIONS to 10, Constants.QueryValues.PRODUCTS to 0),
val defaultItemSection: String = BuildConfig.AUTOCOMPLETE_SECTION,
val testCells: List<Pair<String, String>> = emptyList())
2 changes: 1 addition & 1 deletion library/src/main/java/io/constructor/data/DataManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import javax.inject.Singleton
class DataManager @Inject
constructor(private val constructorApi: ConstructorApi) {

fun getAutocompleteResults(text: String): Observable<ConstructorData<List<Suggestion>?>> = constructorApi.getSuggestions(text).map {
fun getAutocompleteResults(text: String, params: Array<Pair<String, String>> = arrayOf()): Observable<ConstructorData<List<Suggestion>?>> = constructorApi.getSuggestions(text, params.toMap()).map {
if (!it.isError) {
it.response()?.let {
if (it.isSuccessful) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ class TokenInterceptor(val context: Context, private val preferencesHelper: Pref
builder.addQueryParameter(it.first, it.second)
}
}
configMemoryHolder.autocompleteResultCount?.entries?.forEach {
builder.addQueryParameter(Constants.QueryConstants.NUM_RESULTS+it.key, it.value.toString())
}
val url = builder.build()
request = request.newBuilder().url(url).build()
return chain.proceed(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import retrofit2.http.QueryMap
interface ConstructorApi {

@GET(ApiPaths.URL_GET_SUGGESTIONS)
fun getSuggestions(@Path("value") value: String): Single<Result<AutocompleteResult>>
fun getSuggestions(@Path("value") value: String, @QueryMap data: Map<String, String>): Single<Result<AutocompleteResult>>

@GET(ApiPaths.URL_SELECT_EVENT)
fun trackSelect(@Path("term") term: String, @QueryMap data: Map<String, String>, @QueryMap(encoded = true) encodedData: Map<String, String>): Completable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class ConstructorIoTest {
every { pref.id } returns "1"
every { pref.getSessionId() } returns 1
every { pref.getSessionId(any()) } returns 1
constructorIo.testInit(ctx, "dummyKey", data, pref, configMemoryHolder)
constructorIo.testInit(ctx, ConstructorIoConfig("dummyKey",
testCells = listOf("1" to "2", "3" to "4")), data, pref, configMemoryHolder)
}

@After
Expand Down Expand Up @@ -213,8 +214,6 @@ class ConstructorIoTest {
every { configMemoryHolder.testCellParams = any() } just Runs
every { configMemoryHolder.autocompleteResultCount } returns mapOf(Constants.QueryValues.SEARCH_SUGGESTIONS to 10, Constants.QueryValues.PRODUCTS to 0)
every { configMemoryHolder.testCellParams } returns listOf("ef-1" to "2", "ef-3" to "4")
constructorIo.setTestCellValues("1" to "2", "3" to "4")
verify(exactly = 1) { configMemoryHolder.testCellParams = any() }
mockServer.start()
mockServer.enqueue(MockResponse())
var client = OkHttpClient.Builder().addInterceptor(TokenInterceptor(ctx, pref, configMemoryHolder)).build()
Expand Down
11 changes: 6 additions & 5 deletions library/src/test/java/io/constructor/data/DataManagerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DataManagerTest {

@Test
fun getSuggestions() {
every { constructorApi.getSuggestions("titanic") } returns Single.just(Result.response(Response.success(TestDataLoader.loadResponse())))
every { constructorApi.getSuggestions("titanic", any()) } returns Single.just(Result.response(Response.success(TestDataLoader.loadResponse())))
val observer = dataManager.getAutocompleteResults("titanic").test()
observer.assertComplete().assertValue {
it.get()!!.isNotEmpty() && it.get()!!.size == 5
Expand All @@ -36,7 +36,7 @@ class DataManagerTest {

@Test
fun getSuggestionsBadServerResponse() {
every { constructorApi.getSuggestions("titanic") } returns Single.just(Result.response(Response.error(500, ResponseBody.create(MediaType.parse("text/plain"), "Error"))))
every { constructorApi.getSuggestions("titanic", any()) } returns Single.just(Result.response(Response.error(500, ResponseBody.create(MediaType.parse("text/plain"), "Error"))))
val observer = dataManager.getAutocompleteResults("titanic").test()
observer.assertComplete().assertValue {
it.networkError
Expand All @@ -45,7 +45,7 @@ class DataManagerTest {

@Test
fun getSuggestionsException() {
every { constructorApi.getSuggestions("titanic") } returns Single.just(Result.error<AutocompleteResult>(Exception()))
every { constructorApi.getSuggestions("titanic", any()) } returns Single.just(Result.error<AutocompleteResult>(Exception()))
val observer = dataManager.getAutocompleteResults("titanic").test()
observer.assertComplete().assertValue {
it.isError
Expand All @@ -54,7 +54,7 @@ class DataManagerTest {

@Test
fun getSuggestionsUnexpectedDataResponse() {
every { constructorApi.getSuggestions("titanic") } returns Single.just(Result.response(Response.success(TestDataLoader.loadResponseWithUnexpectedData())))
every { constructorApi.getSuggestions("titanic", any()) } returns Single.just(Result.response(Response.success(TestDataLoader.loadResponseWithUnexpectedData())))
val observer = dataManager.getAutocompleteResults("titanic").test()
observer.assertComplete().assertValue {
it.get()!!.isNotEmpty() && it.get()!!.size == 5
Expand All @@ -63,7 +63,8 @@ class DataManagerTest {

@Test
fun getSuggestionsEmptyResponse() {
every { constructorApi.getSuggestions("titanic") } returns Single.just(Result.response(Response.success(TestDataLoader.loadEmptyResponse())))
every { constructorApi.getSuggestions("titanic", any()
) } returns Single.just(Result.response(Response.success(TestDataLoader.loadEmptyResponse())))
val observer = dataManager.getAutocompleteResults("titanic").test()
observer.assertComplete().assertValue {
it.isEmpty
Expand Down
5 changes: 3 additions & 2 deletions sample/src/main/java/io/constructor/sample/SampleApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package io.constructor.sample

import android.app.Application
import io.constructor.core.ConstructorIo
import io.constructor.core.ConstructorIoConfig

class SampleApp : Application() {

override fun onCreate() {
super.onCreate()
ConstructorIo.init(this, "key_OucJxxrfiTVUQx0C")
ConstructorIo.setTestCellValues("ab" to "cd", "11" to "22")
ConstructorIo.init(this, ConstructorIoConfig("key_OucJxxrfiTVUQx0C",
testCells = listOf("ab" to "cd", "11" to "22")))
}
}