Skip to content

Commit

Permalink
updated README.md with many other juicy details + some gardening!
Browse files Browse the repository at this point in the history
  • Loading branch information
noragami committed Feb 25, 2021
1 parent 575af65 commit e5ee2f4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Species>(headers()), Species::class.java)
PokemonApiResponse.Success(getDescription(response.body))
val response = restTemplate.exchange(
getEndpoint(endpoint, pokemonName),
GET,
HttpEntity<Species>(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(), " ")

Expand Down

0 comments on commit e5ee2f4

Please sign in to comment.