Skip to content

Commit

Permalink
fix(#79-support-search): setBody remove at get requests
Browse files Browse the repository at this point in the history
  • Loading branch information
y9Kap committed Nov 3, 2023
1 parent cf364a5 commit d642bea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ 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.optional.map
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.datetime.DateSerializable
Expand All @@ -22,6 +21,7 @@ import app.meetacy.sdk.types.serializable.meeting.type
import app.meetacy.sdk.types.serializable.optional.OptionalSerializable
import app.meetacy.sdk.types.serializable.optional.serializable
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.UserSerializable
Expand Down Expand Up @@ -65,43 +65,32 @@ internal class MeetingsEngine(
return ListMeetingsHistoryRequest.Response(response)
}

private fun ListActiveMeetingsRequest.toBody() = ListMeetingsPagingBody(
amount.serializable(),
pagingId?.serializable()
)

suspend fun listActiveMeetings(
request: ListActiveMeetingsRequest
): ListActiveMeetingsRequest.Response {
val url = baseUrl / "history" / "active"
val body = request.toBody()

val response = httpClient.get(url.string) {
apiVersion(request.apiVersion)
token(request.token)
setBody(body)
parameter("amount", request.amount)
parameter("pagingId", request.pagingId)
}.bodyAsSuccess<PagingResponseSerializable<MeetingSerializable>>()
.type()
.mapItems { meeting -> meeting.type() }

return ListActiveMeetingsRequest.Response(response)
}

private fun ListPastMeetingsRequest.toBody() = ListMeetingsPagingBody(
amount.serializable(),
pagingId?.serializable()
)

suspend fun listPastMeetings(
request: ListPastMeetingsRequest
): ListPastMeetingsRequest.Response {
val url = baseUrl / "history" / "past"
val body = request.toBody()

val response = httpClient.post(url.string) {
apiVersion(request.apiVersion)
token(request.token)
setBody(body)
parameter("amount", request.amount)
parameter("pagingId", request.pagingId)
}.bodyAsSuccess<PagingResponseSerializable<MeetingSerializable>>()
.type()
.mapItems { meeting -> meeting.type() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,25 @@ 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.SearchRequest
import app.meetacy.sdk.types.serializable.location.LocationSerializable
import app.meetacy.sdk.types.serializable.location.serializable
import app.meetacy.sdk.types.serializable.search.SearchItemSerializable
import app.meetacy.sdk.types.serializable.search.type
import app.meetacy.sdk.types.url.Url
import io.ktor.client.*
import io.ktor.client.request.*
import kotlinx.serialization.Serializable

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

@Serializable
private data class SearchBody(val location: LocationSerializable, val promt: String)
private fun SearchRequest.toBody() = SearchBody(location.serializable(), prompt)

suspend fun search(request: SearchRequest): SearchRequest.Response {
val body = request.toBody()
val response = httpClient.get(baseUrl.string) {
apiVersion(request.apiVersion)
token(request.token)
setBody(body)
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

0 comments on commit d642bea

Please sign in to comment.