Library.Canvas (Portico/Z v0.1.0)

Context for accessing Canvas/Instructure API functions.

Requires config.exs to have a setup incantation like so:

config :library, 
  Library.Canvas: [
    api_token: "yer token"
  ]

Schema Diagram

schema diagram | dot | pdf | png | svg

Link to this section Summary

Link to this section Functions

Link to this function

api_attributes(user, atom, attrs \\ %{})

Convert a Library.Canvas.User into attributes suitable for Canvas user APIs.

If attrs are passed, they will override the defaults. Note that if you override the default for eg the :user key, you'll need to override all child keys.

See https://canvas.instructure.com/doc/api/users.html#method.users.create for details about the request structure.

Link to this function

create_user(account_id \\ "self", attrs \\ %{})

Create a user in Canvas.

account_id must be either "self" (default) or the ID from a Library.Canvas.Account.

The user attrs will be passed through the User.create_struct/1 validation pipeline. Clearly, the user attributes must be valid to be sent along to Canvas.

At a minimum, you'll want to send something like:

%{name: "some name", login_id: "some@email.org", sis_user_id: "portico_contact_id"}

Returns {:ok, %Library.Canvas.User} on success.

Returns {:error, error_description} on error.

Link to this function

list_accounts(opts \\ [])

List Canvas accounts.

See https://canvas.instructure.com/doc/api/accounts.html for more information.

opts are passed directly to the API. Current options are:

  • all (boolean - default false) - include sub-accounts as well
  • include - array of additional information to include.
Link to this function

list_logins(user)

List Canvas logins for a Library.Canvas.User.

See https://canvas.instructure.com/doc/api/logins.html#method.pseudonyms.index for more information

Link to this function

list_sub_accounts()

List Canvas sub-accounts.

See https://canvas.instructure.com/doc/api/accounts.html for more information

Link to this function

list_users(account_id \\ "self", options \\ [])

List all users on Canvas.

See https://canvas.instructure.com/doc/api/users.html#method.users.api_index for valid query options. Some notable options include:

  • enrollment_type - "student", "teacher", etc.
  • order - Sort order "asc" or "desc".
  • per_page - Number of people per API request response. Defaults to 10, though results are automatically concatenated.
  • search_term - Name or ID of user
  • sort - Column to sort by, "username", "email", "sis_id", "last_login"

It's recommended you use the per_page: 100 option, at the very least.

Link to this function

search_accounts(keywords \\ [])

Search among all Canvas accounts

Accepts a Keyword list of fields and values to match.

Typically this is used for filtering by SIS account ID eg:

iex> [account] = Library.Canvas.search_accounts(sis_account_id: "JUN")
iex> account.name
"EXPLO at Wheaton"
Link to this function

search_for_user(email, account_id \\ "self")

Find an individual user on Canvas.

It's recommended to search by sis_id (eg. Portico contactID), or email address.

Returns nil if none were found, or more than one user was found (ambiguous search), or there was some sort of data-casting error with the returned data.

Examples

iex> user = Library.Canvas.search_for_user("dmerand@explo.org")
iex> %Library.Canvas.User{id: 3} = user

iex> nil = Library.Canvas.search_for_user("notreal@explo.org")
Link to this function

search_users(query, account_id \\ "self", options \\ [])

Search all users on Canvas.

Shorthand for list_users/1. If you like, you can pass the same options through, eg. for sorting.

Examples

iex> search = Library.Canvas.search_users("dmerand@explo.org")
iex> [%Library.Canvas.User{name: "Donald L. Merand"}] = search
Link to this function

update_account(id, attrs \\ %{})

Update a Library.Canvas.Account.

See https://canvas.instructure.com/doc/api/accounts.html#method.accounts.update for more details about options.

Link to this function

update_login(user, account_id \\ "self", attrs \\ %{})

Update a Library.Canvas.Login.

Errors out if the user has more than one login for the account. That is weird, what is wrong with you?

See https://canvas.instructure.com/doc/api/logins.html#method.pseudonyms.update for more details about updating logins.

Link to this function

update_sis_user_id(user, account_id \\ "self", sis_user_id)

Set the sis_user_id for a given Library.Canvas.Login in Canvas.

See update_login/3 for more details about updating logins.

Link to this function

update_user(user, attrs \\ %{})

Update a given Library.Canvas.User in Canvas with attrs.

See https://canvas.instructure.com/doc/api/users.html#method.users.update for details about which keys can be passed.