View Source NervesHubLinkCommon.Downloader (nerves_hub_link_common v0.4.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.

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Begins downloading a file at url handled by fun.

Link to this section Types

@type event_handler_fun() :: (handler_event() -> any())
@type handler_event() :: {:data, binary()} | {:error, any()} | :complete
Link to this type

initialized_download()

View Source
@type initialized_download() :: %NervesHubLinkCommon.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()
}
@type resume_rescheduled() :: t()
@type t() :: %NervesHubLinkCommon.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()
}

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

start_download(url, fun)

View Source
@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> NervesHubLinkCommon.Downloader.start_download("https://httpbin.com/", fun)
  {:ok, #PID<0.111.0>}
  iex> flush()
  :complete
Link to this function

start_download(url, fun, retry_args)

View Source