Skip to content

Instantly share code, notes, and snippets.

@jeremyrempel
Last active February 18, 2019 16:54
Show Gist options
  • Save jeremyrempel/32cee116b200fc71e0f9264fb51d235a to your computer and use it in GitHub Desktop.
Save jeremyrempel/32cee116b200fc71e0f9264fb51d235a to your computer and use it in GitHub Desktop.
val client = HttpClient { install(ExpectSuccess }
val endPoint = "https://api.unsplash.com"
val client = HttpClient {
install(JsonFeature) {
serializer = KotlinxSerializer(JSON.nonstrict).apply {
setMapper(PhotoResponse::class, PhotoResponse.serializer())
}
}
install(ExpectSuccess)
}
interface PhotoApi {
suspend fun getRandom(): PhotoResponse
}
class PhotoApiService(private val client: HttpClient, private val endPoint: String, private val clientId: String) :
PhotoApi {
companion object {
internal val TAG = PhotoApiService::class.toString()
}
override suspend fun getRandom(): PhotoResponse = client.get {
log(LogLevel.DEBUG, TAG, "Getting random photo from API")
apiUrl("photos/random")
}
private fun HttpRequestBuilder.apiUrl(path: String) {
url {
takeFrom(endPoint)
encodedPath = path
parameters["client_id"] = clientId
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment