-
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
34 changed files
with
509 additions
and
65 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
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
4 changes: 2 additions & 2 deletions
4
core/network/src/commonMain/kotlin/com/stslex/core/network/api/base/BaseNetworkClient.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
7 changes: 0 additions & 7 deletions
7
...etwork/src/commonMain/kotlin/com/stslex/core/network/api/base/DefaultNetworkClientImpl.kt
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
core/network/src/commonMain/kotlin/com/stslex/core/network/api/server/ServerApi.kt
This file was deleted.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
core/network/src/commonMain/kotlin/com/stslex/core/network/api/server/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,94 @@ | ||
package com.stslex.core.network.api.server | ||
|
||
import com.stslex.core.core.AppDispatcher | ||
import com.stslex.core.network.api.base.NetworkClient | ||
import com.stslex.core.network.api.base.NetworkClientBuilder.setupLogging | ||
import com.stslex.core.network.api.base.NetworkClientBuilder.setupNegotiation | ||
import com.stslex.core.network.utils.token.AuthController | ||
import com.stslex.core.network.utils.token.toModel | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.call.body | ||
import io.ktor.client.engine.cio.CIO | ||
import io.ktor.client.plugins.HttpResponseValidator | ||
import io.ktor.client.plugins.ResponseException | ||
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.HttpRequest | ||
import io.ktor.client.request.get | ||
import io.ktor.client.request.headers | ||
import io.ktor.http.ContentType | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.http.URLProtocol | ||
import io.ktor.http.contentType | ||
import io.ktor.http.encodedPath | ||
import kotlinx.coroutines.withContext | ||
|
||
interface ServerApiClient : NetworkClient | ||
|
||
class ServerApiClientImpl( | ||
private val tokenProvider: AuthController, | ||
private val appDispatcher: AppDispatcher | ||
) : ServerApiClient { | ||
|
||
private val client = HttpClient(CIO) { | ||
setupNegotiation() | ||
|
||
setupLogging() | ||
expectSuccess = true | ||
|
||
install(Auth) { | ||
bearer { | ||
loadTokens { | ||
BearerTokens( | ||
accessToken = tokenProvider.accessToken, | ||
refreshToken = tokenProvider.refreshToken | ||
) | ||
} | ||
} | ||
} | ||
HttpResponseValidator { | ||
handleResponseExceptionWithRequest(errorParser) | ||
} | ||
defaultRequest { | ||
url(API_HOST) { | ||
host = API_HOST | ||
encodedPath = "api/v1" | ||
protocol = URLProtocol.HTTP | ||
contentType(ContentType.Application.Json) | ||
} | ||
headers { | ||
append(API_KEY_NAME, "API_KEY") // TODO | ||
} | ||
} | ||
} | ||
|
||
override suspend fun <T> request( | ||
request: suspend HttpClient.() -> T | ||
): T = withContext(appDispatcher.io) { | ||
request(client) | ||
} | ||
|
||
private val errorParser: suspend (Throwable, HttpRequest) -> Unit | ||
get() = { exception, _ -> | ||
val clientException = exception as? ResponseException ?: throw exception | ||
if (HttpStatusCode.Unauthorized.value == clientException.response.status.value) { | ||
refreshToken() | ||
} else { | ||
throw clientException | ||
} | ||
} | ||
|
||
private suspend fun refreshToken() { | ||
val tokenResponse = client | ||
.get("passport/refresh") | ||
.body<TokenResponseModel>() | ||
tokenProvider.update(tokenResponse.toModel()) | ||
} | ||
|
||
companion object { | ||
private const val API_KEY_NAME = "API_KEY" | ||
private const val API_HOST = "api_host" | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
core/network/src/commonMain/kotlin/com/stslex/core/network/api/server/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.stslex.core.network.api.server | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class TokenResponseModel( | ||
@SerialName("uuid") | ||
val uuid: String, | ||
@SerialName("username") | ||
val username: String, | ||
@SerialName("access_token") | ||
val accessToken: String, | ||
@SerialName("refresh_token") | ||
val refreshToken: String | ||
) |
11 changes: 11 additions & 0 deletions
11
core/network/src/commonMain/kotlin/com/stslex/core/network/clients/auth/client/AuthClient.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,11 @@ | ||
package com.stslex.core.network.clients.auth.client | ||
|
||
import com.stslex.core.network.clients.auth.response.LoginOkResponse | ||
|
||
interface AuthClient { | ||
|
||
suspend fun authUser(login: String, password: String): LoginOkResponse | ||
|
||
suspend fun registerUser(login: String, username: String, password: String): LoginOkResponse | ||
} | ||
|
53 changes: 53 additions & 0 deletions
53
...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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.stslex.core.network.clients.auth.client | ||
|
||
import com.stslex.core.network.api.server.ServerApiClient | ||
import com.stslex.core.network.clients.auth.request.AuthRequest | ||
import com.stslex.core.network.clients.auth.response.LoginOkResponse | ||
import com.stslex.core.network.clients.auth.response.RegisterRequest | ||
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.request.post | ||
import io.ktor.util.InternalAPI | ||
|
||
class AuthClientImpl( | ||
private val networkClient: ServerApiClient, | ||
private val tokenController: AuthController | ||
) : AuthClient { | ||
|
||
@OptIn(InternalAPI::class) | ||
override suspend fun authUser( | ||
login: String, | ||
password: String | ||
): LoginOkResponse = networkClient.request { | ||
post("$AUTH_URL/login") { | ||
body = AuthRequest( | ||
login = login, | ||
password = password | ||
) | ||
}.body<LoginOkResponse>().also { | ||
tokenController.update(it.toModel()) | ||
} | ||
} | ||
|
||
@OptIn(InternalAPI::class) | ||
override suspend fun registerUser( | ||
login: String, | ||
username: String, | ||
password: String | ||
): LoginOkResponse = networkClient.request { | ||
post("$AUTH_URL/register") { | ||
body = RegisterRequest( | ||
login = login, | ||
password = password, | ||
username = username | ||
) | ||
}.body<LoginOkResponse>().also { | ||
tokenController.update(it.toModel()) | ||
} | ||
} | ||
|
||
companion object { | ||
private const val AUTH_URL = "passport" | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...network/src/commonMain/kotlin/com/stslex/core/network/clients/auth/request/AuthRequest.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,12 @@ | ||
package com.stslex.core.network.clients.auth.request | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class AuthRequest( | ||
@SerialName("login") | ||
val login: String, | ||
@SerialName("password") | ||
val password: String | ||
) |
16 changes: 16 additions & 0 deletions
16
...rk/src/commonMain/kotlin/com/stslex/core/network/clients/auth/response/LoginOkResponse.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.stslex.core.network.clients.auth.response | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class LoginOkResponse( | ||
@SerialName("uuid") | ||
val uuid: String, | ||
@SerialName("username") | ||
val username: String, | ||
@SerialName("access_token") | ||
val accessToken: String, | ||
@SerialName("refresh_token") | ||
val refreshToken: String | ||
) |
14 changes: 14 additions & 0 deletions
14
...rk/src/commonMain/kotlin/com/stslex/core/network/clients/auth/response/RegisterRequest.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,14 @@ | ||
package com.stslex.core.network.clients.auth.response | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class RegisterRequest( | ||
@SerialName("login") | ||
val login: String, | ||
@SerialName("username") | ||
val username: String, | ||
@SerialName("password") | ||
val password: String | ||
) |
Oops, something went wrong.