### Install Membrane Hackney Plugin Source: https://hexdocs.pm/membrane_hackney_plugin/index Instructions to add the Membrane Hackney plugin to your project's dependencies in `mix.exs` and fetch the dependencies. ```elixir {:membrane_hackney_plugin, "~> 0.11.0"}copy ``` -------------------------------- ### Download File with Membrane Hackney Source Source: https://hexdocs.pm/membrane_hackney_plugin/index An Elixir pipeline example demonstrating how to download a kitten image from imgur using `Membrane.Hackney.Source`. This requires the `:membrane_file_plugin` dependency. ```elixir defmodule DownloadPipeline do use Membrane.Pipeline alias Membrane.{File, Hackney} @impl true def handle_init(_) do children = [ source: %Hackney.Source{ location: "http://i.imgur.com/z4d4kWk.jpg", hackney_opts: [follow_redirect: true] }, sink: %File.Sink{location: "kitty.jpg"} ] links = [link(:source) |> to(:sink)] spec = %ParentSpec{children: children, links: links} {{:ok, spec: spec}, %{}} end end {:ok, pid} = DownloadPipeline.start_link() DownloadPipeline.play(pid)copy ``` -------------------------------- ### Upload File with Membrane Hackney Sink Source: https://hexdocs.pm/membrane_hackney_plugin/index An Elixir pipeline example for uploading a file using `Membrane.Hackney.Sink`. This example requires the Goth library for Google Cloud credentials and `:membrane_file_plugin` dependency. It configures a POST request with authentication headers. ```elixir defmodule UploadPipeline do use Membrane.Pipeline alias Membrane.{File, Hackney} @impl true def handle_init([bucket, name]) do children = [ source: %File.Source{location: "sample.flac"}, sink: %Hackney.Sink{ method: :post, location: build_uri(bucket, name), headers: [auth_header(), {"content-type", "audio/flac"}] } ] links = [link(:source) |> to(:sink)] spec = %ParentSpec{children: children, links: links} {{:ok, spec: spec}, %{}} end @impl true def handle_notification(%Hackney.Sink.Response{} = response, from, _ctx, state) do IO.inspect({from, response}) {:ok, state} end def handle_notification(_notification, _from, _ctx, state) do {:ok, state} end defp auth_header do {:ok, token} = Goth.Token.for_scope("https://www.googleapis.com/auth/devstorage.read_write") {"Authorization", "#{token.type} #{token.token}"} end defp build_uri(bucket, name) do "https://www.googleapis.com/upload/storage/v1/b/#{bucket}/o?" \ <> URI.encode_query(uploadType: "media", name: name) end end {:ok, pid} = UploadPipeline.start_link(["some_bucket", "uploaded_file_name.flac"]) UploadPipeline.play(pid)copy ``` -------------------------------- ### Membrane.Hackney.Source Options Function Source: https://hexdocs.pm/membrane_hackney_plugin/Membrane.Hackney Provides a function 'options()' that returns a keyword list describing the available configuration options for the Membrane.Hackney.Source module. This is useful for understanding or dynamically configuring the source element. ```Elixir @spec options() :: keyword() ``` -------------------------------- ### Membrane.Hackney.Source Options Function Source: https://hexdocs.pm/membrane_hackney_plugin/Membrane.Hackney Provides information about the available options for the `Membrane.Hackney.Source` module. ```APIDOC ## Membrane.Hackney.Source Options Function (`options()`) ### Description Returns a description of the options available for the `Membrane.Hackney.Source` module. ### Function Signature ```elixir @spec options() :: keyword() ``` ``` -------------------------------- ### Membrane.Hackney.Source Pads Source: https://hexdocs.pm/membrane_hackney_plugin/Membrane.Hackney Information about the pads available for the Membrane.Hackney.Source element. ```APIDOC ## Membrane.Hackney.Source Pads ### Description Details about the pads supported by the `Membrane.Hackney.Source` element. ### Pad: `:output` - **Accepted Formats**: `%RemoteStream{type: :bytestream, content_format: nil}` - **Direction**: `:output` - **Availability**: `:always` - **Flow Control**: `:manual` - **Demand Unit**: `nil` ``` -------------------------------- ### Membrane.Hackney.Source Element Options Source: https://hexdocs.pm/membrane_hackney_plugin/Membrane.Hackney This section describes the configuration options for the Membrane.Hackney.Source element, which are passed as a struct. ```APIDOC ## Membrane.Hackney.Source Element Options ### Description Configuration options for the `Membrane.Hackney.Source` element. ### Parameters #### Request Body - **location** (any) - Required - The URL to fetch. - **method** (:get | :post | :put | :patch | :delete | :head | :options) - Optional - The HTTP method to use. Defaults to `:get`. - **body** (any) - Optional - The request body. Defaults to `""`. - **headers** (any) - Optional - Additional request headers in the format accepted by `:hackney.request/5`. Defaults to `[]`. - **hackney_opts** (any) - Optional - Additional options for Hackney in the format accepted by `:hackney.request/5`. Defaults to `[]`. - **max_retries** (non_neg_integer() | :infinity) - Optional - Maximum number of retries before returning an error. Can be set to `:infinity`. Defaults to `0`. - **retry_delay** (any) - Optional - Delay between reconnection attempts in case of connection error. Defaults to `1000000000`. - **is_live** (any) - Optional - Assume the source is live. If true, when resuming after an error, the element will not use the `Range` header to skip to the current position in bytes. Defaults to `false`. ### Response #### Success Response (200) - **type** (t()) - The struct containing options for `Membrane.Hackney.Source`. ### Response Example ```json { "body": "", "hackney_opts": [], "headers": [], "is_live": false, "location": "http://example.com", "max_retries": 0, "method": ":get", "retry_delay": 1000000000 } ``` ``` -------------------------------- ### Download Kitten Image using Hackney Source Source: https://hexdocs.pm/membrane_hackney_plugin/readme This pipeline demonstrates how to download an image from a URL using Membrane.Hackney.Source and save it to a file using Membrane.File.Sink. It requires the ':membrane_file_plugin' dependency. The source is configured with a location and optional Hackney options like following redirects. ```elixir defmodule DownloadPipeline do use Membrane.Pipeline alias Membrane.{File, Hackney} @impl true def handle_init(_) do children = [ source: %Hackney.Source{ location: "http://i.imgur.com/z4d4kWk.jpg", hackney_opts: [follow_redirect: true] }, sink: %File.Sink{location: "kitty.jpg"} ] links = [link(:source) |> to(:sink)] spec = %ParentSpec{children: children, links: links} {{:ok, spec: spec}, %{}} end end {:ok, pid} = DownloadPipeline.start_link() DownloadPipeline.play(pid) ``` -------------------------------- ### Upload File using Hackney Sink Source: https://hexdocs.pm/membrane_hackney_plugin/readme This pipeline shows how to upload a local file to a specified URI using Membrane.Hackney.Sink. It requires the Goth library for authentication with Google Cloud and the ':membrane_file_plugin' dependency. The sink is configured with the HTTP method, location, and necessary headers, including an authorization header. ```elixir defmodule UploadPipeline do use Membrane.Pipeline alias Membrane.{File, Hackney} @impl true def handle_init([bucket, name]) do children = [ source: %File.Source{location: "sample.flac"}, sink: %Hackney.Sink{ method: :post, location: build_uri(bucket, name), headers: [auth_header(), {"content-type", "audio/flac"}] } ] links = [link(:source) |> to(:sink)] spec = %ParentSpec{children: children, links: links} {{:ok, spec: spec}, %{}} end @impl true def handle_notification(%Hackney.Sink.Response{} = response, from, _ctx, state) do IO.inspect({from, response}) {:ok, state} end def handle_notification(_notification, _from, _ctx, state) do {:ok, state} end defp auth_header do {:ok, token} = Goth.Token.for_scope("https://www.googleapis.com/auth/devstorage.read_write") {"Authorization", "#{token.type} #{token.token}"} end defp build_uri(bucket, name) do "https://www.googleapis.com/upload/storage/v1/b/#{bucket}/o?" \ <> URI.encode_query(uploadType: "media", name: name) end end {:ok, pid} = UploadPipeline.start_link(["some_bucket", "uploaded_file_name.flac"]) UploadPipeline.play(pid) ``` -------------------------------- ### Membrane.Hackney.Source Type Definition Source: https://hexdocs.pm/membrane_hackney_plugin/Membrane.Hackney Defines the 't()' type, which represents the configuration struct for Membrane.Hackney.Source. It lists all available options with their expected types and default values. ```Elixir @type t() :: %Membrane.Hackney.Source{ body: any(), hackney_opts: any(), headers: any(), is_live: any(), location: any(), max_retries: non_neg_integer() | :infinity, method: :get | :post | :put | :patch | :delete | :head | :options, retry_delay: any() } ``` -------------------------------- ### Membrane.Hackney.Source Type Source: https://hexdocs.pm/membrane_hackney_plugin/Membrane.Hackney Defines the structure of the `t()` type, representing the options for `Membrane.Hackney.Source`. ```APIDOC ## Membrane.Hackney.Source Type (`t()`) ### Description Defines the struct containing configuration options for the `Membrane.Hackney.Source` element. ### Type Definition ```elixir @type t() :: %Membrane.Hackney.Source{ body: any(), hackney_opts: any(), headers: any(), is_live: any(), location: any(), max_retries: non_neg_integer() | :infinity, method: :get | :post | :put | :patch | :delete | :head | :options, retry_delay: any() } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.