Skip to content

Instantly share code, notes, and snippets.

@nafg
Forked from suni-masuno/some.scala
Last active September 9, 2019 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nafg/e2264fcea68ef9a859227df166a7925f to your computer and use it in GitHub Desktop.
Save nafg/e2264fcea68ef9a859227df166a7925f to your computer and use it in GitHub Desktop.
class SomeClass {
private def get(url: String): Future[Option[String]] =
for {
serviceResponse <- sendGetRequest(url)
} yield maybeBody(url, serviceResponse)
private def maybeBody(url: String, response: HttpResponse[String]): String =
if (response.is2xx) Some(response.body)
else None
private def sendGetRequest(url: String): Future[HttpResponse[String]] =
Future {
Http(url).headers(criticalHeaders).asString
}
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