aboutsummaryrefslogtreecommitdiff
path: root/akka-http-handler/src/main/scala
diff options
context:
space:
mode:
authorPiotr Buda <piotr.buda@softwaremill.com>2017-07-31 11:59:08 +0200
committerPiotr Buda <piotr.buda@softwaremill.com>2017-08-04 16:37:48 +0200
commite6f0ac0289ad3685e2af5dfc17ee79c3c1170bdf (patch)
treec3fda860423c4154bdc73f26f67fe332ce4b1cc6 /akka-http-handler/src/main/scala
parent489e591672257d19b3a07198d4d6c9e21be601c7 (diff)
downloadsttp-e6f0ac0289ad3685e2af5dfc17ee79c3c1170bdf.tar.gz
sttp-e6f0ac0289ad3685e2af5dfc17ee79c3c1170bdf.tar.bz2
sttp-e6f0ac0289ad3685e2af5dfc17ee79c3c1170bdf.zip
#7: Add asFile and asPath responses
Diffstat (limited to 'akka-http-handler/src/main/scala')
-rw-r--r--akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala19
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)
}
}