lib/api/basic/basic.ex

defmodule MyspaceIPFS.Api.Basic do
  @moduledoc """
  MyspaceIPFS.Api is where the main commands of the IPFS API reside.

  TODO:
    - Handle adding of directories.
    - Getting files from IPFS.
  """

  import MyspaceIPFS

  @type result :: MyspaceIPFS.result()
  @type opts :: MyspaceIPFS.opts()
  @type fspath :: MyspaceIPFS.fspath()
  @type path :: MyspaceIPFS.path()

  @doc """
  Add a file to IPFS.

  ## Options
  https://docs.ipfs.tech/reference/kubo/rpc/#api-v0-add

  Opts are of the format
  ```
  [
    quiet: <bool>,
    quieter: <bool>,
    silent: <bool>,
    progress: <bool>,
    trickle: <bool>,
    only-hash: <bool>,
    wrap_with_directory: <bool>,
    chunker: <string>,
    pin: <bool>,
    raw_leaves: <bool>,
    cid_version: <int>,
    hash: <string>,
    inline: <bool>,
    inline_limit: <int>,
    to-files: <string>,
  ]
  ```

  """
  @spec add(fspath, opts) :: result
  def add(fspath, opts \\ []), do: post_file("/add", fspath, opts)

  #TODO: add get for output, archive, compress and compression level
  @doc """
  Get a file or directory from IPFS.
  As it stands ipfs sends a text blob back, so we need to implement a way to
  get the file extracted and saved to disk.

  ## Parameters
  fspath: The filesystem path to the file to add.

  ## Options
  https://docs.ipfs.tech/reference/kubo/rpc/#api-v0-get
  """
  @spec get(path, opts) :: result
  # def get(path, opts \\ []), do: post_query("/get?arg=" <> path, opts)
  def get(_, _ \\ []), do: {:error, "FIXME: Not implemented yet."}

  @doc """
  Get the contents of a file from ipfs.
  Easy way to get the contents of a text file for instance.

  ## Parameters
  path: The IPFS path name or cid of the file to get.

  ## Options
  https://docs.ipfs.tech/reference/kubo/rpc/#api-v0-cat

  ```
  [
    offset: <int>,
    length: <int>,
    timeout: <string>
  ]
  ```
  """
  @spec cat(path, opts) :: result
  def cat(path, opts \\ []), do: post_query("/cat?arg=" <> path, opts)

  @doc """
  List the files in an IPFS object.

  ## Parameters
  path: The IPFS path name or cid of the object to list files from.

  ## Options
  https://docs.ipfs.tech/reference/kubo/rpc/#api-v0-ls

  ### Example
  ```
  [
    headers: <bool>,
    resolve-type: <bool>,
    stream: <bool>,
    timeout: <string>
  ]
  ```
  """
  @spec ls(path, opts) :: result
  def ls(path, opts \\ []), do: post_query("/ls?arg=" <> path, opts)
end