Skip to content

Commit

Permalink
Add some documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
voidcontext committed Mar 25, 2020
1 parent 3154639 commit 232d7f7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
18 changes: 18 additions & 0 deletions fetch-file/src/main/scala/vdx/fetchfile/Downloader.scala
Expand Up @@ -7,12 +7,30 @@ import fs2.io.writeOutputStream
import java.io.OutputStream
import java.net.URL

/**
* Given a url and an OutputStream the `fetch` function populates the stream with the content of the HTTP resource.
* Considering the intended functionality of this interface, the outputsream is usually a FileOutputStream
*
* Example:
* {{{
* fetch(
* new URL("http://example.com/path/to/large.file"),
* Resource.fromAutoClosable(new FileOutputStream(new File("/path/to/destination.file")))
* )
* }}}
*/
trait Downloader[F[_]] {
/**
* Fetches the given URL and popuplates the given output stream.
*/
def fetch(url: URL, out: Resource[F, OutputStream]): F[Unit]
}

object Downloader {

/**
* Creates a `Downloader` that is going to run all blocking operations on the given ExecutionContext.
*/
def apply[F[_]: Concurrent: ContextShift](
ec: Blocker,
progress: Int => Pipe[F, Byte, Unit] = Progress.noop[F]
Expand Down
9 changes: 9 additions & 0 deletions fetch-file/src/main/scala/vdx/fetchfile/Progress.scala
Expand Up @@ -7,8 +7,14 @@ import java.util.concurrent.atomic.AtomicLong
import java.util.Locale

object Progress {
/**
* A progress tracker that does nothing
*/
def noop[F[_]]: Int => Pipe[F, Byte, Unit] = _ => _.map(_ => ())

/**
* A console based progress tracker which needs control character support as it refreshes the status in place.
*/
def consoleProgress[F[_]: Sync](implicit clock: MonotonicClock): Int => Pipe[F, Byte, Unit] =
custom[F] { (downloadedBytes, contentLength, elapsedTime, downloadSpeed) =>
println(
Expand All @@ -19,6 +25,9 @@ object Progress {
)
}

/**
* Helps building custom progress trackers
*/
def custom[F[_]: Sync](
f: (Long, Int, Long, Long) => Unit,
chunkLimit: Option[Int] = None
Expand Down
5 changes: 4 additions & 1 deletion fetch-file/src/main/scala/vdx/fetchfile/package.scala
Expand Up @@ -6,6 +6,9 @@ import fs2.Stream
import java.net.URL

package object fetchfile {

/**
* A HttpBackend is a function from URL to Resource that's a tuple of
* the content length and the content itself as an fs2 Stream
*/
type HttpBackend[F[_]] = URL => Resource[F, (Int, Stream[F, Byte])]
}

0 comments on commit 232d7f7

Please sign in to comment.