Skip to content

Commit

Permalink
feat(#93-add-getSubscriptions-and-getSubscribers-methods): support re…
Browse files Browse the repository at this point in the history
…lationships was added
  • Loading branch information
y9Kap committed Mar 7, 2024
1 parent f000f01 commit 3dce318
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public class KtorMeetacyEngine(
is AddFriendRequest -> friends.add(request) as T
is DeleteFriendRequest -> friends.delete(request) as T
is ListFriendsRequest -> friends.list(request) as T
is GetSubscriptionsRequest -> friends.subscriptions(request) as T
is GetSubscribersRequest -> friends.subscribers(request) as T
is EmitFriendsLocationRequest -> friends.streamFriendsLocation(request) as T
// users
is GetMeRequest -> users.getMe(request) as T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import app.meetacy.sdk.engine.ktor.handleRSocketExceptions
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.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.requests.*
import app.meetacy.sdk.types.annotation.UnsafeConstructor
import app.meetacy.sdk.types.datetime.DateTime
import app.meetacy.sdk.types.location.Location
Expand Down Expand Up @@ -102,6 +99,39 @@ internal class FriendsEngine(
return ListFriendsRequest.Response(response)
}

suspend fun subscriptions(request: GetSubscriptionsRequest): GetSubscriptionsRequest.Response {
val url = baseUrl / "relationship" / "subscriptions"

val response = httpClient.post(url.string) {
apiVersion(request.apiVersion)
token(request.token)
parameter("id", request.userId?.string)
parameter("amount", request.amount.int)
parameter("pagingId", request.pagingId?.string)
}.bodyAsSuccess<PagingResponseSerializable<UserSerializable>>()
.type()
.mapItems { user -> user.type() as RegularUser }

return GetSubscriptionsRequest.Response(response)
}

suspend fun subscribers(request: GetSubscribersRequest): GetSubscribersRequest.Response {
val url = baseUrl / "relationship" / "subscribers"

val response = httpClient.post(url.string) {
apiVersion(request.apiVersion)
token(request.token)
parameter("id", request.userId?.string)
parameter("amount", request.amount.int)
parameter("pagingId", request.pagingId?.string)
}.bodyAsSuccess<PagingResponseSerializable<UserSerializable>>()
.type()
.mapItems { user -> user.type() as RegularUser }

return GetSubscribersRequest.Response(response)
}


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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package app.meetacy.sdk.engine.requests

import app.meetacy.sdk.types.amount.Amount
import app.meetacy.sdk.types.auth.Token
import app.meetacy.sdk.types.paging.PagingId
import app.meetacy.sdk.types.paging.PagingResponse
import app.meetacy.sdk.types.user.RegularUser
import app.meetacy.sdk.types.user.UserId

public data class GetSubscribersRequest(
val token: Token,
val amount: Amount,
val pagingId: PagingId?,
val userId: UserId?,
) : MeetacyRequest<GetSubscriptionsRequest.Response> {
public data class Response(val paging: PagingResponse<RegularUser>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package app.meetacy.sdk.engine.requests

import app.meetacy.sdk.types.amount.Amount
import app.meetacy.sdk.types.auth.Token
import app.meetacy.sdk.types.paging.PagingId
import app.meetacy.sdk.types.paging.PagingResponse
import app.meetacy.sdk.types.user.RegularUser
import app.meetacy.sdk.types.user.UserId

public data class GetSubscriptionsRequest(
val token: Token,
val amount: Amount,
val pagingId: PagingId?,
val userId: UserId?,
) : MeetacyRequest<GetSubscriptionsRequest.Response> {
public data class Response(val paging: PagingResponse<RegularUser>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ public class AuthorizedFriendsApi(private val api: AuthorizedMeetacyApi) {
)
}

public suspend fun subscriptions(
amount: Amount,
pagingId: PagingId? = null,
userId: UserId? = null
): PagingRepository<AuthorizedRegularUserRepository> =
base.subscriptions(token, amount, pagingId, userId).mapItems { user ->
AuthorizedRegularUserRepository(
data = user.data,
api = api
)
}

public suspend fun subscribers(
amount: Amount,
pagingId: PagingId? = null,
userId: UserId? = null
): PagingRepository<AuthorizedRegularUserRepository> =
base.subscribers(token, amount, pagingId, userId).mapItems { user ->
AuthorizedRegularUserRepository(
data = user.data,
api = api
)
}

public fun paging(
chunkSize: Amount,
startPagingId: PagingId? = null,
Expand Down
46 changes: 43 additions & 3 deletions api/src/commonMain/kotlin/app/meetacy/sdk/friends/FriendsApi.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package app.meetacy.sdk.friends

import app.meetacy.sdk.MeetacyApi
import app.meetacy.sdk.engine.requests.AddFriendRequest
import app.meetacy.sdk.engine.requests.DeleteFriendRequest
import app.meetacy.sdk.engine.requests.ListFriendsRequest
import app.meetacy.sdk.engine.requests.*
import app.meetacy.sdk.friends.location.FriendsLocationApi
import app.meetacy.sdk.users.RegularUserRepository
import app.meetacy.sdk.types.amount.Amount
Expand Down Expand Up @@ -44,6 +42,48 @@ public class FriendsApi(private val api: MeetacyApi) {
}
}

public suspend fun subscriptions(
token: Token,
amount: Amount,
pagingId: PagingId? = null,
userId: UserId? = null
): PagingRepository<RegularUserRepository> = PagingRepository(
amount = amount,
startPagingId = pagingId
) { currentAmount, currentPagingId ->
api.engine.execute(
request = GetSubscriptionsRequest(
token = token,
userId = userId,
amount = currentAmount,
pagingId = currentPagingId
)
).paging.mapItems { regularUser ->
RegularUserRepository(regularUser, api)
}
}

public suspend fun subscribers(
token: Token,
amount: Amount,
pagingId: PagingId? = null,
userId: UserId? = null,
): PagingRepository<RegularUserRepository> = PagingRepository(
amount = amount,
startPagingId = pagingId
) { currentAmount, currentPagingId ->
api.engine.execute(
request = GetSubscribersRequest(
token = token,
userId = userId,
amount = currentAmount,
pagingId = currentPagingId
)
).paging.mapItems { regularUser ->
RegularUserRepository(regularUser, api)
}
}

public fun paging(
token: Token,
chunkSize: Amount,
Expand Down

0 comments on commit 3dce318

Please sign in to comment.