From cb0b3350d2792f9379c860868fb28743de768b19 Mon Sep 17 00:00:00 2001 From: Alex Sokol / y9san9 Date: Tue, 5 Mar 2024 19:28:42 +0300 Subject: [PATCH] feat: added get user by username --- .../sdk/engine/ktor/KtorMeetacyEngine.kt | 3 ++- .../sdk/engine/ktor/exception/getException.kt | 2 ++ .../engine/ktor/requests/users/UsersEngine.kt | 21 ++++++++++++------- ...etUserRequest.kt => GetUserByIdRequest.kt} | 6 +++--- .../requests/GetUserByUsernameRequest.kt | 13 ++++++++++++ .../exception/MeetacyUserNotFoundException.kt | 11 ++++++++++ .../kotlin/app/meetacy/sdk/users/UsersApi.kt | 4 ++-- gradle/libs.versions.toml | 2 +- 8 files changed, 48 insertions(+), 14 deletions(-) rename api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/{GetUserRequest.kt => GetUserByIdRequest.kt} (68%) create mode 100644 api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetUserByUsernameRequest.kt create mode 100644 api/src/commonMain/kotlin/app/meetacy/sdk/exception/MeetacyUserNotFoundException.kt diff --git a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/KtorMeetacyEngine.kt b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/KtorMeetacyEngine.kt index 2c6ea287..425db344 100644 --- a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/KtorMeetacyEngine.kt +++ b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/KtorMeetacyEngine.kt @@ -127,7 +127,8 @@ public class KtorMeetacyEngine( is EmitFriendsLocationRequest -> friends.streamFriendsLocation(request) as T // users is GetMeRequest -> users.getMe(request) as T - is GetUserRequest -> users.getUser(request) as T + is GetUserByIdRequest -> users.getUserById(request) as T + is GetUserByUsernameRequest -> users.getUserByUsername(request) as T is EditUserRequest -> users.editUser(request) as T is UsernameAvailableRequest -> users.usernameAvailable(request) as T // meetings diff --git a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/exception/getException.kt b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/exception/getException.kt index c30f6e61..0c26b59a 100644 --- a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/exception/getException.kt +++ b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/exception/getException.kt @@ -3,6 +3,7 @@ package app.meetacy.sdk.engine.ktor.exception import app.meetacy.sdk.engine.ktor.response.ServerResponse import app.meetacy.sdk.exception.MeetacyInternalException import app.meetacy.sdk.exception.MeetacyUnauthorizedException +import app.meetacy.sdk.exception.MeetacyUserNotFoundException import app.meetacy.sdk.exception.MeetacyUsernameAlreadyOccupiedException internal fun getException( @@ -11,6 +12,7 @@ internal fun getException( return when (error.errorCode) { MeetacyUnauthorizedException.CODE -> MeetacyUnauthorizedException(error.errorMessage) MeetacyUsernameAlreadyOccupiedException.CODE -> MeetacyUsernameAlreadyOccupiedException(error.errorMessage) + MeetacyUserNotFoundException.CODE -> MeetacyUserNotFoundException(error.errorMessage) else -> MeetacyInternalException(error.errorMessage) } } diff --git a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/users/UsersEngine.kt b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/users/UsersEngine.kt index e5178407..fbc74f68 100644 --- a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/users/UsersEngine.kt +++ b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/users/UsersEngine.kt @@ -3,10 +3,7 @@ package app.meetacy.sdk.engine.ktor.requests.users import app.meetacy.sdk.engine.ktor.apiVersion import app.meetacy.sdk.engine.ktor.response.bodyAsSuccess import app.meetacy.sdk.engine.ktor.token -import app.meetacy.sdk.engine.requests.EditUserRequest -import app.meetacy.sdk.engine.requests.GetMeRequest -import app.meetacy.sdk.engine.requests.GetUserRequest -import app.meetacy.sdk.engine.requests.UsernameAvailableRequest +import app.meetacy.sdk.engine.requests.* import app.meetacy.sdk.types.optional.map import app.meetacy.sdk.types.serializable.file.FileIdSerializable import app.meetacy.sdk.types.serializable.file.serializable @@ -35,14 +32,24 @@ internal class UsersEngine( return GetMeRequest.Response(response.type() as SelfUserDetails) } - suspend fun getUser(request: GetUserRequest): GetUserRequest.Response { + suspend fun getUserById(request: GetUserByIdRequest): GetUserByIdRequest.Response { val url = baseUrl / "get" val response = httpClient.get(url.string) { apiVersion(request.apiVersion) token(request.token) - parameter("id", request.userId?.string) + parameter("id", request.userId.string) }.bodyAsSuccess() - return GetUserRequest.Response(response.type()) + return GetUserByIdRequest.Response(response.type()) + } + + suspend fun getUserByUsername(request: GetUserByUsernameRequest): GetUserByUsernameRequest.Response { + val url = baseUrl / "get" + val response = httpClient.post(url.string) { + apiVersion(request.apiVersion) + token(request.token) + parameter("username", request.username.string) + }.bodyAsSuccess() + return GetUserByUsernameRequest.Response(response.type()) } @Serializable diff --git a/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetUserRequest.kt b/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetUserByIdRequest.kt similarity index 68% rename from api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetUserRequest.kt rename to api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetUserByIdRequest.kt index dea7b5af..17694aa3 100644 --- a/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetUserRequest.kt +++ b/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetUserByIdRequest.kt @@ -4,9 +4,9 @@ import app.meetacy.sdk.types.auth.Token import app.meetacy.sdk.types.user.UserDetails import app.meetacy.sdk.types.user.UserId -public data class GetUserRequest( +public data class GetUserByIdRequest( val token: Token, - val userId: UserId? -) : MeetacyRequest { + val userId: UserId +) : MeetacyRequest { public data class Response(val user: UserDetails) } diff --git a/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetUserByUsernameRequest.kt b/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetUserByUsernameRequest.kt new file mode 100644 index 00000000..d91d608a --- /dev/null +++ b/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetUserByUsernameRequest.kt @@ -0,0 +1,13 @@ +package app.meetacy.sdk.engine.requests + +import app.meetacy.sdk.types.auth.Token +import app.meetacy.sdk.types.user.UserDetails +import app.meetacy.sdk.types.user.UserId +import app.meetacy.sdk.types.user.Username + +public data class GetUserByUsernameRequest( + val token: Token, + val username: Username +) : MeetacyRequest { + public data class Response(val user: UserDetails) +} diff --git a/api/src/commonMain/kotlin/app/meetacy/sdk/exception/MeetacyUserNotFoundException.kt b/api/src/commonMain/kotlin/app/meetacy/sdk/exception/MeetacyUserNotFoundException.kt new file mode 100644 index 00000000..7ec33d95 --- /dev/null +++ b/api/src/commonMain/kotlin/app/meetacy/sdk/exception/MeetacyUserNotFoundException.kt @@ -0,0 +1,11 @@ +package app.meetacy.sdk.exception + +public class MeetacyUserNotFoundException(message: String) : MeetacyResponseException( + code = CODE, + message = message, + cause = null +) { + public companion object { + public const val CODE: Int = 8 + } +} diff --git a/api/src/commonMain/kotlin/app/meetacy/sdk/users/UsersApi.kt b/api/src/commonMain/kotlin/app/meetacy/sdk/users/UsersApi.kt index ae880259..11551630 100644 --- a/api/src/commonMain/kotlin/app/meetacy/sdk/users/UsersApi.kt +++ b/api/src/commonMain/kotlin/app/meetacy/sdk/users/UsersApi.kt @@ -2,7 +2,7 @@ package app.meetacy.sdk.users import app.meetacy.sdk.MeetacyApi import app.meetacy.sdk.engine.requests.EditUserRequest -import app.meetacy.sdk.engine.requests.GetUserRequest +import app.meetacy.sdk.engine.requests.GetUserByIdRequest import app.meetacy.sdk.engine.requests.UsernameAvailableRequest import app.meetacy.sdk.types.auth.Token import app.meetacy.sdk.types.file.FileId @@ -20,7 +20,7 @@ import app.meetacy.sdk.types.user.Username */ public class UsersApi(private val api: MeetacyApi) { public suspend fun get(token: Token, userId: UserId): UserDetailsRepository { - val user = api.engine.execute(GetUserRequest(token, userId)).user + val user = api.engine.execute(GetUserByIdRequest(token, userId)).user return UserDetailsRepository.of(user, api) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 03f8a68a..09bbd809 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -2,7 +2,7 @@ kotlin = "1.9.0" ktor = "2.3.6" -meetacySdk = "0.0.64" +meetacySdk = "0.0.65" # kotlinx kotlinxCoroutines = "1.6.4"