Skip to content

Commit

Permalink
Add pokemon client
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedRejeb committed Feb 25, 2023
1 parent f79acc2 commit 8670313
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mocoding.pokedex.core.di

import com.mocoding.pokedex.core.database.databaseModule
import com.mocoding.pokedex.core.network.networkModule
import com.mocoding.pokedex.core.network.di.networkModule
import org.koin.core.context.startKoin
import org.koin.dsl.KoinAppDeclaration

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mocoding.pokedex.core.network

object NetworkConstants {
const val baseUrl = "https://pokeapi.co/api/v2/"

object Pokemon {
const val route = baseUrl + "pokemon"
val byName: (String) -> String = { name -> "$route/$name"}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.mocoding.pokedex.core.network.client

import com.mocoding.pokedex.core.network.NetworkConstants
import com.mocoding.pokedex.core.network.helper.handleErrors
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.http.ContentType
import io.ktor.http.contentType
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class PokemonClient: KoinComponent {

private val httpClient by inject<HttpClient>()

suspend fun getPokemonList(
limit: Int = 20,
offset: Int = 0
): String {
return handleErrors {
httpClient.get(NetworkConstants.Pokemon.route) {
url {
parameters.append("limit", limit.toString())
parameters.append("offset", offset.toString())
}
contentType(ContentType.Application.Json)
}
}
}

suspend fun getPokemonByName(
name: String,
): String {
return handleErrors {
httpClient.get(NetworkConstants.Pokemon.byName(name)) {
contentType(ContentType.Application.Json)
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mocoding.pokedex.core.network.di

import com.mocoding.pokedex.core.network.client.PokemonClient
import com.mocoding.pokedex.core.network.createHttpClient
import io.ktor.client.HttpClient
import org.koin.core.module.dsl.singleOf
import org.koin.dsl.module

val networkModule = module {
singleOf<HttpClient> { createHttpClient() }
singleOf<PokemonClient> { PokemonClient() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mocoding.pokedex.core.network.errors

enum class PokedexError {
ServiceUnavailable,
ClientError,
ServerError,
UnknownError
}

class PokedexException(error: PokedexError): Exception(
"Something goes wrong: $error"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.mocoding.pokedex.core.network.helper

import com.mocoding.pokedex.core.network.errors.PokedexError
import com.mocoding.pokedex.core.network.errors.PokedexException
import io.ktor.client.call.body
import io.ktor.client.statement.HttpResponse
import io.ktor.utils.io.errors.IOException

suspend inline fun <reified T> handleErrors(response: () -> HttpResponse): T {
val result = try {
response()
} catch(e: IOException) {
throw PokedexException(PokedexError.ServiceUnavailable)
}

when(result.status.value) {
in 200..299 -> Unit
in 400..499 -> throw PokedexException(PokedexError.ClientError)
500 -> throw PokedexException(PokedexError.ServerError)
else -> throw PokedexException(PokedexError.UnknownError)
}

return try {
result.body()
} catch(e: Exception) {
throw PokedexException(PokedexError.ServerError)
}
}

0 comments on commit 8670313

Please sign in to comment.