View Source AnkiConnect.Actions.Miscellaneous (anki_connect v0.1.1)

Miscellaneous actions.

All functions are delegated inside AnkiConnect module, so you should import them from there.

Link to this section Summary

Functions

Gets information about the AnkiConnect APIs available.

Exports a given deck in .apkg format.

Retrieve the list of profiles.

Imports a file in .apkg format into the collection.

Selects the profile specified in request.

Performs multiple actions in one request, returning an array with the response of each action (in the given order).

Tells anki to reload all data from the database.

Requests permission to use the API exposed by AnkiConnect.

Synchronizes the local Anki collections with AnkiWeb.

Gets the version of the API exposed by AnkiConnect.

Link to this section Functions

@spec api_reflect(%{scopes: [String.t()], actions: [String.t()] | nil}) ::
  {:ok, %{scopes: [String.t()], actions: [String.t()]}} | {:error, any()}

Gets information about the AnkiConnect APIs available.

The request supports the following params:

  • scopes - An array of scopes to get reflection information about. The only currently supported value is "actions".
  • actions - Either nil or an array of API method names to check for. If the value is nil, the result will list all of the available API actions. If the value is an array of strings, the result will only contain actions which were in this array.

The result will contain a list of which scopes were used and a value for each scope. For example, the "actions" scope will contain a "actions" property which contains a list of supported action names.

sample-param

Sample param:

%{
  scopes: ["actions", "invalidType"],
  actions: ["apiReflect", "invalidMethod"]
}

sample-result

Sample result:

{:ok, %{
  "scopes" => ["actions"],
  "actions" => ["apiReflect"]
}}
@spec export_package(%{
  deck: String.t(),
  path: String.t(),
  include_sched: boolean() | nil
}) ::
  {:ok, nil} | {:error, any()}

Exports a given deck in .apkg format.

The optional property includeSched (default is false) can be specified to include the cards’ scheduling data.

sample-param

Sample param:

%{
  deck: "Default",
  path: "/data/Deck.apkg",
  include_sched: true
}

sample-result

Sample result:

{:ok, nil}
@spec get_profiles() :: {:ok, [String.t()]} | {:error, any()}

Retrieve the list of profiles.

sample-result

Sample result:

{:ok, ["Default"]}
@spec import_package(%{path: String.t()}) :: {:ok, nil} | {:error, any()}

Imports a file in .apkg format into the collection.

Note that the file path is relative to Anki’s collection.media folder, not to the client.

sample-param

Sample param:

%{
  path: "/data/Deck.apkg"
}

sample-result

Sample result:

{:ok, nil}
@spec load_profile(%{name: String.t()}) :: {:ok, nil} | {:error, any()}

Selects the profile specified in request.

sample-param

Sample param:

%{
  name: "user1"
}

sample-result

Sample result:

{:ok, nil}
@spec multi(%{actions: [map()]}) :: {:ok, [any()]} | {:error, any()}

Performs multiple actions in one request, returning an array with the response of each action (in the given order).

Warning: returned values will vary from returned values of the same actions called separately. That's becasue Elixir-AnkiConnect maps returned value to {:ok, result} or {:error, reason} and is more strict when checking for errors. If you wish to get the same result as calling actions separately, you have to investigate the sources of distinct functions manually. Also, the default version is 6 for all actions.

sample-param

Sample param:

%{
  actions: [
    %{
      action: "deckNames"
    },
    %{
      action: "deckNames",
      version: 6
    },
    %{
      action: "invalidAction",
      params: %{
        useless: "param"
      }
    },
    %{
      action: "invalidAction",
      params: %{
        useless: "param"
      },
      version: 6
    }
  ]
}

sample-result

Sample result:

{:ok, [
  ["Default"],
  %{"result" => ["Default"], "error" => nil},
  %{"result" => nil, "error" => "unsupported action"},
  %{"result" => nil, "error" => "unsupported action"}
]}
@spec reload_collection() :: {:ok, nil} | {:error, any()}

Tells anki to reload all data from the database.

@spec request_permission() :: {:ok, map()} | {:error, any()}

Requests permission to use the API exposed by AnkiConnect.

This method does not require the API key, and is the only one that accepts requests from any origin; the other methods only accept requests from trusted origins, which are listed under webCorsOriginList in the add-on config. localhost is trusted by default.

Calling this method from an untrusted origin will display a popup in Anki asking the user whether they want to allow your origin to use the API; calls from trusted origins will return the result without displaying the popup. When denying permission, the user may also choose to ignore further permission requests from that origin. These origins end up in the ignoreOriginList, editable via the add-on config.

The result always contains the permission field, which in turn contains either the string granted or denied, corresponding to whether your origin is trusted. If your origin is trusted, the fields requireApiKey (true if required) and version will also be returned.

This should be the first call you make to make sure that your application and Anki-Connect are able to communicate properly with each other. New versions of Anki-Connect are backwards compatible; as long as you are using actions which are available in the reported Anki-Connect version or earlier, everything should work fine.

sample-results

Sample results:

{:ok, %{
  "permission" => "granted",
  "requireApiKey" => false,
  "version" => 6
}}
{:ok, %{
  "permission" => "denied"
}}
@spec sync() :: {:ok, nil} | {:error, any()}

Synchronizes the local Anki collections with AnkiWeb.

@spec version() :: {:ok, integer()} | {:error, any()}

Gets the version of the API exposed by AnkiConnect.

Currently versions 1 through 6 are defined.

sample-result

Sample result:

{:ok, 6}