-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce unary callable in http common (#822)
Showing
5 changed files
with
72 additions
and
48 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
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
42 changes: 0 additions & 42 deletions
42
events-app/src/commonMain/kotlin/io/ashdavies/playground/events/EventsService.kt
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
events-app/src/commonMain/kotlin/io/ashdavies/playground/events/GetEventsCallable.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,61 @@ | ||
package io.ashdavies.playground.events | ||
|
||
import io.ashdavies.http.DefaultHttpClient | ||
import io.ashdavies.http.UnaryCallable | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.call.body | ||
import io.ktor.client.plugins.ClientRequestException | ||
import io.ktor.client.plugins.DefaultRequest | ||
import io.ktor.client.plugins.HttpCallValidator | ||
import io.ktor.client.request.get | ||
import kotlinx.serialization.Serializable | ||
import io.ashdavies.http.common.models.Event as ApiEvent | ||
|
||
private const val NETWORK_PAGE_SIZE = 100 | ||
private const val PLAYGROUND_API_HOST = "playground.ashdavies.dev" | ||
|
||
@Serializable | ||
internal data class GetEventsRequest( | ||
val startAt: String? = null, | ||
val limit: Int = NETWORK_PAGE_SIZE, | ||
) | ||
|
||
internal typealias GetEventsResponse = List<ApiEvent> | ||
|
||
internal class GetEventsCallable( | ||
httpClient: HttpClient = DefaultHttpClient(), | ||
) : UnaryCallable<GetEventsRequest, GetEventsResponse> { | ||
|
||
private val httpClient = httpClient.config { | ||
install(DefaultRequest) { | ||
host = PLAYGROUND_API_HOST | ||
} | ||
|
||
install(HttpCallValidator) { | ||
handleResponseExceptionWithRequest { exception, _ -> | ||
if (exception is ClientRequestException) { | ||
throw exception.response.body<GetEventsError>() | ||
} | ||
} | ||
} | ||
|
||
expectSuccess = true | ||
} | ||
|
||
override suspend fun invoke(request: GetEventsRequest): GetEventsResponse { | ||
val queryAsString = buildList { | ||
if (request.startAt != null) add("startAt=${request.startAt}") | ||
add("limit=${request.limit}") | ||
}.joinToString("&") | ||
|
||
return httpClient | ||
.get("events?$queryAsString") | ||
.body() | ||
} | ||
} | ||
|
||
@Serializable | ||
public data class GetEventsError( | ||
override val message: String, | ||
val code: Int, | ||
) : Throwable() |
5 changes: 5 additions & 0 deletions
5
http-common/src/commonMain/kotlin/io/ashdavies/http/UnaryCallable.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,5 @@ | ||
package io.ashdavies.http | ||
|
||
public fun interface UnaryCallable<Request, Response> { | ||
public suspend operator fun invoke(request: Request): Response | ||
} |