Skip to content

Instantly share code, notes, and snippets.

@marekzebrowski
Created March 29, 2016 18:49
Show Gist options
  • Save marekzebrowski/2d04b04d5e9e1e6c36abab0a7ef1d553 to your computer and use it in GitHub Desktop.
Save marekzebrowski/2d04b04d5e9e1e6c36abab0a7ef1d553 to your computer and use it in GitHub Desktop.
simplifications of http-client
trait HttpClient {
implicit def system: ActorSystem
implicit def materializer: ActorMaterializer
def connectionContext: HttpsConnectionContext
protected val http = Http()
//execs only if result was successful
def exec[T, U](r: HttpRequest, f: U => T)(implicit u: Unmarshaller[HttpResponse, U], ec: ExecutionContext): Future[T] =
http.singleRequest(r).flatMap(u.apply(_).map(f))
//execs only if result was successful
def execOK[T, U](r: HttpRequest, f: U => T)(implicit u: Unmarshaller[HttpResponse, U], ec: ExecutionContext): Future[T] =
http.singleRequest(r).flatMap { rsp =>
if (rsp.status.isSuccess()) u.apply(rsp).map(f)
else Unmarshal(rsp.entity).to[String].flatMap(str => Future.failed[T](SgHttpError(rsp.status.intValue(), str)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment