-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f79acc2
commit 8670313
Showing
7 changed files
with
104 additions
and
10 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
shared/src/commonMain/kotlin/com/mocoding/pokedex/core/di/AppModule.kt
This file contains 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
10 changes: 10 additions & 0 deletions
10
shared/src/commonMain/kotlin/com/mocoding/pokedex/core/network/NetworkConstants.kt
This file contains 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
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"} | ||
} | ||
} |
9 changes: 0 additions & 9 deletions
9
shared/src/commonMain/kotlin/com/mocoding/pokedex/core/network/NetworkModule.kt
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
shared/src/commonMain/kotlin/com/mocoding/pokedex/core/network/client/PokemonClient.kt
This file contains 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
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) | ||
} | ||
} | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
shared/src/commonMain/kotlin/com/mocoding/pokedex/core/network/di/NetworkModule.kt
This file contains 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
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() } | ||
} |
12 changes: 12 additions & 0 deletions
12
shared/src/commonMain/kotlin/com/mocoding/pokedex/core/network/errors/PokedexException.kt
This file contains 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
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" | ||
) |
28 changes: 28 additions & 0 deletions
28
shared/src/commonMain/kotlin/com/mocoding/pokedex/core/network/helper/ErrorHandler.kt
This file contains 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
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) | ||
} | ||
} |