From e5ee2f435464ab309da1fdfefbfa8b86d2893fda Mon Sep 17 00:00:00 2001 From: noragami Date: Fri, 26 Feb 2021 00:44:30 +0100 Subject: [PATCH] updated README.md with many other juicy details + some gardening! --- README.md | 9 +++++++++ .../pokeshake/delivery/pokeapi/PokeApiGateway.kt | 13 +++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 83744b1..a97c6d0 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,15 @@ instead of failing badly with a `500` response code. So we can keep the informat I also implemented a `404` response code if the client requests a Pokemon that doesn't exist. +##### :lock: PokeApi 403 Forbidden error +I encountered a problem during the integration of PokeApi. At first, every call returned a `403` error. + +Fun facts: + 1. API Documentation didn't help at all + 2. The same call worked using the browser + +Point 2 made me think that the only difference between the browser call and the console call was the user-agent. +By setting up a dummy user-agent, the problem disappeared. ## :bulb: Improvement areas for production ready product diff --git a/src/main/kotlin/com/pokemon/pokeshake/delivery/pokeapi/PokeApiGateway.kt b/src/main/kotlin/com/pokemon/pokeshake/delivery/pokeapi/PokeApiGateway.kt index 620421f..334d531 100644 --- a/src/main/kotlin/com/pokemon/pokeshake/delivery/pokeapi/PokeApiGateway.kt +++ b/src/main/kotlin/com/pokemon/pokeshake/delivery/pokeapi/PokeApiGateway.kt @@ -14,15 +14,20 @@ class PokeApiGateway(private val restTemplate: RestTemplate, private val endpoin override fun englishDescription(pokemonName: String): PokemonApiResponse { return try { - val response = restTemplate.exchange(getEndpoint(endpoint, pokemonName), GET, HttpEntity(headers()), Species::class.java) - PokemonApiResponse.Success(getDescription(response.body)) + val response = restTemplate.exchange( + getEndpoint(endpoint, pokemonName), + GET, + HttpEntity(headers()), + Species::class.java + ) + PokemonApiResponse.Success(getDescription(response.body!!)) } catch (ex: RestClientException) { PokemonApiResponse.Failure(ex.localizedMessage) } } - private fun getDescription(body: Species?): String = - body?.entries?.first { it.language.name == "en" }?.text!!.removeUnwantedChars() + private fun getDescription(body: Species): String = + body.entries.first { it.language.name == "en" }.text.removeUnwantedChars() private fun String.removeUnwantedChars() = replace("\\s+".toRegex(), " ")