Skip to content

Commit

Permalink
WIP(#76-support-search): friends fix in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
y9Kap committed Oct 27, 2023
1 parent f08f485 commit ae4cc3c
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<app.meetacy.sdk.engine.ktor.models.StatusTrueResponse>(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<StatusTrueResponse>()
return response
}

suspend fun delete(request: DeleteFriendRequest): app.meetacy.sdk.engine.ktor.models.StatusTrueResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ 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,
private val httpClient: HttpClient
) {
val baseUrl: Url = baseUrl / "search"

suspend fun search(request: SearchRequest): SearchRequest.Response {
suspend fun search(request: SearchRequest){
httpClient.get(baseUrl.string) {

request.location
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<app.meetacy.sdk.engine.ktor.models.GetUserResponse>(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<UserSerializable>()
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"
Expand Down Expand Up @@ -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<app.meetacy.sdk.engine.ktor.models.ValidateUsernameRequest>(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<UsernameSerializable>()

return UsernameAvailableRequest.Response(username = Username(response))
return UsernameAvailableRequest.Response(response.type())
}
}
Original file line number Diff line number Diff line change
@@ -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

)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import app.meetacy.sdk.types.auth.Token

public data class GenerateAuthRequest(
val nickname: String
) : MeetacyRequest<GenerateAuthRequest.Response>, TokenProviderEmpty<GenerateAuthRequest.Response> {
) : MeetacyRequest<GenerateAuthRequest.Response>{
public data class Response(val token: Token)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetMeRequest.Response>, MeetacyRequestWithToken<GetMeRequest.Response> {
val token: Token
): MeetacyRequest<GetMeRequest.Response>{
public data class Response(val me: SelfUser)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetUserRequest.Response>, MeetacyRequestWithToken<GetUserRequest.Response> {
val token: Token,
val userId: UserId?
) : MeetacyRequest<GetUserRequest.Response> {
public data class Response(
val user: User
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
Expand All @@ -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")
)
}

0 comments on commit ae4cc3c

Please sign in to comment.