Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get user by username #92

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -26,32 +23,33 @@ internal class UsersEngine(
) {
private val baseUrl = baseUrl / "users"

private fun toBody() = GetUserBody(null)

suspend fun getMe(request: GetMeRequest): GetMeRequest.Response {
val url = baseUrl / "get"
val body = toBody()
val response = httpClient.post(url.string) {
val response = httpClient.get(url.string) {
apiVersion(request.apiVersion)
token(request.token)
setBody(body)
}.bodyAsSuccess<UserDetailsSerializable>()
return GetMeRequest.Response(response.type() as SelfUserDetails)
}

@Serializable
private data class GetUserBody(val id: UserIdSerializable?)
private fun GetUserRequest.toBody() = GetUserBody(userId?.serializable())
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)
}.bodyAsSuccess<UserDetailsSerializable>()
return GetUserByIdRequest.Response(response.type())
}

suspend fun getUser(request: GetUserRequest): GetUserRequest.Response {
suspend fun getUserByUsername(request: GetUserByUsernameRequest): GetUserByUsernameRequest.Response {
val url = baseUrl / "get"
val body = request.toBody()
val response = httpClient.post(url.string) {
val response = httpClient.get(url.string) {
apiVersion(request.apiVersion)
token(request.token)
setBody(body)
parameter("username", request.username.string)
}.bodyAsSuccess<UserDetailsSerializable>()
return GetUserRequest.Response(response.type())
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
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.meetacy.sdk.users

import app.meetacy.sdk.AuthorizedMeetacyApi
import app.meetacy.sdk.engine.requests.GetUserByUsernameRequest
import app.meetacy.sdk.types.auth.Token
import app.meetacy.sdk.types.file.FileId
import app.meetacy.sdk.types.optional.Optional
Expand All @@ -24,6 +25,11 @@ public class AuthorizedUsersApi(private val api: AuthorizedMeetacyApi) {
return AuthorizedUserDetailsRepository.of(user.data, api)
}

public suspend fun get(username: Username): AuthorizedUserDetailsRepository {
val user = base.get(token, username)
return AuthorizedUserDetailsRepository.of(user.data, api)
}

public suspend fun edit(
nickname: String,
username: Username?,
Expand Down
10 changes: 8 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,8 @@ 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.GetUserByUsernameRequest
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 +21,12 @@ 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)
}

public suspend fun get(token: Token, username: Username): UserDetailsRepository {
val user = api.engine.execute(GetUserByUsernameRequest(token, username)).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
Loading