Skip to content

Commit

Permalink
Refactor the interface of Downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
voidcontext committed Mar 15, 2020
1 parent ac2a8bb commit 80b0f8b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 29 deletions.
14 changes: 7 additions & 7 deletions examples/src/main/scala/vdx/fetchfile/examples/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ object Main extends IOApp {

Blocker[IO].use { blocker =>
val out = new ByteArrayOutputStream()
Downloader[IO].fetch(
new URL("http://localhost:8088/100MB.bin"),
Resource.fromAutoCloseable(IO.delay(out)),
blocker,
1024,
Progress.consoleProgress[IO]
).as(ExitCode.Success)
Downloader[IO](blocker, 1024 * 8, Progress.consoleProgress[IO])
.fetch(
new URL("http://localhost:8088/100MB.bin"),
Resource.fromAutoCloseable(IO.delay(out)),

)
.as(ExitCode.Success)
}
}
}
23 changes: 7 additions & 16 deletions fetch-file/src/main/scala/vdx/fetchfile/Downloader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,17 @@ import java.net.URL
import cats.effect.ContextShift

trait Downloader[F[_]] {
def fetch(
url: URL,
out: Resource[F, OutputStream],
ec: Blocker,
chunkSize: Int,
progress: Int => Pipe[F, Byte, Unit] = Progress.noop[F]
)(implicit backend: Backend[F]): F[Unit]
def fetch(url: URL, out: Resource[F, OutputStream]): F[Unit]
}

object Downloader {

def apply[F[_]: Concurrent: ContextShift]: Downloader[F] = new Downloader[F] {
def fetch(
url: URL,
out: Resource[F, OutputStream],
ec: Blocker,
chunkSize: Int,
progress: Int => Pipe[F, Byte, Unit] = Progress.noop
)(implicit backend: Backend[F]): F[Unit] = {
def apply[F[_]: Concurrent: ContextShift](
ec: Blocker,
chunkSize: Int,
progress: Int => Pipe[F, Byte, Unit] = Progress.noop[F]
)(implicit backend: Backend[F]): Downloader[F] = new Downloader[F] {
def fetch(url: URL, out: Resource[F, OutputStream]): F[Unit] =
backend(url).product(out).use {
case ((inStream, contentLength), outStream) =>
readInputStream[F](Sync[F].delay(inStream), chunkSize, ec)
Expand All @@ -37,6 +29,5 @@ object Downloader {
.compile
.drain
}
}
}
}
12 changes: 6 additions & 6 deletions fetch-file/src/test/scala/vdx/fetchfile/DownloaderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,35 @@ class DownloaderSpec extends AnyFlatSpec with Matchers {
case (s, _) => IO.delay(s.close())
}

val downloader = Downloader[IO]


(Blocker[IO].use { blocker =>
val out = new ByteArrayOutputStream()
val downloader = Downloader[IO](
blocker,
1,
)

for {
_ <- downloader.fetch(
new URL("http://example.com/test.file"),
Resource.fromAutoCloseable(IO.delay(out)),
blocker,
1,
)
content <- IO.delay(out.toString)
} yield content
}).unsafeRunSync() should be("http://example.com/test.file")
}

it should "download the file correctly through the HttpURLConnectionBackend" in {
val downloader = Downloader[IO]

val downloadedBytes = (Blocker[IO].use { blocker =>
implicit val backend = HttpURLConnectionBackend[IO]
val downloader = Downloader[IO](blocker, 1024 * 8)
val out = new ByteArrayOutputStream()
for {
_ <- downloader.fetch(
new URL("http://localhost:8088/100MB.bin"),
Resource.fromAutoCloseable(IO.delay(out)),
blocker,
1024 * 64,
)
content <- IO.delay(out.toByteArray())
} yield content
Expand Down

0 comments on commit 80b0f8b

Please sign in to comment.