Skip to content

Commit

Permalink
Merge pull request #94 from meetacy/#93-add-getSubscriptions-and-getS…
Browse files Browse the repository at this point in the history
…ubscribers-methods

#93 add get subscriptions and get subscribers methods
  • Loading branch information
y9san9 authored Mar 16, 2024
2 parents 7b2ab33 + 45c1a03 commit 5aa835f
Show file tree
Hide file tree
Showing 34 changed files with 530 additions and 30 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 ListSubscribersRequest -> friends.subscribers.list(request) as T
is ListSubscriptionsRequest -> friends.subscriptions.list(request) as T
is EmitFriendsLocationRequest -> friends.streamFriendsLocation(request) as T
is PushLocationRequest -> friends.pushLocation(request) as T
// users
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@ package app.meetacy.sdk.engine.ktor.requests.friends

import app.meetacy.sdk.engine.ktor.apiVersion
import app.meetacy.sdk.engine.ktor.handleRSocketExceptions
import app.meetacy.sdk.engine.ktor.requests.friends.subscribers.SubscribersEngine
import app.meetacy.sdk.engine.ktor.requests.friends.subscriptions.SubscriptionsEngine
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.types.annotation.UnsafeConstructor
import app.meetacy.sdk.types.datetime.DateTime
import app.meetacy.sdk.types.location.Location
import app.meetacy.sdk.types.serializable.paging.PagingResponseSerializable
import app.meetacy.sdk.types.serializable.amount.AmountSerializable
import app.meetacy.sdk.types.serializable.amount.serializable
import app.meetacy.sdk.types.serializable.location.LocationSerializable
import app.meetacy.sdk.types.serializable.location.serializable
import app.meetacy.sdk.types.serializable.location.type
import app.meetacy.sdk.types.serializable.paging.PagingIdSerializable
import app.meetacy.sdk.types.serializable.paging.PagingResponseSerializable
import app.meetacy.sdk.types.serializable.paging.serializable
import app.meetacy.sdk.types.serializable.paging.type
import app.meetacy.sdk.types.serializable.user.UserIdSerializable
import app.meetacy.sdk.types.serializable.user.UserSerializable
import app.meetacy.sdk.types.serializable.user.serializable
import app.meetacy.sdk.types.serializable.user.type
import app.meetacy.sdk.types.serializable.user.*
import app.meetacy.sdk.types.url.Url
import app.meetacy.sdk.types.user.RegularUser
import app.meetacy.sdk.types.user.UserLocationSnapshot
Expand All @@ -50,6 +49,16 @@ internal class FriendsEngine(
) {
private val baseUrl = baseUrl / "friends"

val subscribers = SubscribersEngine(
baseUrl = this.baseUrl,
httpClient = httpClient
)

val subscriptions = SubscriptionsEngine(
baseUrl = this.baseUrl,
httpClient = httpClient
)

@Serializable
private data class AddFriendBody(val friendId: UserIdSerializable)
private fun AddFriendRequest.toBody() = AddFriendBody(friendId.serializable())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package app.meetacy.sdk.engine.ktor.requests.friends.subscribers

import app.meetacy.sdk.engine.ktor.apiVersion
import app.meetacy.sdk.engine.ktor.response.bodyAsSuccess
import app.meetacy.sdk.engine.ktor.token
import app.meetacy.sdk.engine.requests.ListSubscribersRequest
import app.meetacy.sdk.types.serializable.paging.PagingResponseSerializable
import app.meetacy.sdk.types.serializable.paging.type
import app.meetacy.sdk.types.serializable.user.UserDetailsSerializable
import app.meetacy.sdk.types.serializable.user.UserSerializable
import app.meetacy.sdk.types.serializable.user.type
import app.meetacy.sdk.types.url.Url
import app.meetacy.sdk.types.user.User
import io.ktor.client.*
import io.ktor.client.request.*

internal class SubscribersEngine(
baseUrl: Url,
private val httpClient: HttpClient
) {
private val baseUrl = baseUrl / "subscribers"

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

val response = httpClient.get(url.string) {
apiVersion(request.apiVersion)
token(request.token)
parameter("userId", request.userId?.string)
parameter("amount", request.amount.int)
parameter("pagingId", request.pagingId?.string)
}.bodyAsSuccess<PagingResponseSerializable<UserSerializable>>()
.type()
.mapItems(UserSerializable::type)

return ListSubscribersRequest.Response(response)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package app.meetacy.sdk.engine.ktor.requests.friends.subscriptions

import app.meetacy.sdk.engine.ktor.apiVersion
import app.meetacy.sdk.engine.ktor.response.bodyAsSuccess
import app.meetacy.sdk.engine.ktor.token
import app.meetacy.sdk.engine.requests.ListSubscriptionsRequest
import app.meetacy.sdk.types.serializable.paging.PagingResponseSerializable
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.request.*

internal class SubscriptionsEngine(
baseUrl: Url,
private val httpClient: HttpClient
) {
private val baseUrl = baseUrl / "subscriptions"

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

val response = httpClient.get(url.string) {
apiVersion(request.apiVersion)
token(request.token)
parameter("userId", request.userId?.string)
parameter("amount", request.amount.int)
parameter("pagingId", request.pagingId?.string)
}.bodyAsSuccess<PagingResponseSerializable<UserSerializable>>()
.type()
.mapItems(UserSerializable::type)

return ListSubscriptionsRequest.Response(response)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ internal class SearchEngine(
val response = httpClient.get(baseUrl.string) {
apiVersion(request.apiVersion)
token(request.token)
parameter("prompt", request.prompt)
parameter("latitude", request.location?.latitude)
parameter("longitude", request.location?.longitude)
parameter("prompt", request.prompt)
}.bodyAsSuccess<List<SearchItemSerializable>>()
return SearchRequest.Response(response.map { it.type() })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import app.meetacy.sdk.types.auth.Token
import app.meetacy.sdk.types.location.Location
import app.meetacy.sdk.updates.AuthorizedUpdatesApi
import app.meetacy.sdk.users.AuthorizedSelfUserDetailsRepository
import app.meetacy.sdk.users.AuthorizedSelfUserRepository
import app.meetacy.sdk.users.AuthorizedUsersApi

/**
Expand Down Expand Up @@ -44,6 +43,6 @@ public class AuthorizedMeetacyApi @UnsafeConstructor constructor(
)

public suspend fun search(location: Location?, prompt: String): List<AuthorizedSearchItemRepository> =
base.search(token, location, prompt).map { AuthorizedSearchItemRepository.of(it.data, api = this) }
base.search(prompt, token, location).map { AuthorizedSearchItemRepository.of(it.data, api = this) }

}
8 changes: 3 additions & 5 deletions api/src/commonMain/kotlin/app/meetacy/sdk/MeetacyApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import app.meetacy.sdk.search.SearchItemRepository
import app.meetacy.sdk.types.annotation.UnsafeConstructor
import app.meetacy.sdk.types.auth.Token
import app.meetacy.sdk.types.location.Location
import app.meetacy.sdk.types.user.SelfUser
import app.meetacy.sdk.types.user.SelfUserDetails
import app.meetacy.sdk.updates.UpdatesApi
import app.meetacy.sdk.users.SelfUserDetailsRepository
import app.meetacy.sdk.users.UsersApi
Expand All @@ -40,13 +38,13 @@ public class MeetacyApi(
}

public suspend fun search(
prompt: String,
token: Token,
location: Location?,
prompt: String
location: Location?
): List<SearchItemRepository> {
return engine.execute(
request = SearchRequest(
token, location, prompt
prompt, token, location
)
).items.map { searchItem ->
SearchItemRepository.of(searchItem, api = this)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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.User
import app.meetacy.sdk.types.user.UserDetails
import app.meetacy.sdk.types.user.UserId

public data class ListSubscribersRequest(
val token: Token,
val amount: Amount,
val pagingId: PagingId?,
val userId: UserId?,
) : MeetacyRequest<ListSubscribersRequest.Response> {
public data class Response(val paging: PagingResponse<User>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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.User
import app.meetacy.sdk.types.user.UserDetails
import app.meetacy.sdk.types.user.UserId

public data class ListSubscriptionsRequest(
val token: Token,
val amount: Amount,
val pagingId: PagingId?,
val userId: UserId?,
) : MeetacyRequest<ListSubscriptionsRequest.Response> {
public data class Response(val paging: PagingResponse<User>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import app.meetacy.sdk.types.location.Location
import app.meetacy.sdk.types.search.SearchItem

public data class SearchRequest(
val prompt: String,
val token: Token,
val location: Location?,
val prompt: String
val location: Location?
) : MeetacyRequest<SearchRequest.Response> {
public data class Response(val items: List<SearchItem>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package app.meetacy.sdk.friends

import app.meetacy.sdk.AuthorizedMeetacyApi
import app.meetacy.sdk.friends.location.AuthorizedFriendsLocationApi
import app.meetacy.sdk.friends.subscribers.AuthorizedSubscribersApi
import app.meetacy.sdk.friends.subscriptions.AuthorizedSubscriptionsApi
import app.meetacy.sdk.types.amount.Amount
import app.meetacy.sdk.types.auth.Token
import app.meetacy.sdk.types.paging.*
import app.meetacy.sdk.types.paging.PagingId
import app.meetacy.sdk.types.paging.PagingRepository
import app.meetacy.sdk.types.paging.PagingSource
import app.meetacy.sdk.types.paging.mapItems
import app.meetacy.sdk.types.user.UserId
import app.meetacy.sdk.users.AuthorizedRegularUserRepository

Expand All @@ -17,6 +22,8 @@ public class AuthorizedFriendsApi(private val api: AuthorizedMeetacyApi) {
public val base: FriendsApi get() = api.base.friends

public val location: AuthorizedFriendsLocationApi = AuthorizedFriendsLocationApi(api)
public val subscribers: AuthorizedSubscribersApi = AuthorizedSubscribersApi(api)
public val subscriptions: AuthorizedSubscriptionsApi = AuthorizedSubscriptionsApi(api)

public suspend fun add(friendId: UserId) {
base.add(token, friendId)
Expand Down
17 changes: 12 additions & 5 deletions api/src/commonMain/kotlin/app/meetacy/sdk/friends/FriendsApi.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
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.friends.subscribers.SubscribersApi
import app.meetacy.sdk.friends.subscriptions.SubscriptionsApi
import app.meetacy.sdk.types.amount.Amount
import app.meetacy.sdk.types.auth.Token
import app.meetacy.sdk.types.paging.*
import app.meetacy.sdk.types.paging.PagingId
import app.meetacy.sdk.types.paging.PagingRepository
import app.meetacy.sdk.types.paging.PagingSource
import app.meetacy.sdk.types.user.UserId
import app.meetacy.sdk.users.RegularUserRepository
import app.meetacy.sdk.users.UserDetailsRepository
import app.meetacy.sdk.users.UserRepository

/**
* When modifying this class, corresponding classes should be altered:
* - [app.meetacy.sdk.users.RegularUserRepository]
* - [app.meetacy.sdk.friends.AuthorizedFriendsApi]
*/
public class FriendsApi(private val api: MeetacyApi) {
public val location: FriendsLocationApi = FriendsLocationApi(api)
public val subscribers: SubscribersApi = SubscribersApi(api)
public val subscriptions: SubscriptionsApi = SubscriptionsApi(api)

public suspend fun add(token: Token, friendId: UserId) {
api.engine.execute(AddFriendRequest(token, friendId))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package app.meetacy.sdk.friends.subscribers

import app.meetacy.sdk.AuthorizedMeetacyApi
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.PagingRepository
import app.meetacy.sdk.types.paging.PagingSource
import app.meetacy.sdk.types.paging.mapItems
import app.meetacy.sdk.types.user.UserId
import app.meetacy.sdk.users.AuthorizedUserRepository

/**
* When modifying this class, corresponding classes should be altered:
* - [app.meetacy.sdk.users.subscribers.AuthorizedSubscribersRepository]
*/
public class AuthorizedSubscribersApi(private val api: AuthorizedMeetacyApi) {
public val token: Token get() = api.token
public val base: SubscribersApi get() = api.base.friends.subscribers

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

public fun paging(
chunkSize: Amount,
startPagingId: PagingId? = null,
limit: Amount? = null,
userId: UserId? = null
): PagingSource<AuthorizedUserRepository> {
return PagingSource(
chunkSize, startPagingId, limit
) { currentAmount, currentPagingId ->
list(currentAmount, currentPagingId, userId).response
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package app.meetacy.sdk.friends.subscribers

import app.meetacy.sdk.MeetacyApi
import app.meetacy.sdk.engine.requests.ListSubscribersRequest
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.PagingRepository
import app.meetacy.sdk.types.paging.PagingSource
import app.meetacy.sdk.types.user.UserId
import app.meetacy.sdk.users.UserRepository

/**
* When modifying this class, corresponding classes should be altered:
* - [app.meetacy.sdk.friends.subscribers.AuthorizedSubscribersApi]
* - [app.meetacy.sdk.users.subscribers.SubscribersRepository]
*/
public class SubscribersApi(private val api: MeetacyApi) {
public suspend fun list(
token: Token,
amount: Amount,
pagingId: PagingId? = null,
userId: UserId? = null,
): PagingRepository<UserRepository> = PagingRepository(
amount = amount,
startPagingId = pagingId
) { currentAmount, currentPagingId ->
api.engine.execute(
request = ListSubscribersRequest(
token = token,
userId = userId,
amount = currentAmount,
pagingId = currentPagingId
)
).paging.mapItems { user ->
UserRepository.of(user, api)
}
}

public fun paging(
token: Token,
chunkSize: Amount,
startPagingId: PagingId? = null,
limit: Amount? = null,
userId: UserId? = null
): PagingSource<UserRepository> {
return PagingSource(
chunkSize, startPagingId, limit
) { currentAmount, currentPagingId ->
list(token, currentAmount, currentPagingId, userId).response
}
}
}
Loading

0 comments on commit 5aa835f

Please sign in to comment.