Last active
April 29, 2019 09:26
-
-
Save hlung/9b0f439c3b7938e4e75fa0a471da5ec6 to your computer and use it in GitHub Desktop.
Pokemon API client in Kotlin
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
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