Skip to content

Write new package function to get all users #35

Description

@tin900

write this function, also optional arguments:

get_users <- function(canvas, account_id = "self", per_page = 100, search_term = NULL, include) {
  all_users <- data.frame()
  page <- 1
  
  repeat {
    base <- paste0(canvas$base_url, "/api/v1/accounts/", account_id, "/users?per_page=", per_page, "&page=", page)
    if (!is.null(search_term)) {
      base <- paste0(base, "&search_term=", URLencode(search_term, reserved = TRUE))
    }
    
    if (!is.null(include)) {
      base <- paste0(base, "&include[]=", paste(include, collapse = "&include[]="))
    }
    
    response <- httr::GET(base, httr::add_headers(Authorization = paste("Bearer", canvas$api_key)))
    
    if (httr::status_code(response) != 200) {
      stop("Failed to retrieve users. Please check authentication and API endpoint.")
    }
    
    users <- httr::content(response, "text", encoding = "UTF-8") %>%
      jsonlite::fromJSON(flatten = TRUE)
    
    all_users <- dplyr::bind_rows(all_users, users)
    message("Fetched ", nrow(users), " users from page ", page, "\n")
    
    if (length(users) == 0) {
      break
    }

    
    
    page <- page + 1
  }
  
  return(all_users)
}

documentation:

Request Parameters:

Parameter   Type Description
search_term   string The partial name or full ID of the users to match and return in the results list. Must be at least 3 characters. Note that the API will prefer matching on canonical user ID if the ID has a numeric form. It will only search against other fields if non-numeric in form, or if the numeric value doesn’t yield any matches. Queries by administrative users will search on SIS ID, Integration ID, login ID, name, or email address
enrollment_type   string When set, only return users enrolled with the specified course-level base role. This can be a base role type of ‘student’, ‘teacher’, ‘ta’, ‘observer’, or ‘designer’.
sort   string The column to sort results by. For efficiency, use id if you intend to retrieve many pages of results. In the future, other sort options may be rate-limited after 50 pages. Allowed values: username, email, sis_id, integration_id, last_login, id
order   string The order to sort the given column by. Allowed values: asc, desc
include_deleted_users   boolean When set to true and used with an account context, returns users who have deleted pseudonyms for the context
uuids   Array When set, only return users with the specified UUIDs. UUIDs after the first 100 are ignored

NOTE: we can also use the include to include all possible objects of the user object:


An User object looks like:

// A Canvas user, e.g. a student, teacher, administrator, observer, etc.
{
  // The ID of the user.
  "id": 2,
  // The name of the user.
  "name": "Sheldon Cooper",
  // The name of the user that is should be used for sorting groups of users, such
  // as in the gradebook.
  "sortable_name": "Cooper, Sheldon",
  // The last name of the user.
  "last_name": "Cooper",
  // The first name of the user.
  "first_name": "Sheldon",
  // A short name the user has selected, for use in conversations or other less
  // formal places through the site.
  "short_name": "Shelly",
  // The SIS ID associated with the user.  This field is only included if the user
  // came from a SIS import and has permissions to view SIS information.
  "sis_user_id": "SHEL93921",
  // The id of the SIS import.  This field is only included if the user came from
  // a SIS import and has permissions to manage SIS information.
  "sis_import_id": 18,
  // The integration_id associated with the user.  This field is only included if
  // the user came from a SIS import and has permissions to view SIS information.
  "integration_id": "ABC59802",
  // The unique login id for the user.  This is what the user uses to log in to
  // Canvas.
  "login_id": "sheldon@caltech.example.com",
  // If avatars are enabled, this field will be included and contain a url to
  // retrieve the user's avatar.
  "avatar_url": "https://en.gravatar.com/avatar/d8cb8c8cd40ddf0cd05241443a591868?s=80&r=g",
  // Optional: If avatars are enabled and caller is admin, this field can be
  // requested and will contain the current state of the user's avatar.
  "avatar_state": "approved",
  // Optional: This field can be requested with certain API calls, and will return
  // a list of the users active enrollments. See the List enrollments API for more
  // details about the format of these records.
  "enrollments": null,
  // Optional: This field can be requested with certain API calls, and will return
  // the users primary email address.
  "email": "sheldon@caltech.example.com",
  // Optional: This field can be requested with certain API calls, and will return
  // the users locale in RFC 5646 format.
  "locale": "tlh",
  // Optional: This field is only returned in certain API calls, and will return a
  // timestamp representing the last time the user logged in to canvas.
  "last_login": "2012-05-30T17:45:25Z",
  // Optional: This field is only returned in certain API calls, and will return
  // the IANA time zone name of the user's preferred timezone.
  "time_zone": "America/Denver",
  // Optional: The user's bio.
  "bio": "I like the Muppets.",
  // Optional: This field is only returned if pronouns are enabled, and will
  // return the pronouns of the user.
  "pronouns": "he/him"
}


Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions