Skip to content

Commit

Permalink
Merge pull request #85 from meetacy/main
Browse files Browse the repository at this point in the history
Fetch upstream
  • Loading branch information
y9san9 authored Nov 26, 2023
2 parents 2d181a7 + 8fd9542 commit 2ffd9e5
Show file tree
Hide file tree
Showing 27 changed files with 349 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public class KtorMeetacyEngine(

private val auth = AuthEngine(
baseUrl = baseUrl,
httpClient = this.httpClient
httpClient = this.httpClient,
rsocketClient = rsocketClient,
json = this.json
)

private val users = UsersEngine(
Expand Down Expand Up @@ -114,6 +116,10 @@ public class KtorMeetacyEngine(
return when (request) {
// auth
is GenerateAuthRequest -> auth.generate(request) as T
// telegram
is AwaitTelegramAuthRequest -> auth.telegram.await(request) as T
is FinishTelegramAuthRequest -> auth.telegram.finish(request) as T
is PreloginTelegramAuthRequest -> auth.telegram.prelogin(request) as T
// friends
is AddFriendRequest -> friends.add(request) as T
is DeleteFriendRequest -> friends.delete(request) as T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,38 @@
package app.meetacy.sdk.engine.ktor.requests.auth

import app.meetacy.sdk.engine.ktor.apiVersion
import app.meetacy.sdk.engine.ktor.requests.auth.telegram.AuthTelegramEngine
import app.meetacy.sdk.engine.ktor.response.bodyAsSuccess
import app.meetacy.sdk.engine.requests.GenerateAuthRequest
import app.meetacy.sdk.types.annotation.UnsafeConstructor
import app.meetacy.sdk.types.auth.Token
import app.meetacy.sdk.types.url.Url
import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.client.HttpClient
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json

internal class AuthEngine(
private val baseUrl: Url,
private val httpClient: HttpClient
baseUrl: Url,
private val httpClient: HttpClient,
rsocketClient: HttpClient,
json: Json
) {
private val baseUrl: Url = baseUrl / "auth"

val telegram: AuthTelegramEngine = AuthTelegramEngine(
baseUrl = this.baseUrl,
httpClient = httpClient,
rsocketClient = rsocketClient,
json = json
)

@Serializable
private data class GenerateAuthBody(val nickname: String)

suspend fun generate(request: GenerateAuthRequest): GenerateAuthRequest.Response {
val url = baseUrl / "auth" / "generate"
val url = baseUrl / "generate"
val body = GenerateAuthBody(request.nickname)
val response = httpClient
.post(url.string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
@file:OptIn(UnsafeConstructor::class)

package app.meetacy.sdk.engine.ktor.requests.auth.telegram

import app.meetacy.sdk.engine.ktor.apiVersion
import app.meetacy.sdk.engine.ktor.handleRSocketExceptions
import app.meetacy.sdk.engine.ktor.response.ServerResponse
import app.meetacy.sdk.engine.ktor.response.StatusTrueResponse
import app.meetacy.sdk.engine.ktor.response.bodyAsSuccess
import app.meetacy.sdk.engine.requests.AwaitTelegramAuthRequest
import app.meetacy.sdk.engine.requests.FinishTelegramAuthRequest
import app.meetacy.sdk.engine.requests.PreloginTelegramAuthRequest
import app.meetacy.sdk.types.annotation.UnsafeConstructor
import app.meetacy.sdk.types.auth.Token
import app.meetacy.sdk.types.auth.telegram.TempTelegramAuth
import app.meetacy.sdk.types.url.Url
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.rsocket.kotlin.ktor.client.rSocket
import io.rsocket.kotlin.payload.buildPayload
import io.rsocket.kotlin.payload.data
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

internal class AuthTelegramEngine(
baseUrl: Url,
private val httpClient: HttpClient,
private val rsocketClient: HttpClient,
private val json: Json
) {
private val baseUrl = baseUrl / "telegram"

@Serializable
private data class AwaitTelegramAuthInit(
val temporalToken: String,
val apiVersion: Int
)

suspend fun await(
request: AwaitTelegramAuthRequest
): AwaitTelegramAuthRequest.Response = handleRSocketExceptions(json) {
val url = baseUrl.replaceProtocolWithWebsocket() / "await"
val socket = rsocketClient.rSocket(
urlString = url.string,
secure = url.protocol.isWss
)

val serializable = with(request) {
AwaitTelegramAuthInit(
temporalToken = temporalToken.string,
apiVersion = apiVersion.int
)
}
val requestPayload = buildPayload {
data(json.encodeToString(serializable))
}

val responsePayload = socket.requestResponse(requestPayload)
val deserialized = json.decodeFromString<ServerResponse<String>>(
string = responsePayload.data.readText()
) as ServerResponse.Success

return AwaitTelegramAuthRequest.Response(
permanentToken = Token(deserialized.result)
)
}

@Serializable
private data class FinishBody(
val temporalHash: String,
val secretBotKey: String,
val telegramId: Long,
val username: String?,
val firstName: String?,
val lastName: String?,
)

suspend fun finish(
request: FinishTelegramAuthRequest
) {
val url = baseUrl / "finish"
httpClient
.post(url.string) {
apiVersion(request.apiVersion)
setBody(
FinishBody(
temporalHash = request.temporalHash.string,
secretBotKey = request.secretBotKey.string,
telegramId = request.telegramId,
username = request.username,
firstName = request.firstName,
lastName = request.lastName,
)
)
}
.bodyAsSuccess<StatusTrueResponse>()
}

@Serializable
private data class PreloginResponse(
val token: String,
val botLink: String
)

suspend fun prelogin(
request: PreloginTelegramAuthRequest
): PreloginTelegramAuthRequest.Response {
val url = baseUrl / "prelogin"
val response = httpClient
.get(url.string) { apiVersion(request.apiVersion) }
.bodyAsSuccess<PreloginResponse>()
return PreloginTelegramAuthRequest.Response(
tempAuth = TempTelegramAuth(
token = Token(response.token),
botLink = Url(response.botLink)
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,28 @@ internal class FriendsEngine(
private data class AddFriendBody(val friendId: UserIdSerializable)
private fun AddFriendRequest.toBody() = AddFriendBody(friendId.serializable())

suspend fun add(request: AddFriendRequest): StatusTrueResponse {
suspend fun add(request: AddFriendRequest) {
val url = baseUrl / "add"
val body = request.toBody()
val response = httpClient.post(url.string) {
httpClient.post(url.string) {
apiVersion(request.apiVersion)
token(request.token)
setBody(body)
}.body<StatusTrueResponse>()
return response
}

@Serializable
private data class DeleteFriendBody(val friendId: UserIdSerializable)
private fun DeleteFriendRequest.toBody() = DeleteFriendBody(friendId.serializable())

suspend fun delete(request: DeleteFriendRequest): StatusTrueResponse {
suspend fun delete(request: DeleteFriendRequest) {
val url = baseUrl / "delete"
val body = request.toBody()
val response = httpClient.post(url.string) {
httpClient.post(url.string) {
apiVersion(request.apiVersion)
token(request.token)
setBody(body)
}.body<StatusTrueResponse>()
return response
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import app.meetacy.sdk.types.serializable.meeting.serializable
import app.meetacy.sdk.types.serializable.user.UserIdSerializable
import app.meetacy.sdk.types.serializable.user.serializable
import app.meetacy.sdk.types.url.Url
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import kotlinx.serialization.Serializable

internal class InvitationsEngine(
Expand All @@ -33,6 +34,7 @@ internal class InvitationsEngine(
val meetingId: MeetingIdSerializable,
val userId: UserIdSerializable
)

private fun CreateInvitationRequest.toBody() = CreateInvitationBody(
meetingId.serializable(),
userId.serializable()
Expand All @@ -53,14 +55,15 @@ internal class InvitationsEngine(

@Serializable
private data class AcceptInvitationBody(val invitationId: InvitationIdSerializable)

private fun AcceptInvitationRequest.toBody() = AcceptInvitationBody(invitationId.serializable())

suspend fun accept(
request: AcceptInvitationRequest
): StatusTrueResponse {
) {
val url = baseUrl / "accept"
val body = request.toBody()
return httpClient.post(url.string) {
httpClient.post(url.string) {
apiVersion(request.apiVersion)
token(request.token)
setBody(body)
Expand All @@ -69,14 +72,15 @@ internal class InvitationsEngine(

@Serializable
private data class DenyInvitationBody(val invitationId: InvitationIdSerializable)

private fun DenyInvitationRequest.toBody() = DenyInvitationBody(invitationId.serializable())

suspend fun deny(
request: DenyInvitationRequest
): StatusTrueResponse {
) {
val url = baseUrl / "deny"
val body = request.toBody()
return httpClient.post(url.string) {
httpClient.post(url.string) {
apiVersion(request.apiVersion)
token(request.token)
setBody(body)
Expand All @@ -85,14 +89,15 @@ internal class InvitationsEngine(

@Serializable
private data class CancelInvitationBody(val invitationId: InvitationIdSerializable)

private fun CancelInvitationRequest.toBody() = CancelInvitationBody(invitationId.serializable())

suspend fun cancel(
request: CancelInvitationRequest
): StatusTrueResponse {
) {
val url = baseUrl / "cancel"
val body = request.toBody()
return httpClient.post(url.string) {
httpClient.post(url.string) {
apiVersion(request.apiVersion)
token(request.token)
setBody(body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import app.meetacy.sdk.engine.ktor.apiVersion
import app.meetacy.sdk.engine.ktor.response.StatusTrueResponse
import app.meetacy.sdk.engine.ktor.response.bodyAsSuccess
import app.meetacy.sdk.engine.ktor.token
import app.meetacy.sdk.engine.requests.*
import app.meetacy.sdk.engine.requests.CreateMeetingRequest
import app.meetacy.sdk.engine.requests.EditMeetingRequest
import app.meetacy.sdk.engine.requests.GetMeetingRequest
import app.meetacy.sdk.engine.requests.ListActiveMeetingsRequest
import app.meetacy.sdk.engine.requests.ListMeetingParticipantsRequest
import app.meetacy.sdk.engine.requests.ListMeetingsHistoryRequest
import app.meetacy.sdk.engine.requests.ListMeetingsMapRequest
import app.meetacy.sdk.engine.requests.ListPastMeetingsRequest
import app.meetacy.sdk.engine.requests.ParticipateMeetingRequest
import app.meetacy.sdk.types.optional.map
import app.meetacy.sdk.types.serializable.amount.AmountSerializable
import app.meetacy.sdk.types.serializable.amount.serializable
Expand All @@ -27,9 +35,11 @@ import app.meetacy.sdk.types.serializable.paging.type
import app.meetacy.sdk.types.serializable.user.UserSerializable
import app.meetacy.sdk.types.serializable.user.type
import app.meetacy.sdk.types.url.Url
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import kotlinx.serialization.Serializable

internal class MeetingsEngine(
Expand All @@ -43,6 +53,7 @@ internal class MeetingsEngine(
val amount: AmountSerializable,
val pagingId: PagingIdSerializable?
)

private fun ListMeetingsHistoryRequest.toBody() = ListMeetingsPagingBody(
amount.serializable(),
pagingId?.serializable()
Expand Down Expand Up @@ -113,6 +124,7 @@ internal class MeetingsEngine(
private data class ListMeetingsMapBody(
val location: LocationSerializable
)

private fun ListMeetingsMapRequest.toBody() = ListMeetingsMapBody(location.serializable())

suspend fun listMeetingsMap(
Expand Down Expand Up @@ -140,6 +152,7 @@ internal class MeetingsEngine(
val visibility: MeetingSerializable.Visibility,
val avatarId: FileIdSerializable?
)

private fun CreateMeetingRequest.toBody() = CreateMeetingBody(
title,
description,
Expand Down Expand Up @@ -201,6 +214,7 @@ internal class MeetingsEngine(
val amount: AmountSerializable,
val pagingId: PagingIdSerializable?
)

private fun ListMeetingParticipantsRequest.toBody() = ListMeetingParticipantsBody(
meetingId.serializable(),
amount.serializable(),
Expand All @@ -226,12 +240,13 @@ internal class MeetingsEngine(

@Serializable
private data class ParticipateMeetingBody(val meetingId: MeetingIdSerializable)

private fun ParticipateMeetingRequest.toBody() = ParticipateMeetingBody(meetingId.serializable())

suspend fun participateMeeting(request: ParticipateMeetingRequest): StatusTrueResponse {
suspend fun participateMeeting(request: ParticipateMeetingRequest) {
val url = baseUrl / "participate"
val body = request.toBody()
return httpClient.post(url.string) {
httpClient.post(url.string) {
apiVersion(request.apiVersion)
token(request.token)
setBody(body)
Expand All @@ -240,6 +255,7 @@ internal class MeetingsEngine(

@Serializable
private data class GetMeetingBody(val meetingId: MeetingIdSerializable)

private fun GetMeetingRequest.toBody() = GetMeetingBody(meetingId.serializable())

suspend fun getMeeting(request: GetMeetingRequest): GetMeetingRequest.Response {
Expand Down
Loading

0 comments on commit 2ffd9e5

Please sign in to comment.