-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
231 additions
and
154 deletions.
There are no files selected for viewing
146 changes: 0 additions & 146 deletions
146
core/network/src/commonMain/kotlin/com/stslex/core/network/api/server/ServerApiClient.kt
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
...etwork/src/commonMain/kotlin/com/stslex/core/network/api/server/client/ServerApiClient.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,5 @@ | ||
package com.stslex.core.network.api.server.client | ||
|
||
import com.stslex.core.network.api.base.NetworkClient | ||
|
||
interface ServerApiClient : NetworkClient |
23 changes: 23 additions & 0 deletions
23
...rk/src/commonMain/kotlin/com/stslex/core/network/api/server/client/ServerApiClientImpl.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,23 @@ | ||
package com.stslex.core.network.api.server.client | ||
|
||
import com.stslex.core.core.AppDispatcher | ||
import com.stslex.core.network.api.server.http_client.ServerHttpClient | ||
import com.stslex.core.network.api.server.model.ErrorRepeatEnd | ||
import io.ktor.client.HttpClient | ||
import kotlinx.coroutines.withContext | ||
|
||
class ServerApiClientImpl( | ||
private val appDispatcher: AppDispatcher, | ||
private val client: ServerHttpClient | ||
) : ServerApiClient { | ||
|
||
override suspend fun <T> request( | ||
request: suspend HttpClient.() -> T | ||
): T = withContext(appDispatcher.io) { | ||
try { | ||
request(client.authClient) | ||
} catch (error: ErrorRepeatEnd) { | ||
request(client.authClient) | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...rk/src/commonMain/kotlin/com/stslex/core/network/api/server/error_handler/ErrorHandler.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,5 @@ | ||
package com.stslex.core.network.api.server.error_handler | ||
|
||
import io.ktor.client.plugins.CallRequestExceptionHandler | ||
|
||
interface ErrorHandler : CallRequestExceptionHandler |
52 changes: 52 additions & 0 deletions
52
...rc/commonMain/kotlin/com/stslex/core/network/api/server/error_handler/ErrorHandlerImpl.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,52 @@ | ||
package com.stslex.core.network.api.server.error_handler | ||
|
||
import com.stslex.core.network.api.server.error_handler.RefreshTokenValidator.setupResponseValidator | ||
import com.stslex.core.network.api.server.http_client.ServerHttpClient | ||
import com.stslex.core.network.api.server.model.ErrorRepeatEnd | ||
import com.stslex.core.network.api.server.model.TokenResponseModel | ||
import com.stslex.core.network.utils.token.AuthController | ||
import com.stslex.core.network.utils.token.toModel | ||
import io.ktor.client.call.body | ||
import io.ktor.client.plugins.ResponseException | ||
import io.ktor.client.request.HttpRequest | ||
import io.ktor.client.request.bearerAuth | ||
import io.ktor.client.request.get | ||
import io.ktor.http.HttpStatusCode | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.launch | ||
|
||
class ErrorHandlerImpl( | ||
private val client: Lazy<ServerHttpClient>, | ||
private val tokenProvider: AuthController, | ||
) : ErrorHandler { | ||
|
||
private var refreshJob: Job? = null | ||
|
||
override suspend fun invoke(cause: Throwable, request: HttpRequest) { | ||
when { | ||
cause !is ResponseException -> throw cause | ||
cause.response.status.value == HttpStatusCode.Unauthorized.value -> refreshToken() | ||
else -> throw cause | ||
} | ||
} | ||
|
||
private suspend fun refreshToken() { | ||
if (refreshJob?.isActive == true) return | ||
refreshJob = coroutineScope { | ||
launch { | ||
val tokenResponse = client | ||
.value | ||
.client | ||
.setupResponseValidator() | ||
.get("passport/refresh") { | ||
bearerAuth(tokenProvider.refreshToken) | ||
} | ||
.body<TokenResponseModel>() | ||
tokenProvider.update(tokenResponse.toModel()) | ||
// TODO remove error throw after refresh token (move logic out from throwing) | ||
throw ErrorRepeatEnd | ||
} | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...mmonMain/kotlin/com/stslex/core/network/api/server/error_handler/RefreshTokenValidator.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,27 @@ | ||
package com.stslex.core.network.api.server.error_handler | ||
|
||
import com.stslex.core.network.api.server.model.ErrorRefresh | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.plugins.CallRequestExceptionHandler | ||
import io.ktor.client.plugins.HttpResponseValidator | ||
import io.ktor.client.plugins.ResponseException | ||
import io.ktor.client.request.HttpRequest | ||
import io.ktor.http.HttpStatusCode | ||
|
||
internal object RefreshTokenValidator : CallRequestExceptionHandler { | ||
|
||
fun HttpClient.setupResponseValidator(): HttpClient = config { | ||
HttpResponseValidator { | ||
handleResponseExceptionWithRequest(this@RefreshTokenValidator) | ||
} | ||
} | ||
|
||
override suspend fun invoke(cause: Throwable, request: HttpRequest) { | ||
throw when { | ||
cause !is ResponseException -> cause | ||
cause.response.status.value == HttpStatusCode.Unauthorized.value -> ErrorRefresh | ||
else -> cause | ||
} | ||
} | ||
} | ||
|
10 changes: 10 additions & 0 deletions
10
.../src/commonMain/kotlin/com/stslex/core/network/api/server/http_client/ServerHttpClient.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.stslex.core.network.api.server.http_client | ||
|
||
import io.ktor.client.HttpClient | ||
|
||
interface ServerHttpClient { | ||
|
||
val client: HttpClient | ||
|
||
val authClient: HttpClient | ||
} |
65 changes: 65 additions & 0 deletions
65
.../commonMain/kotlin/com/stslex/core/network/api/server/http_client/ServerHttpClientImpl.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,65 @@ | ||
package com.stslex.core.network.api.server.http_client | ||
|
||
import Wizard.core.network.BuildConfig | ||
import com.stslex.core.network.api.base.NetworkClientBuilder.setupLogging | ||
import com.stslex.core.network.api.base.NetworkClientBuilder.setupNegotiation | ||
import com.stslex.core.network.api.server.error_handler.ErrorHandler | ||
import com.stslex.core.network.utils.token.AuthController | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.engine.cio.CIO | ||
import io.ktor.client.plugins.HttpResponseValidator | ||
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.defaultRequest | ||
import io.ktor.client.request.headers | ||
import io.ktor.http.ContentType | ||
import io.ktor.http.URLProtocol | ||
import io.ktor.http.contentType | ||
|
||
class ServerHttpClientImpl( | ||
private val errorHandler: ErrorHandler, | ||
private val tokenProvider: AuthController | ||
) : ServerHttpClient { | ||
|
||
override val client: HttpClient = HttpClient(CIO) { | ||
setupNegotiation() | ||
setupLogging() | ||
expectSuccess = true | ||
HttpResponseValidator { | ||
handleResponseExceptionWithRequest(errorHandler) | ||
} | ||
defaultRequest { | ||
url( | ||
scheme = URLProtocol.HTTP.name, | ||
host = BuildConfig.SERVER_HOST, | ||
port = BuildConfig.SERVER_PORT.toInt(), | ||
path = BuildConfig.SERVER_API_VERSION, | ||
block = { | ||
contentType(ContentType.Application.Json) | ||
} | ||
) | ||
headers { | ||
append(API_KEY_NAME, BuildConfig.SERVER_API_KEY) | ||
} | ||
} | ||
} | ||
|
||
override val authClient: HttpClient | ||
get() = client.config { | ||
install(Auth) { | ||
bearer { | ||
loadTokens { | ||
BearerTokens( | ||
accessToken = tokenProvider.accessToken, | ||
refreshToken = tokenProvider.refreshToken | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private const val API_KEY_NAME = "X-Api-Key" | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
core/network/src/commonMain/kotlin/com/stslex/core/network/api/server/model/ErrorRefresh.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,9 @@ | ||
package com.stslex.core.network.api.server.model | ||
|
||
/** | ||
* Error refresh token response | ||
* @see ServerApiClientImpl.refreshToken | ||
* @see ServerApiClientImpl.errorParser | ||
* @see ServerApiClientImpl.request | ||
*/ | ||
data object ErrorRefresh : Throwable() |
8 changes: 8 additions & 0 deletions
8
.../network/src/commonMain/kotlin/com/stslex/core/network/api/server/model/ErrorRepeatEnd.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,8 @@ | ||
package com.stslex.core.network.api.server.model | ||
|
||
/** | ||
* Error repeat request. | ||
* Show that the request was repeated after a refresh token | ||
* @see ServerApiClientImpl.request | ||
*/ | ||
internal data object ErrorRepeatEnd : Throwable() |
2 changes: 1 addition & 1 deletion
2
.../network/api/server/TokenResponseModel.kt → ...rk/api/server/model/TokenResponseModel.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
...twork/src/commonMain/kotlin/com/stslex/core/network/clients/auth/client/AuthClientImpl.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
...src/commonMain/kotlin/com/stslex/core/network/clients/profile/client/ProfileClientImpl.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
Oops, something went wrong.