Last active
February 18, 2019 16:54
-
-
Save jeremyrempel/32cee116b200fc71e0f9264fb51d235a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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