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 e62efccf..61778a48 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 @@ -45,9 +45,9 @@ public class KtorMeetacyEngine( install(RSocketSupport) } - private val auth = AuthEngine(baseUrl, this.httpClient, this.json) - private val users = UsersEngine(baseUrl, this.httpClient, this.json) - private val friends = FriendsEngine(baseUrl, this.httpClient, this.json) + private val auth = AuthEngine(baseUrl, this.httpClient) + private val users = UsersEngine(baseUrl, this.httpClient) + private val friends = FriendsEngine(baseUrl, this.httpClient) private val meetings = MeetingsEngine(baseUrl, this.httpClient, this.json) private val files = FilesEngine(baseUrl, this.httpClient) private val invitations = InvitationsEngine(baseUrl, this.httpClient, this.json) diff --git a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/friends/FriendsEngine.kt b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/friends/FriendsEngine.kt index 34e0ed76..9f385392 100644 --- a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/friends/FriendsEngine.kt +++ b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/friends/FriendsEngine.kt @@ -2,28 +2,23 @@ package app.meetacy.sdk.engine.ktor.requests.friends -import app.meetacy.sdk.engine.ktor.handleRSocketExceptions -import app.meetacy.sdk.engine.ktor.mapToLocation -import app.meetacy.sdk.engine.ktor.mapToRegularUser -import app.meetacy.sdk.engine.ktor.mapToUser -import app.meetacy.sdk.engine.ktor.requests.extencion.post -import app.meetacy.sdk.engine.ktor.models.ListFriendsResponse -import app.meetacy.sdk.engine.ktor.models.Location as ModelLocation -import app.meetacy.sdk.engine.ktor.models.StatusTrueResponse -import app.meetacy.sdk.engine.ktor.models.User as ModelUser -import app.meetacy.sdk.engine.requests.AddFriendRequest -import app.meetacy.sdk.engine.requests.DeleteFriendRequest -import app.meetacy.sdk.engine.requests.EmitFriendsLocationRequest -import app.meetacy.sdk.engine.requests.ListFriendsRequest +import app.meetacy.sdk.engine.ktor.* +import app.meetacy.sdk.engine.ktor.response.StatusTrueResponse +import app.meetacy.sdk.engine.requests.* import app.meetacy.sdk.types.annotation.UnsafeConstructor import app.meetacy.sdk.types.datetime.DateTime import app.meetacy.sdk.types.location.Location import app.meetacy.sdk.types.paging.PagingId import app.meetacy.sdk.types.paging.PagingResponse +import app.meetacy.sdk.types.serializable.user.UserIdSerializable +import app.meetacy.sdk.types.serializable.user.serializable import app.meetacy.sdk.types.url.Url import app.meetacy.sdk.types.user.RegularUser import app.meetacy.sdk.types.user.UserLocationSnapshot import io.ktor.client.* +import io.ktor.client.call.* +import io.ktor.client.request.* +import io.ktor.client.statement.* import io.rsocket.kotlin.ktor.client.rSocket import io.rsocket.kotlin.payload.Payload import io.rsocket.kotlin.payload.buildPayload @@ -38,21 +33,23 @@ import kotlinx.serialization.json.put internal class FriendsEngine( baseUrl: Url, - private val httpClient: HttpClient, - private val json: Json + private val httpClient: HttpClient ) { private val baseUrl = baseUrl / "friends" - suspend fun add(request: AddFriendRequest): app.meetacy.sdk.engine.ktor.models.StatusTrueResponse { - val url = baseUrl / "add" - - val jsonObject = buildJsonObject { - put("friendId", request.friendId.string) - } + @Serializable + private data class AddFriendBody(val friendId: UserIdSerializable) + private fun AddFriendRequest.toBody() = AddFriendBody(friendId.serializable()) - val string = post(url.string, jsonObject, httpClient, request) - - return json.decodeFromString(string) + suspend fun add(request: AddFriendRequest): StatusTrueResponse { + val url = baseUrl / "add" + val body = request.toBody() + val response = httpClient.post(url.string) { + apiVersion(request.apiVersion) + token(request.token) + setBody(body) + }.body() + return response } suspend fun delete(request: DeleteFriendRequest): app.meetacy.sdk.engine.ktor.models.StatusTrueResponse { diff --git a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/search/SearchEngine.kt b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/search/SearchEngine.kt index 87f3c223..88b2e1f0 100644 --- a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/search/SearchEngine.kt +++ b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/search/SearchEngine.kt @@ -4,7 +4,6 @@ import app.meetacy.sdk.engine.requests.SearchRequest import app.meetacy.sdk.types.url.Url import io.ktor.client.* import io.ktor.client.request.* -import kotlinx.serialization.json.Json internal class SearchEngine( baseUrl: Url, @@ -12,9 +11,9 @@ internal class SearchEngine( ) { val baseUrl: Url = baseUrl / "search" - suspend fun search(request: SearchRequest): SearchRequest.Response { + suspend fun search(request: SearchRequest){ httpClient.get(baseUrl.string) { - + request.location } } } 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 fd5e9d49..e24ce0cb 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 @@ -1,20 +1,13 @@ package app.meetacy.sdk.engine.ktor.requests.users import app.meetacy.sdk.engine.ktor.apiVersion -import app.meetacy.sdk.engine.ktor.mapToSelfUser -import app.meetacy.sdk.engine.ktor.mapToUser -import app.meetacy.sdk.engine.ktor.requests.extencion.post -import app.meetacy.sdk.engine.ktor.requests.extencion.postWithoutToken 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.exception.meetacyApiError import app.meetacy.sdk.types.annotation.UnsafeConstructor -import app.meetacy.sdk.types.file.FileId -import app.meetacy.sdk.types.optional.ifPresent import app.meetacy.sdk.types.optional.map import app.meetacy.sdk.types.serializable.file.FileIdSerializable import app.meetacy.sdk.types.serializable.file.serializable @@ -23,46 +16,33 @@ import app.meetacy.sdk.types.serializable.optional.serializable import app.meetacy.sdk.types.serializable.user.* import app.meetacy.sdk.types.url.Url import app.meetacy.sdk.types.user.SelfUser -import app.meetacy.sdk.types.user.User -import app.meetacy.sdk.types.user.Username import io.ktor.client.* import io.ktor.client.request.* -import io.ktor.http.* -import io.ktor.http.HttpHeaders.Authorization import kotlinx.serialization.Serializable -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.buildJsonObject -import kotlinx.serialization.json.put -import app.meetacy.sdk.engine.ktor.models.GetUserRequest as ModelGetUserRequest internal class UsersEngine( baseUrl: Url, - private val httpClient: HttpClient, - private val json: Json + private val httpClient: HttpClient ) { private val baseUrl = baseUrl / "users" + private fun toBody() = GetUserBody(null) + suspend fun getMe(request: GetMeRequest): GetMeRequest.Response { val url = baseUrl / "get" - - val jsonObject = buildJsonObject { - put("id", ModelGetUserRequest().id) - } - - val string = post(url.string, jsonObject, httpClient, request) - - val response = json.decodeFromString(string) - - return GetMeRequest.Response( - (response.result?.mapToUser() ?: meetacyApiError("'result' should present")) - as? SelfUser ?: meetacyApiError("Resulted user is not SelfUser") - ) + val body = toBody() + val response = httpClient.post(url.string) { + apiVersion(request.apiVersion) + token(request.token) + setBody(body) + }.bodyAsSuccess() + return GetMeRequest.Response(response.type() as SelfUser) } @Serializable - private data class GetUserBody(val id: UserIdSerializable) + private data class GetUserBody(val id: UserIdSerializable?) - private fun GetUserRequest.toBody() = GetUserBody(userId.serializable()) + private fun GetUserRequest.toBody() = GetUserBody(userId?.serializable()) suspend fun getUser(request: GetUserRequest): GetUserRequest.Response { val url = baseUrl / "get" @@ -100,18 +80,23 @@ internal class UsersEngine( return EditUserRequest.Response(result) } - @OptIn(UnsafeConstructor::class) - suspend fun usernameAvailable(request: UsernameAvailableRequest): UsernameAvailableRequest.Response { - val url = baseUrl / "username" / "available" + @Serializable + private data class UsernameAvailableBody(val username: UsernameSerializable?) - val jsonObject = buildJsonObject { - put("username", request.username.string) - } + private fun UsernameAvailableRequest.toBody() = UsernameAvailableBody( + username = username.serializable() + ) - val string = postWithoutToken(url.string, jsonObject, httpClient, request) - val response = json.decodeFromString(string).username + @OptIn(UnsafeConstructor::class) + suspend fun usernameAvailable(request: UsernameAvailableRequest): UsernameAvailableRequest.Response { + val url = baseUrl / "username" / "available" + val body = request.toBody() + val response = httpClient.post(url.string) { + apiVersion(request.apiVersion) + setBody(body) + }.bodyAsSuccess() - return UsernameAvailableRequest.Response(username = Username(response)) + return UsernameAvailableRequest.Response(response.type()) } } diff --git a/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/response/SimpleResponse.kt b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/response/SimpleResponse.kt new file mode 100644 index 00000000..d1f964a7 --- /dev/null +++ b/api/api-ktor/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/response/SimpleResponse.kt @@ -0,0 +1,12 @@ +package app.meetacy.sdk.engine.ktor.response + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +internal data class StatusTrueResponse ( + + @SerialName("status") + val status: Boolean + +) \ No newline at end of file diff --git a/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/AddFriendRequest.kt b/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/AddFriendRequest.kt index e2e78de4..69703126 100644 --- a/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/AddFriendRequest.kt +++ b/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/AddFriendRequest.kt @@ -4,6 +4,6 @@ import app.meetacy.sdk.types.auth.Token import app.meetacy.sdk.types.user.UserId public data class AddFriendRequest( - override val token: Token, + val token: Token, val friendId: UserId -) : SimpleMeetacyRequest, SimpleMeetacyRequestWithToken +) : SimpleMeetacyRequest diff --git a/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GenerateAuthRequest.kt b/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GenerateAuthRequest.kt index 85794d28..8759e9be 100644 --- a/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GenerateAuthRequest.kt +++ b/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GenerateAuthRequest.kt @@ -4,6 +4,6 @@ import app.meetacy.sdk.types.auth.Token public data class GenerateAuthRequest( val nickname: String -) : MeetacyRequest, TokenProviderEmpty { +) : MeetacyRequest{ public data class Response(val token: Token) } diff --git a/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetMeRequest.kt b/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetMeRequest.kt index cc11c405..25ea81c9 100644 --- a/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetMeRequest.kt +++ b/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetMeRequest.kt @@ -4,7 +4,7 @@ import app.meetacy.sdk.types.auth.Token import app.meetacy.sdk.types.user.SelfUser public data class GetMeRequest( - override val token: Token -): MeetacyRequest, MeetacyRequestWithToken { + val token: Token +): MeetacyRequest{ public data class Response(val me: SelfUser) } diff --git a/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetUserRequest.kt b/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetUserRequest.kt index 9ab53e3a..469533a0 100644 --- a/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetUserRequest.kt +++ b/api/src/commonMain/kotlin/app/meetacy/sdk/engine/requests/GetUserRequest.kt @@ -5,9 +5,9 @@ import app.meetacy.sdk.types.user.User import app.meetacy.sdk.types.user.UserId public data class GetUserRequest( - override val token: Token, - val userId: UserId -) : MeetacyRequest, MeetacyRequestWithToken { + val token: Token, + val userId: UserId? +) : MeetacyRequest { public data class Response( val user: User ) diff --git a/types/serializable/src/commonMain/kotlin/app/meetacy/sdk/types/serializable/user/UserSerializable.kt b/types/serializable/src/commonMain/kotlin/app/meetacy/sdk/types/serializable/user/UserSerializable.kt index 158c8367..ecc6419d 100644 --- a/types/serializable/src/commonMain/kotlin/app/meetacy/sdk/types/serializable/user/UserSerializable.kt +++ b/types/serializable/src/commonMain/kotlin/app/meetacy/sdk/types/serializable/user/UserSerializable.kt @@ -35,7 +35,7 @@ public fun UserSerializable.type(): User = if (isSelf) { id = id.type(), email = email?.type(), nickname = nickname, - emailVerified = emailVerified!!, + emailVerified = emailVerified ?: error("Self user must always return emailVerified parameter"), username = username?.type(), avatarId = avatarId?.type() ) @@ -45,6 +45,6 @@ public fun UserSerializable.type(): User = if (isSelf) { nickname = nickname, username = username?.type(), avatarId = avatarId?.type(), - relationship = relationship!!.type() + relationship = relationship?.type() ?: error("Regular user should always return relationship parameter") ) }