Skip to content

Commit

Permalink
Merge pull request #51 from andacata/bugfix/50_throw-unusable-api-exc…
Browse files Browse the repository at this point in the history
…eption-on-unavailable-client

fix(credentials): #50 throw unusable api exception on unavailable client
  • Loading branch information
lilgallon authored Dec 28, 2023
2 parents 5ba2569 + 81dc58d commit 6f0acea
Showing 1 changed file with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.izivia.ocpi.toolkit.modules.versions.domain.VersionNumber
import com.izivia.ocpi.toolkit.transport.TransportClientBuilder
import com.izivia.ocpi.toolkit.transport.domain.HttpMethod
import com.izivia.ocpi.toolkit.transport.domain.HttpRequest
import com.izivia.ocpi.toolkit.transport.domain.HttpStatus

class CredentialsServerService(
private val partnerRepository: PartnerRepository,
Expand Down Expand Up @@ -134,10 +135,27 @@ class CredentialsServerService(
.authenticate(token = credentials.token)
)
}
.parseBody<OcpiResponseBody<List<Version>>>()
.also {
if (it.status != HttpStatus.OK) {
throw OcpiServerUnusableApiException(
"Could not get version of sender, there was an error in the response code: " +
"URL='${credentials.url}', HttpStatus=${it.status.code}"
)
}
}
.runCatching {
this.parseBody<OcpiResponseBody<List<Version>>>()
}
.onFailure {
throw OcpiServerUnusableApiException(
"Could not get versions of sender, there was an error parsing the response: " +
"URL='${credentials.url}', error='${it.message}'"
)
}
.getOrThrow()
.let {
it.data
?: throw OcpiServerGenericException(
?: throw OcpiServerUnusableApiException(
"Could not get versions of sender, there was an error during the call: '${it.status_message}'"
)
}
Expand All @@ -159,10 +177,27 @@ class CredentialsServerService(
.authenticate(token = credentials.token)
)
}
.parseBody<OcpiResponseBody<VersionDetails>>()
.also {
if (it.status != HttpStatus.OK) {
throw OcpiServerUnusableApiException(
"Could not get version of sender, there was an error in the response code: " +
"URL='${matchingVersion.url}', HttpStatus=${it.status.code}"
)
}
}
.runCatching {
this.parseBody<OcpiResponseBody<VersionDetails>>()
}
.onFailure {
throw OcpiServerUnusableApiException(
"Could not get version of sender, there was an error parsing the response: " +
"URL='${matchingVersion.url}', error='${it.message}'"
)
}
.getOrThrow()
.let {
it.data
?: throw OcpiServerGenericException(
?: throw OcpiServerUnusableApiException(
"Could not get version of sender, there was an error during the call: '${it.status_message}'"
)
}
Expand Down

0 comments on commit 6f0acea

Please sign in to comment.