Skip to content

Instantly share code, notes, and snippets.

@hlung
Last active April 29, 2019 09:26
Show Gist options
  • Save hlung/9b0f439c3b7938e4e75fa0a471da5ec6 to your computer and use it in GitHub Desktop.
Save hlung/9b0f439c3b7938e4e75fa0a471da5ec6 to your computer and use it in GitHub Desktop.
Pokemon API client in Kotlin
package com.raywenderlich.pokelist
import com.raywenderlich.pokelist.shared.ApplicationDispatcher
import com.raywenderlich.pokelist.shared.Image
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
class PokeApi {
private val httpClient = HttpClient()
fun getPokemonList(success: (List<PokemonEntry>) -> Unit, failure: (Throwable?) -> Unit) {
GlobalScope.launch(ApplicationDispatcher) {
try {
val url = "https://pokeapi.co/api/v2/pokedex/kanto/"
val json = httpClient.get<String>(url)
Json.nonstrict.parse(Pokedex.serializer(), json)
.pokemon_entries
.also(success)
} catch (ex: Exception) {
failure(ex)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment