Skip to content

Commit

Permalink
feat: added get user by username
Browse files Browse the repository at this point in the history
  • Loading branch information
y9san9 committed Mar 5, 2024
1 parent 5a15614 commit cb0b335
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<UserDetailsSerializable>()
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<UserDetailsSerializable>()
return GetUserByUsernameRequest.Response(response.type())
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetUserRequest.Response> {
val userId: UserId
) : MeetacyRequest<GetUserByIdRequest.Response> {
public data class Response(val user: UserDetails)
}
Original file line number Diff line number Diff line change
@@ -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<GetUserByUsernameRequest.Response> {
public data class Response(val user: UserDetails)
}
Original file line number Diff line number Diff line change
@@ -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
}
}
4 changes: 2 additions & 2 deletions api/src/commonMain/kotlin/app/meetacy/sdk/users/UsersApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit cb0b335

Please sign in to comment.