diff options
author | Adam Warski <adam@warski.org> | 2017-08-04 16:48:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-04 16:48:22 +0200 |
commit | 951a975ae80427b3b385a24c9e7a22330868caef (patch) | |
tree | c3fda860423c4154bdc73f26f67fe332ce4b1cc6 /akka-http-handler | |
parent | 489e591672257d19b3a07198d4d6c9e21be601c7 (diff) | |
parent | e6f0ac0289ad3685e2af5dfc17ee79c3c1170bdf (diff) | |
download | sttp-951a975ae80427b3b385a24c9e7a22330868caef.tar.gz sttp-951a975ae80427b3b385a24c9e7a22330868caef.tar.bz2 sttp-951a975ae80427b3b385a24c9e7a22330868caef.zip |
Merge pull request #16 from softwaremill/responseaspath
Save response as File/Path
Diffstat (limited to 'akka-http-handler')
-rw-r--r-- | akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala b/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala index d4ca3d8..0a2a467 100644 --- a/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala +++ b/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala @@ -1,6 +1,6 @@ package com.softwaremill.sttp.akkahttp -import java.io.UnsupportedEncodingException +import java.io.{File, IOException, UnsupportedEncodingException} import akka.actor.ActorSystem import akka.http.scaladsl.Http @@ -10,7 +10,7 @@ import akka.http.scaladsl.model._ import akka.http.scaladsl.model.headers.{HttpEncodings, `Content-Type`} import akka.http.scaladsl.model.ContentTypes.`application/octet-stream` import akka.stream.ActorMaterializer -import akka.stream.scaladsl.{Source, StreamConverters} +import akka.stream.scaladsl.{FileIO, Source, StreamConverters} import akka.util.ByteString import com.softwaremill.sttp._ import com.softwaremill.sttp.model._ @@ -67,6 +67,18 @@ class AkkaHttpSttpHandler private (actorSystem: ActorSystem, .runFold(ByteString(""))(_ ++ _) .map(_.toArray[Byte]) + def saved(file: File, overwrite: Boolean) = { + if (!file.exists()) { + file.getParentFile.mkdirs() + file.createNewFile() + } else if (!overwrite) { + throw new IOException( + s"File ${file.getAbsolutePath} exists - overwriting prohibited") + } + + hr.entity.dataBytes.runWith(FileIO.toPath(file.toPath)) + } + rr match { case MappedResponseAs(raw, g) => bodyFromAkka(raw, hr).map(g) @@ -82,6 +94,9 @@ class AkkaHttpSttpHandler private (actorSystem: ActorSystem, case r @ ResponseAsStream() => Future.successful(r.responseIsStream(hr.entity.dataBytes)) + + case ResponseAsFile(file, overwrite) => + saved(file, overwrite).map(_ => file) } } |