Skip to content

Instantly share code, notes, and snippets.

@suni-masuno
Created September 9, 2019 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save suni-masuno/b148ec3aa95079f78f558e63e32cfcd5 to your computer and use it in GitHub Desktop.
Save suni-masuno/b148ec3aa95079f78f558e63e32cfcd5 to your computer and use it in GitHub Desktop.
Class SomeClass(){
private def get(url: String): Future[String] = {
for {
serviceResponse <- sendGetRequest(url)
} yield checkStatusCode(url, serviceResponse)
}
private def checkStatusCode(
url: String,
response: HttpResponse[String]
): String = {
if (response.is2xx) response.body
else
throw new RuntimeException(
s"$url is responding with a non 200 series status code."
)
}
private def sendGetRequest(url: String): Future[HttpResponse[String]] =
Future {
Http(url)
.headers(criticalHeaders)
.asString
}.recoverWith {
case NonFatal(_) =>
throw new RuntimeException(s"$url is failing to respond.")
}
private def criticalHeaders = Seq(
"x-http-request-id" -> Context.getRequestContext.getRequestId,
"x-http-caller-id" -> Context.getRequestContext.getCallerId,
"Accept" -> "application/json"
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment