Skip to content

Commit

Permalink
feat(#76-support-auth-header): remove moko network dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
y9Kap committed Oct 23, 2023
1 parent 414977e commit c315e3d
Show file tree
Hide file tree
Showing 17 changed files with 200 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,17 @@ import app.meetacy.sdk.types.meeting.MeetingId
import app.meetacy.sdk.types.notification.Notification
import app.meetacy.sdk.types.notification.NotificationId
import app.meetacy.sdk.types.user.*
import dev.icerock.moko.network.generated.models.Notification.Type.MEETING_INVITATION
import dev.icerock.moko.network.generated.models.Notification.Type.SUBSCRIPTION
import dev.icerock.moko.network.generated.models.Invitation as GeneratedInvitation
import dev.icerock.moko.network.generated.models.Location as GeneratedLocation
import dev.icerock.moko.network.generated.models.Meeting as GeneratedMeeting
import dev.icerock.moko.network.generated.models.Notification as GeneratedNotification
import dev.icerock.moko.network.generated.models.User as GeneratedUser
import app.meetacy.sdk.engine.ktor.response.models.Meeting as LolMeeting
import app.meetacy.sdk.engine.ktor.response.models.User as LolUser
import app.meetacy.sdk.engine.ktor.response.models.Notification as LolNotification
import app.meetacy.sdk.engine.ktor.response.models.Invitation as LolInvitation
import app.meetacy.sdk.engine.ktor.response.models.Location as LolLocation
import app.meetacy.sdk.engine.ktor.response.models.Invitation as ModelInvitation
import app.meetacy.sdk.engine.ktor.response.models.Location as ModelLocation
import app.meetacy.sdk.engine.ktor.response.models.Meeting as ModelMeeting
import app.meetacy.sdk.engine.ktor.response.models.Notification as ModelNotification
import app.meetacy.sdk.engine.ktor.response.models.User as ModelUser

internal fun GeneratedUser.mapToSelfUser(): SelfUser = mapToUser() as SelfUser
internal fun GeneratedUser.mapToRegularUser(): RegularUser = mapToUser() as RegularUser

internal fun LolUser.mapToSelfUser(): SelfUser = mapToUser() as SelfUser
internal fun LolUser.mapToRegularUser(): RegularUser = mapToUser() as RegularUser
internal fun ModelUser.mapToSelfUser(): SelfUser = mapToUser() as SelfUser
internal fun ModelUser.mapToRegularUser(): RegularUser = mapToUser() as RegularUser

@OptIn(UnsafeConstructor::class)
internal fun GeneratedUser.mapToUser(): User = if (isSelf) {
internal fun ModelUser.mapToUser(): User = if (isSelf) {
SelfUser(
id = UserId(id),
nickname = nickname,
Expand All @@ -54,39 +44,7 @@ internal fun GeneratedUser.mapToUser(): User = if (isSelf) {
)
}

@OptIn(UnsafeConstructor::class)
internal fun LolUser.mapToUser(): User = if (isSelf) {
SelfUser(
id = UserId(id),
nickname = nickname,
email = email?.let(::Email),
emailVerified = emailVerified ?: error("Self user must always return emailVerified parameter"),
username = username?.let(::Username),
avatarId = avatarId?.let(::FileId)
)
} else {
RegularUser(
id = UserId(id),
nickname = nickname,
avatarId = avatarId?.let(::FileId),
username = username?.let(::Username),
relationship = relationship?.mapToRelationship() ?: error("Regular user should always return relationship parameter")
)
}

internal fun GeneratedInvitation.toInvitation(): Invitation = Invitation(
id = identity.let(::InvitationId),
meeting = meeting.mapToMeeting(),
invitedUser = invitedUser.mapToUser(),
inviterUser = inviterUser.mapToUser(),
isAccepted = when (isAccepted) {
null -> AcceptationState.Waiting
true -> AcceptationState.Accepted
false -> AcceptationState.Declined
}
)

internal fun LolInvitation.toInvitation(): Invitation = Invitation(
internal fun ModelInvitation.toInvitation(): Invitation = Invitation(
id = identity.let(::InvitationId),
meeting = meeting.mapToMeeting(),
invitedUser = invitedUser.mapToUser(),
Expand All @@ -106,7 +64,7 @@ internal fun String.mapToRelationship(): Relationship? = when(this) {
else -> null
}

internal fun LolMeeting.mapToMeeting(): Meeting = Meeting(
internal fun ModelMeeting.mapToMeeting(): Meeting = Meeting(
id = MeetingId(id),
creator = creator.mapToUser(),
date = Date(date),
Expand All @@ -118,57 +76,18 @@ internal fun LolMeeting.mapToMeeting(): Meeting = Meeting(
description = description,
participantsCount = participantsCount,
isParticipating = isParticipating,
previewParticipants = previewParticipants.map(LolUser::mapToUser),
previewParticipants = previewParticipants.map(ModelUser::mapToUser),
avatarId = avatarId?.let(::FileId),
visibility = when (visibility) {
LolMeeting.Visibility.PUBLIC -> Meeting.Visibility.Public
LolMeeting.Visibility.PRIVATE -> Meeting.Visibility.Private
ModelMeeting.Visibility.PUBLIC -> Meeting.Visibility.Public
ModelMeeting.Visibility.PRIVATE -> Meeting.Visibility.Private
}
)

internal fun GeneratedMeeting.mapToMeeting(): Meeting = Meeting(
id = MeetingId(id),
creator = creator.mapToUser(),
date = Date(date),
location = Location(
location.latitude,
location.longitude
),
title = title,
description = description,
participantsCount = participantsCount,
isParticipating = isParticipating,
previewParticipants = previewParticipants.map(GeneratedUser::mapToUser),
avatarId = avatarId?.let(::FileId),
visibility = when (visibility) {
GeneratedMeeting.Visibility.PUBLIC -> Meeting.Visibility.Public
GeneratedMeeting.Visibility.PRIVATE -> Meeting.Visibility.Private
}
)

internal fun GeneratedLocation.mapToLocation(): Location =
Location(latitude, longitude)

internal fun GeneratedNotification.mapToNotification(): Notification = when (this.type) {
SUBSCRIPTION -> Notification.Subscription(
id = NotificationId(id),
isNew = isNew,
date = Date(date),
subscriber = subscriber!!.mapToRegularUser()
)
MEETING_INVITATION -> Notification.Invitation(
id = NotificationId(id),
isNew = isNew,
date = Date(date),
meeting = meeting!!.mapToMeeting(),
inviter = inviter!!.mapToRegularUser()
)
}

internal fun LolLocation.mapToLocation(): Location =
internal fun ModelLocation.mapToLocation(): Location =
Location(latitude, longitude)

internal fun LolNotification.mapToNotification(): Notification = when (this.type) {
internal fun ModelNotification.mapToNotification(): Notification = when (this.type) {
app.meetacy.sdk.engine.ktor.response.models.Notification.Type.SUBSCRIPTION -> Notification.Subscription(
id = NotificationId(id),
isNew = isNew,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.meetacy.sdk.engine.ktor.requests.files

import app.meetacy.sdk.engine.ktor.response.models.GenerateIdentityResponse
import app.meetacy.sdk.engine.requests.GetFileRequest
import app.meetacy.sdk.engine.requests.UploadFileRequest
import app.meetacy.sdk.files.DownloadableFile
Expand All @@ -10,7 +11,6 @@ import app.meetacy.sdk.io.asMeetacyInput
import app.meetacy.sdk.types.file.FileId
import app.meetacy.sdk.types.url.Url
import app.meetacy.sdk.types.url.parametersOf
import dev.icerock.moko.network.generated.models.GenerateIdentityResponse
import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.client.request.forms.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ 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.response.models.ListFriendsResponse
import app.meetacy.sdk.engine.ktor.response.models.Location
import app.meetacy.sdk.engine.ktor.response.models.Location as ModelLocation
import app.meetacy.sdk.engine.ktor.response.models.StatusTrueResponse
import app.meetacy.sdk.engine.ktor.response.models.User
import app.meetacy.sdk.engine.ktor.response.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.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.url.Url
Expand All @@ -36,13 +37,14 @@ import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put

internal class FriendsEngine(
private val baseUrl: Url,
baseUrl: Url,
private val httpClient: HttpClient,
private val json: Json
) {
private val baseUrl = baseUrl / "friends"

suspend fun add(request: AddFriendRequest): StatusTrueResponse {
val url = baseUrl / "friends" / "add"
val url = baseUrl / "add"

val jsonObject = buildJsonObject {
put("friendId", request.friendId.string)
Expand All @@ -54,7 +56,7 @@ internal class FriendsEngine(
}

suspend fun delete(request: DeleteFriendRequest): StatusTrueResponse {
val url = baseUrl / "friends" / "delete"
val url = baseUrl / "delete"

val jsonObject = buildJsonObject {
put("friendId", request.friendId.string)
Expand All @@ -66,7 +68,7 @@ internal class FriendsEngine(
}

suspend fun list(request: ListFriendsRequest): ListFriendsRequest.Response {
val url = baseUrl / "friends" / "list"
val url = baseUrl / "list"

val jsonObject = buildJsonObject {
put("amount", request.amount.int.toString())
Expand All @@ -86,7 +88,7 @@ internal class FriendsEngine(
}

suspend fun streamFriendsLocation(request: EmitFriendsLocationRequest) = handleRSocketExceptions(json) {
val url = baseUrl.replaceProtocolWithWebsocket() / "friends" / "location" / "stream"
val url = baseUrl.replaceProtocolWithWebsocket() / "location" / "stream"

val socket = httpClient.rSocket(
urlString = url.string,
Expand Down Expand Up @@ -115,7 +117,7 @@ private fun EmitFriendsLocationRequest.encodeToPayload(json: Json): Payload = bu
data(json.encodeToString(initObject))
}

private fun app.meetacy.sdk.types.location.Location.encodeToPayload(): Payload = buildPayload {
private fun Location.encodeToPayload(): Payload = buildPayload {
val locationString = buildJsonObject {
val `object` = buildJsonObject {
put("latitude", latitude)
Expand All @@ -128,14 +130,14 @@ private fun app.meetacy.sdk.types.location.Location.encodeToPayload(): Payload =
}

@Serializable
private data class LolUserLocationSnapshotSerializable(
val user: User,
val location: Location, // TODO: изменить название класса после удаления зависимостей
private data class ModelUserLocationSnapshotSerializable(
val user: ModelUser,
val location: ModelLocation,
val capturedAt: String
)

private fun Payload.decodeToUserLocationSnapshot(json: Json): UserLocationSnapshot {
val deserialized = json.decodeFromString<LolUserLocationSnapshotSerializable>(data.readText())
val deserialized = json.decodeFromString<ModelUserLocationSnapshotSerializable>(data.readText())

return UserLocationSnapshot(
user = deserialized.user.mapToUser() as RegularUser,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,73 +1,81 @@
package app.meetacy.sdk.engine.ktor.requests.invitations

import app.meetacy.sdk.engine.ktor.requests.extencion.post
import app.meetacy.sdk.engine.ktor.response.models.StatusTrueResponse
import app.meetacy.sdk.engine.ktor.toInvitation
import app.meetacy.sdk.engine.requests.*
import app.meetacy.sdk.engine.requests.AcceptInvitationRequest
import app.meetacy.sdk.engine.requests.CancelInvitationRequest
import app.meetacy.sdk.engine.requests.CreateInvitationRequest
import app.meetacy.sdk.engine.requests.DenyInvitationRequest
import app.meetacy.sdk.types.url.Url
import dev.icerock.moko.network.generated.apis.InvitationsApi
import dev.icerock.moko.network.generated.apis.InvitationsApiImpl
import io.ktor.client.*
import kotlinx.serialization.json.Json
import dev.icerock.moko.network.generated.models.AcceptInvitationRequest as GeneratedAcceptInvitationRequest
import dev.icerock.moko.network.generated.models.CancelInvitationRequest as GeneratedCancelInvitationRequest
import dev.icerock.moko.network.generated.models.CreateInvitationRequest as GeneratedCreateInvitationRequest
import dev.icerock.moko.network.generated.models.DenyInvitationRequest as GeneratedDenyInvitationRequest
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put

internal class InvitationsEngine(
private val baseUrl: Url,
baseUrl: Url,
private val httpClient: HttpClient,
json: Json
private val json: Json
) {
private val base: InvitationsApi = InvitationsApiImpl(baseUrl.string, httpClient, json)

private val baseUrl = baseUrl / "invitations"
suspend fun create(
request: CreateInvitationRequest
): CreateInvitationRequest.Response {
val response = base.invitationsCreatePost(
createInvitationRequest = GeneratedCreateInvitationRequest(
meetingId = request.meetingId.string,
userId = request.userId.string
),
apiVersion = request.apiVersion.int.toString(),
authorization = request.token.string
).result
val url = baseUrl / "create"

val jsonObject = buildJsonObject {
put("meetingId", request.meetingId.string)
put("userId", request.userId.string)
}

val string = post(url.string, jsonObject, httpClient, request)

val response = json.decodeFromString<app.meetacy.sdk.engine.ktor.response.models.CreateInvitationResponse>(string).result

return CreateInvitationRequest.Response(response.toInvitation())
}

suspend fun accept(
request: AcceptInvitationRequest
) {
base.invitationsAcceptPost(
acceptInvitationRequest = GeneratedAcceptInvitationRequest(
id = request.invitationId.string
),
apiVersion = request.apiVersion.int.toString(),
authorization = request.token.string
)
): StatusTrueResponse {
val url = baseUrl / "accept"

val jsonObject = buildJsonObject {
put("invitationId", request.invitationId.string)
}

val string = post(url.string, jsonObject, httpClient, request)

return json.decodeFromString<StatusTrueResponse>(string)
}

suspend fun deny(
request: DenyInvitationRequest
) {
base.invitationsDenyPost(
denyInvitationRequest = GeneratedDenyInvitationRequest(
id = request.invitationId.string
),
apiVersion = request.apiVersion.int.toString(),
authorization = request.token.string
)
): StatusTrueResponse {
val url = baseUrl / "deny"

val jsonObject = buildJsonObject {
put("invitationId", request.invitationId.string)
}

val string = post(url.string, jsonObject, httpClient, request)

return json.decodeFromString<StatusTrueResponse>(string)
}

suspend fun cancel(
request: CancelInvitationRequest
) {
base.invitationsCancelPost(
cancelInvitationRequest = GeneratedCancelInvitationRequest(
id = request.invitationId.string
),
apiVersion = request.apiVersion.int.toString(),
authorization = request.token.string
)
): StatusTrueResponse {
val url = baseUrl / "cancel"

val jsonObject = buildJsonObject {
put("invitationId", request.invitationId.string)
}

val string = post(url.string, jsonObject, httpClient, request)

return json.decodeFromString<StatusTrueResponse>(string)
}

}
Loading

0 comments on commit c315e3d

Please sign in to comment.