Created
March 29, 2016 18:49
-
-
Save marekzebrowski/2d04b04d5e9e1e6c36abab0a7ef1d553 to your computer and use it in GitHub Desktop.
simplifications of http-client
This file contains 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
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