-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP(#76-support-search): added DateTime in progress
- Loading branch information
Showing
7 changed files
with
135 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 31 additions & 29 deletions
60
...mmonMain/kotlin/app/meetacy/sdk/engine/ktor/requests/notifications/NotificationsEngine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,59 @@ | ||
package app.meetacy.sdk.engine.ktor.requests.notifications | ||
|
||
import app.meetacy.sdk.engine.ktor.mapToNotification | ||
import app.meetacy.sdk.engine.ktor.requests.extencion.post | ||
import app.meetacy.sdk.engine.ktor.models.ListNotificationsResponse | ||
import app.meetacy.sdk.engine.ktor.models.StatusTrueResponse | ||
import app.meetacy.sdk.engine.ktor.apiVersion | ||
import app.meetacy.sdk.engine.ktor.response.ListMeetingsResponse | ||
import app.meetacy.sdk.engine.ktor.response.ListNotificationsResponse | ||
import app.meetacy.sdk.engine.ktor.response.bodyAsSuccess | ||
import app.meetacy.sdk.engine.ktor.token | ||
import app.meetacy.sdk.engine.requests.CancelInvitationRequest | ||
import app.meetacy.sdk.engine.requests.ListNotificationsRequest | ||
import app.meetacy.sdk.engine.requests.ReadNotificationRequest | ||
import app.meetacy.sdk.types.paging.PagingId | ||
import app.meetacy.sdk.types.paging.PagingResponse | ||
import app.meetacy.sdk.types.serializable.amount.AmountSerializable | ||
import app.meetacy.sdk.types.serializable.amount.serializable | ||
import app.meetacy.sdk.types.serializable.invitation.InvitationIdSerializable | ||
import app.meetacy.sdk.types.serializable.invitation.serializable | ||
import app.meetacy.sdk.types.serializable.paging.PagingIdSerializable | ||
import app.meetacy.sdk.types.serializable.paging.serializable | ||
import app.meetacy.sdk.types.url.Url | ||
import io.ktor.client.* | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.buildJsonObject | ||
import kotlinx.serialization.json.put | ||
import io.ktor.client.request.* | ||
import kotlinx.serialization.Serializable | ||
|
||
internal class NotificationsEngine( | ||
baseUrl: Url, | ||
private val httpClient: HttpClient, | ||
private val json: Json | ||
private val httpClient: HttpClient | ||
) { | ||
private val baseUrl = baseUrl / "notifications" | ||
|
||
@Serializable | ||
private data class ListNotificationsBody( | ||
val amount: AmountSerializable, | ||
val pagingId: PagingIdSerializable? | ||
) | ||
private fun ListNotificationsRequest.toBody() = ListNotificationsBody(amount.serializable(), pagingId?.serializable()) | ||
|
||
suspend fun list( | ||
request: ListNotificationsRequest | ||
): ListNotificationsRequest.Response = with (request) { | ||
): ListNotificationsRequest.Response { | ||
val url = baseUrl / "list" | ||
|
||
val jsonObject = buildJsonObject { | ||
put("amount", amount.int) | ||
put("pagingId", pagingId?.string) | ||
} | ||
|
||
val string = post(url.string, jsonObject, httpClient, request) | ||
|
||
val response = json.decodeFromString<app.meetacy.sdk.engine.ktor.models.ListNotificationsResponse>(string).result | ||
val body = request.toBody() | ||
val response = httpClient.post(url.string) { | ||
apiVersion(request.apiVersion) | ||
token(request.token) | ||
setBody(body) | ||
}.bodyAsSuccess<ListNotificationsResponse>() | ||
|
||
val paging = PagingResponse( | ||
data = response.data.map { it.mapToNotification() }, | ||
data = response.data.map { it }, | ||
nextPagingId = response.nextPagingId?.let(::PagingId) | ||
) | ||
|
||
ListNotificationsRequest.Response(paging) | ||
return ListNotificationsRequest.Response(paging) | ||
} | ||
|
||
suspend fun read(request: ReadNotificationRequest): app.meetacy.sdk.engine.ktor.models.StatusTrueResponse = with (request) { | ||
val url = baseUrl / "read" | ||
|
||
val jsonObject = buildJsonObject { | ||
put("lastNotificationId", request.lastNotificationId.string) | ||
} | ||
|
||
val string = post(url.string, jsonObject, httpClient, request) | ||
|
||
return json.decodeFromString<app.meetacy.sdk.engine.ktor.models.StatusTrueResponse>(string) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...or/src/commonMain/kotlin/app/meetacy/sdk/engine/ktor/response/ListNotificationResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package app.meetacy.sdk.engine.ktor.response | ||
|
||
import app.meetacy.sdk.types.serializable.notification.NotificationSerializable | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
internal data class ListNotificationsResponse ( | ||
@SerialName("data") | ||
val data: List<NotificationSerializable>, | ||
@SerialName("nextPagingId") | ||
val nextPagingId: String? = null | ||
) |
12 changes: 12 additions & 0 deletions
12
...c/commonMain/kotlin/app/meetacy/sdk/types/serializable/datetime/DateOrTimeSerializable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package app.meetacy.sdk.types.serializable.datetime | ||
|
||
import app.meetacy.sdk.types.datetime.DateOrTime | ||
import kotlinx.serialization.Serializable | ||
import kotlin.jvm.JvmInline | ||
|
||
@Serializable | ||
@JvmInline | ||
public value class DateOrTimeSerializable(public val string: String) | ||
|
||
public fun DateOrTimeSerializable.type(): DateOrTime = DateOrTime.parse(string) | ||
public fun DateOrTime.serializable(): DateOrTimeSerializable = DateOrTimeSerializable(iso8601) |
12 changes: 12 additions & 0 deletions
12
...src/commonMain/kotlin/app/meetacy/sdk/types/serializable/datetime/DateTimeSerializable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package app.meetacy.sdk.types.serializable.datetime | ||
|
||
import app.meetacy.sdk.types.datetime.DateTime | ||
import kotlinx.serialization.Serializable | ||
import kotlin.jvm.JvmInline | ||
|
||
@Serializable | ||
@JvmInline | ||
public value class DateTimeSerializable(public val iso8601: String) | ||
|
||
public fun DateTimeSerializable.type(): DateTime = DateTime.parse(iso8601) | ||
public fun DateTime.serializable(): DateTimeSerializable = DateTimeSerializable(iso8601) |
12 changes: 12 additions & 0 deletions
12
...Main/kotlin/app/meetacy/sdk/types/serializable/notification/NotificationIdSerializable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package app.meetacy.sdk.types.serializable.notification | ||
|
||
import app.meetacy.sdk.types.notification.NotificationId | ||
import kotlinx.serialization.Serializable | ||
import kotlin.jvm.JvmInline | ||
|
||
|
||
@Serializable | ||
@JvmInline | ||
public value class NotificationIdSerializable(public val string: String) | ||
public fun NotificationIdSerializable.type(): NotificationId = NotificationId(string) | ||
public fun NotificationId.serializable(): NotificationIdSerializable = NotificationIdSerializable(string) |
54 changes: 54 additions & 0 deletions
54
...onMain/kotlin/app/meetacy/sdk/types/serializable/notification/NotificationSerializable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package app.meetacy.sdk.types.serializable.notification | ||
|
||
import app.meetacy.sdk.types.notification.Notification | ||
import app.meetacy.sdk.types.serializable.datetime.DateTimeSerializable | ||
import app.meetacy.sdk.types.serializable.datetime.type | ||
import app.meetacy.sdk.types.serializable.meeting.MeetingSerializable | ||
import app.meetacy.sdk.types.serializable.meeting.type | ||
import app.meetacy.sdk.types.serializable.user.UserSerializable | ||
import app.meetacy.sdk.types.serializable.user.type | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
public sealed class NotificationSerializable { | ||
public abstract val id: NotificationIdSerializable | ||
public abstract val isNew: Boolean | ||
public abstract val date: DateTimeSerializable | ||
|
||
@SerialName("subscription") | ||
@Serializable | ||
public class Subscription( | ||
override val isNew: Boolean, | ||
override val id: NotificationIdSerializable, | ||
override val date: DateTimeSerializable, | ||
public val subscriber: UserSerializable, | ||
) : NotificationSerializable() | ||
|
||
@SerialName("meeting_invitation") | ||
@Serializable | ||
public class Invitation( | ||
override val id: NotificationIdSerializable, | ||
override val isNew: Boolean, | ||
override val date: DateTimeSerializable, | ||
public val meeting: MeetingSerializable, | ||
public val inviter: UserSerializable, | ||
) : NotificationSerializable() | ||
} | ||
|
||
public fun NotificationSerializable.type(): Notification = | ||
when (this) { | ||
is NotificationSerializable.Invitation -> Notification.Invitation( | ||
id = id.type(), | ||
isNew = isNew, | ||
meeting = meeting.type(), | ||
date = date.type(), | ||
inviter = inviter.type() | ||
) | ||
is NotificationSerializable.Subscription -> Notification.Subscription( | ||
id = id.serializable(), | ||
isNew = isNew, | ||
subscriber = subscriber.mapToEndpoint(), | ||
date = date.serializable() | ||
) | ||
} |