-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create first domain providers and data classes
- Loading branch information
Showing
20 changed files
with
308 additions
and
26 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
42 changes: 42 additions & 0 deletions
42
applivery-android-sdk/src/debug/java/com/applivery/android/sdk/data/auth/SessionManager.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,42 @@ | ||
package com.applivery.android.sdk.data.auth | ||
|
||
import android.content.SharedPreferences | ||
import androidx.core.content.edit | ||
import arrow.core.Option | ||
import com.applivery.android.sdk.domain.SharedPreferencesProvider | ||
|
||
interface SessionManager { | ||
|
||
val isLoggedIn: Boolean | ||
|
||
fun saveToken(token: String) | ||
|
||
fun getToken(): Option<String> | ||
|
||
fun logOut() | ||
} | ||
|
||
class SessionManagerImpl( | ||
private val preferencesProvider: SharedPreferencesProvider | ||
) : SessionManager { | ||
|
||
private val preferences: SharedPreferences get() = preferencesProvider.sharedPreferences | ||
|
||
override val isLoggedIn: Boolean = getToken().isSome() | ||
|
||
override fun saveToken(token: String) { | ||
preferences.edit { putString(KeyToken, token) } | ||
} | ||
|
||
override fun getToken(): Option<String> { | ||
return Option.fromNullable(preferences.getString(KeyToken, null)) | ||
} | ||
|
||
override fun logOut() { | ||
preferences.edit { remove(KeyToken) } | ||
} | ||
|
||
companion object { | ||
private const val KeyToken = "token_key" | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ry/android/sdk/network/base/JsonMapper.kt → ...ivery/android/sdk/data/base/JsonMapper.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
2 changes: 1 addition & 1 deletion
2
...roid/sdk/network/models/ApiErrorSchema.kt → ...android/sdk/data/models/ApiErrorSchema.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
2 changes: 1 addition & 1 deletion
2
...ndroid/sdk/network/models/AppConfigApi.kt → ...y/android/sdk/data/models/AppConfigApi.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
2 changes: 1 addition & 1 deletion
2
...dk/network/models/ServerResponseSchema.kt → ...d/sdk/data/models/ServerResponseSchema.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
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
36 changes: 36 additions & 0 deletions
36
...y-android-sdk/src/debug/java/com/applivery/android/sdk/data/service/HeadersInterceptor.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,36 @@ | ||
package com.applivery.android.sdk.data.service | ||
|
||
import android.os.Build | ||
import com.applivery.android.sdk.BuildConfig | ||
import com.applivery.android.sdk.domain.HostAppPackageInfoProvider | ||
import okhttp3.Interceptor | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import java.util.Locale | ||
|
||
class HeadersInterceptor( | ||
private val packageInfoProvider: HostAppPackageInfoProvider | ||
) : Interceptor { | ||
|
||
override fun intercept(chain: Interceptor.Chain): Response { | ||
return chain.proceed(composeRequest(chain)) | ||
} | ||
|
||
private fun composeRequest(chain: Interceptor.Chain): Request { | ||
val original = chain.request() | ||
val packageInfo = packageInfoProvider.packageInfo | ||
return original.newBuilder() | ||
.addHeader("Accept-Language", Locale.getDefault().language) | ||
.addHeader("x-sdk-version", "ANDROID_" + BuildConfig.LibraryVersion) | ||
.addHeader("x-app-version", packageInfo.versionName) | ||
.addHeader("x-os-version", Build.VERSION.RELEASE) | ||
.addHeader("x-os-name", "android") | ||
.addHeader("x-device-vendor", Build.MANUFACTURER) | ||
.addHeader("x-device-model", Build.MODEL) | ||
.addHeader("x-package-name", packageInfo.packageName) | ||
.addHeader("x-package-version", packageInfo.versionCode.toString()) | ||
.addHeader("x-os-minsdkversion", packageInfo.minSdkVersion.toString()) | ||
.addHeader("x-os-targetsdkversion", packageInfo.targetSdkVersion.toString()) | ||
.build() | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...y-android-sdk/src/debug/java/com/applivery/android/sdk/data/service/SessionInterceptor.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,34 @@ | ||
package com.applivery.android.sdk.data.service | ||
|
||
import com.applivery.android.sdk.data.auth.SessionManager | ||
import com.applivery.android.sdk.domain.InstallationIdProvider | ||
import okhttp3.Interceptor | ||
import okhttp3.Interceptor.Chain | ||
import okhttp3.Response | ||
|
||
class SessionInterceptor( | ||
private val sessionManager: SessionManager, | ||
private val idProvider: InstallationIdProvider, | ||
private val appToken: String | ||
) : Interceptor { | ||
|
||
override fun intercept(chain: Chain): Response { | ||
val requestBuilder = chain.request() | ||
.newBuilder() | ||
.addHeader("Authorization", "Bearer $appToken") | ||
.addHeader("x-installation-token", idProvider.installationId) | ||
|
||
sessionManager.getToken().onSome { | ||
requestBuilder.addHeader("x-sdk-auth-token", it) | ||
} | ||
val response = chain.proceed(requestBuilder.build()) | ||
if (response.code == CodeUnauthorized) { | ||
sessionManager.logOut() | ||
} | ||
return response | ||
} | ||
|
||
companion object { | ||
private const val CodeUnauthorized = 401 | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
...android-sdk/src/debug/java/com/applivery/android/sdk/domain/HostAppPackageInfoProvider.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,38 @@ | ||
package com.applivery.android.sdk.domain | ||
|
||
import android.content.Context | ||
import android.os.Build | ||
import com.applivery.android.sdk.domain.model.PackageInfo | ||
import android.content.pm.PackageInfo as AndroidPackageInfo | ||
|
||
interface HostAppPackageInfoProvider { | ||
|
||
val packageInfo: PackageInfo | ||
} | ||
|
||
class AndroidHostAppPackageInfoProvider( | ||
private val context: Context | ||
) : HostAppPackageInfoProvider { | ||
|
||
override val packageInfo: PackageInfo | ||
get() { | ||
val pInfo = context.packageManager.getPackageInfo(context.packageName, 0) | ||
val appInfo = context.packageManager.getApplicationInfo(context.packageName, 0) | ||
return PackageInfo( | ||
packageName = pInfo.packageName, | ||
versionCode = pInfo.versionCodeCompat, | ||
versionName = pInfo.versionName, | ||
minSdkVersion = appInfo.minSdkVersion, | ||
targetSdkVersion = appInfo.targetSdkVersion | ||
) | ||
} | ||
|
||
private val AndroidPackageInfo.versionCodeCompat: Long | ||
get() { | ||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | ||
longVersionCode | ||
} else { | ||
versionCode.toLong() | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ery-android-sdk/src/debug/java/com/applivery/android/sdk/domain/InstallationIdProvider.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,29 @@ | ||
package com.applivery.android.sdk.domain | ||
|
||
import androidx.core.content.edit | ||
import java.util.UUID | ||
|
||
interface InstallationIdProvider { | ||
|
||
val installationId: String | ||
} | ||
|
||
class InstallationIdProviderImpl( | ||
private val sharedPreferencesProvider: SharedPreferencesProvider | ||
) : InstallationIdProvider { | ||
|
||
private val preferences get() = sharedPreferencesProvider.sharedPreferences | ||
|
||
override val installationId: String | ||
get() { | ||
return preferences.getString(KeyInstallationId, null).orEmpty().ifEmpty { | ||
UUID.randomUUID().toString().also { | ||
preferences.edit { putString(KeyInstallationId, it) } | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private const val KeyInstallationId = "applivery_id_token_key" | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...-android-sdk/src/debug/java/com/applivery/android/sdk/domain/SharedPreferencesProvider.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,19 @@ | ||
package com.applivery.android.sdk.domain | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
|
||
interface SharedPreferencesProvider { | ||
|
||
val sharedPreferences: SharedPreferences | ||
} | ||
|
||
class AndroidSharedPreferencesProvider(private val context: Context) : SharedPreferencesProvider { | ||
|
||
override val sharedPreferences: SharedPreferences | ||
get() = context.getSharedPreferences(PreferencesName, Context.MODE_PRIVATE) | ||
|
||
companion object { | ||
private const val PreferencesName = "applivery-data" | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
applivery-android-sdk/src/debug/java/com/applivery/android/sdk/domain/model/Error.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,3 @@ | ||
package com.applivery.android.sdk.domain.model | ||
|
||
abstract class Error : Throwable() |
Oops, something went wrong.