-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added core-network & core-preference implementation
- Loading branch information
1 parent
1e06699
commit 381c4d1
Showing
21 changed files
with
545 additions
and
34 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
7 changes: 0 additions & 7 deletions
7
...re-network/src/androidMain/kotlin/com/spotify/app/core_network/shared/Platform.android.kt
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
shared/core-network/src/commonMain/kotlin/com/spotify/app/core_network/shared/Greeting.kt
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
shared/core-network/src/commonMain/kotlin/com/spotify/app/core_network/shared/Platform.kt
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
...re-network/src/commonMain/kotlin/com/spotify/app/core_network/shared/api/HttpClientApi.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,10 @@ | ||
package com.spotify.app.core_network.shared.api | ||
|
||
import com.spotify.app.core_network.shared.impl.HttpEngineProvider | ||
import io.ktor.client.HttpClient | ||
|
||
|
||
interface HttpClientApi { | ||
fun getHttpClient(httpEngineProvider: HttpEngineProvider): HttpClient | ||
|
||
} |
146 changes: 146 additions & 0 deletions
146
...twork/src/commonMain/kotlin/com/spotify/app/core_network/shared/impl/HttpClientApiImpl.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,146 @@ | ||
package com.spotify.app.core_network.shared.impl | ||
|
||
import com.spotify.app.core_logger.shared.api.LoggerApi | ||
import com.spotify.app.core_network.shared.CoreNetworkBuildKonfig | ||
import com.spotify.app.core_network.shared.api.HttpClientApi | ||
import com.spotify.app.core_preferences.shared.impl.util.PreferenceUtil | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.call.body | ||
import io.ktor.client.plugins.HttpTimeout | ||
import io.ktor.client.plugins.auth.Auth | ||
import io.ktor.client.plugins.auth.providers.BearerTokens | ||
import io.ktor.client.plugins.auth.providers.bearer | ||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation | ||
import io.ktor.client.plugins.defaultRequest | ||
import io.ktor.client.plugins.logging.LogLevel | ||
import io.ktor.client.plugins.logging.Logger | ||
import io.ktor.client.plugins.logging.Logging | ||
import io.ktor.client.plugins.observer.ResponseObserver | ||
import com.spotify.app.core_network.shared.impl.util.NetworkConstants.Endpoints | ||
import com.spotify.app.core_network.shared.impl.model.RefreshTokenRequest | ||
import com.spotify.app.core_network.shared.impl.model.RefreshTokenResponse | ||
import io.ktor.client.request.host | ||
import io.ktor.client.request.post | ||
import io.ktor.client.request.setBody | ||
import io.ktor.client.request.url | ||
import io.ktor.client.statement.bodyAsText | ||
import io.ktor.http.ContentType | ||
import io.ktor.http.URLProtocol | ||
import io.ktor.http.contentType | ||
import io.ktor.serialization.kotlinx.json.json | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.json.Json | ||
|
||
class HttpClientApiImpl( | ||
private val shouldEnableLogging: Boolean, | ||
private val loggerApi: LoggerApi, | ||
private val preferenceUtil: PreferenceUtil | ||
) : HttpClientApi { | ||
|
||
companion object { | ||
private const val SOCKET_TIMEOUT_MILLIS = 60_000L | ||
private const val CONNECT_TIMEOUT_MILLIS = 60_000L | ||
private const val REQUEST_TIMEOUT_MILLIS = 60_000L | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
private val json by lazy { | ||
Json { | ||
ignoreUnknownKeys = true | ||
prettyPrint = false | ||
isLenient = true | ||
useAlternativeNames = true | ||
encodeDefaults = true | ||
explicitNulls = false | ||
} | ||
} | ||
|
||
override fun getHttpClient(httpEngineProvider: HttpEngineProvider): HttpClient { | ||
return HttpClient(httpEngineProvider.clientEngine(shouldEnableLogging)) { | ||
expectSuccess = true | ||
|
||
//Default Request | ||
defaultRequest { | ||
host = CoreNetworkBuildKonfig.BASE_URL | ||
url { | ||
protocol = URLProtocol.HTTPS | ||
} | ||
contentType(ContentType.Application.Json) | ||
} | ||
|
||
//Authenticator | ||
install(Auth) { | ||
bearer { | ||
loadTokens { | ||
BearerTokens( | ||
accessToken = preferenceUtil.getAccessToken()!!, | ||
refreshToken = preferenceUtil.getRefreshToken()!! | ||
) | ||
} | ||
refreshTokens { | ||
val refreshTokenResponse = client.post { | ||
host = CoreNetworkBuildKonfig.BASE_URL_AUTH | ||
url(Endpoints.REFRESH_TOKEN) | ||
contentType(ContentType.parse("application/x-www-form-urlencoded")) | ||
setBody( | ||
RefreshTokenRequest( | ||
grantType = "", | ||
refreshToken = preferenceUtil.getRefreshToken()!!, | ||
clientId = "" | ||
) | ||
) | ||
markAsRefreshTokenRequest() | ||
}.body<RefreshTokenResponse>() | ||
|
||
val accessToken = refreshTokenResponse.accessToken | ||
val refreshToken = refreshTokenResponse.refreshToken | ||
|
||
runBlocking { | ||
preferenceUtil.setAccessToken(accessToken) | ||
preferenceUtil.setRefreshToken(refreshToken) | ||
} | ||
|
||
BearerTokens( | ||
accessToken = accessToken, | ||
refreshToken = refreshToken | ||
) | ||
} | ||
} | ||
} | ||
|
||
//Timeout | ||
install(HttpTimeout) { | ||
requestTimeoutMillis = REQUEST_TIMEOUT_MILLIS | ||
socketTimeoutMillis = SOCKET_TIMEOUT_MILLIS | ||
connectTimeoutMillis = CONNECT_TIMEOUT_MILLIS | ||
} | ||
|
||
//Logging | ||
if (shouldEnableLogging) { | ||
install(Logging) { | ||
logger = object : Logger { | ||
override fun log(message: String) { | ||
loggerApi.logD(message) | ||
} | ||
} | ||
level = LogLevel.ALL | ||
} | ||
} | ||
|
||
//Response Observer | ||
if (shouldEnableLogging) { | ||
install(ResponseObserver) { | ||
onResponse { | ||
loggerApi.logD(it.bodyAsText()) | ||
} | ||
} | ||
} | ||
|
||
//Serialization | ||
install(ContentNegotiation) { | ||
json(json) | ||
} | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...work/src/commonMain/kotlin/com/spotify/app/core_network/shared/impl/HttpEngineProvider.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,7 @@ | ||
package com.spotify.app.core_network.shared.impl | ||
|
||
import io.ktor.client.engine.HttpClientEngine | ||
|
||
interface HttpEngineProvider { | ||
fun clientEngine(shouldEnableLogging: Boolean): HttpClientEngine | ||
} |
16 changes: 16 additions & 0 deletions
16
...c/commonMain/kotlin/com/spotify/app/core_network/shared/impl/model/RefreshTokenRequest.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,16 @@ | ||
package com.spotify.app.core_network.shared.impl.model | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class RefreshTokenRequest( | ||
@SerialName("grant_type") | ||
val grantType: String, | ||
|
||
@SerialName("refresh_token") | ||
val refreshToken: String, | ||
|
||
@SerialName("client_id") | ||
val clientId: String | ||
) |
19 changes: 19 additions & 0 deletions
19
.../commonMain/kotlin/com/spotify/app/core_network/shared/impl/model/RefreshTokenResponse.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.spotify.app.core_network.shared.impl.model | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class RefreshTokenResponse( | ||
@SerialName("access_token") | ||
val accessToken: String, | ||
|
||
@SerialName("refresh_token") | ||
val refreshToken: String, | ||
|
||
@SerialName("token_type") | ||
val tokenType: String, | ||
|
||
@SerialName("expires_in") | ||
val expiresIn: Int | ||
) |
7 changes: 7 additions & 0 deletions
7
...k/src/commonMain/kotlin/com/spotify/app/core_network/shared/impl/util/NetworkConstants.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,7 @@ | ||
package com.spotify.app.core_network.shared.impl.util | ||
|
||
object NetworkConstants { | ||
object Endpoints { | ||
const val REFRESH_TOKEN = "/api/token" | ||
} | ||
} |
9 changes: 0 additions & 9 deletions
9
shared/core-network/src/iosMain/kotlin/com/spotify/app/core_network/shared/Platform.ios.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.