View Source NervesHubLink.Downloader (nerves_hub_link v2.6.0)

Handles downloading files via HTTP. internally caches several interesting properties about the download such as:

  • the URI of the request
  • the total content amounts of bytes of the file being downloaded
  • the total amount of bytes downloaded at any given time

Using this information, it can restart a download using the Range HTTP header.

This process's only focus is obtaining data reliably. It doesn't have any side effects on the system.

Summary

Functions

Returns a specification to start this module under a supervisor.

Begins downloading a file at url handled by fun.

Types

event_handler_fun()

@type event_handler_fun() :: (handler_event() -> any())

handler_event()

@type handler_event() :: {:data, binary()} | {:error, any()} | :complete

initialized_download()

@type initialized_download() :: %NervesHubLink.Downloader{
  conn: Mint.HTTP.t(),
  content_length: non_neg_integer(),
  downloaded_length: non_neg_integer(),
  handler_fun: event_handler_fun(),
  max_timeout: term(),
  request_ref: reference(),
  response_headers: Mint.Types.headers(),
  retry_args: term(),
  retry_number: non_neg_integer(),
  retry_timeout: term(),
  status: nil | Mint.Types.status(),
  uri: URI.t(),
  worst_case_timeout: term(),
  worst_case_timeout_remaining_ms: term()
}

resume_rescheduled()

@type resume_rescheduled() :: t()

retry_args()

@type retry_args() :: NervesHubLink.Downloader.RetryConfig.t()

t()

@type t() :: %NervesHubLink.Downloader{
  conn: nil | Mint.HTTP.t(),
  content_length: non_neg_integer(),
  downloaded_length: non_neg_integer(),
  handler_fun: event_handler_fun(),
  max_timeout: timer(),
  request_ref: nil | reference(),
  response_headers: Mint.Types.headers(),
  retry_args: retry_args(),
  retry_number: non_neg_integer(),
  retry_timeout: nil | timer(),
  status: nil | Mint.Types.status(),
  uri: nil | URI.t(),
  worst_case_timeout: nil | timer(),
  worst_case_timeout_remaining_ms: nil | non_neg_integer()
}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

start_download(url, fun)

@spec start_download(String.t() | URI.t(), event_handler_fun()) ::
  GenServer.on_start()

Begins downloading a file at url handled by fun.

Example

  iex> pid = self()
  #PID<0.110.0>
  iex> fun = fn {:data, data} -> File.write("index.html", data)
  ...> {:error, error} -> IO.puts("error streaming file: #{inspect(error)}")
  ...> :complete -> send pid, :complete
  ...> end
  #Function<44.97283095/1 in :erl_eval.expr/5>
  iex> NervesHubLink.Downloader.start_download("https://httpbin.com/", fun)
  {:ok, #PID<0.111.0>}
  iex> flush()
  :complete

start_download(url, fun, retry_args)