Created
September 9, 2019 14:13
-
-
Save suni-masuno/b148ec3aa95079f78f558e63e32cfcd5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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