Util

canvasapi.util.clean_headers(headers)

Sanitize a dictionary containing HTTP headers of sensitive values.

Parameters:headers (dict) – The headers to sanitize.
Returns:A list of headers without sensitive information stripped out.
Return type:dict
canvasapi.util.combine_kwargs(**kwargs)

Flatten a series of keyword arguments from complex combinations of dictionaries and lists into a list of tuples representing properly-formatted parameters to pass to the Requester object.

Parameters:kwargs (dict) – A dictionary containing keyword arguments to be flattened into properly-formatted parameters.
Returns:A list of tuples that represent flattened kwargs. The first element is a string representing the key. The second element is the value.
Return type:list of tuple
canvasapi.util.file_or_path(file)

Open a file and return the handler if a path is given. If a file handler is given, return it directly.

Parameters:file – A file handler or path to a file.
Returns:A tuple with the open file handler and whether it was a path.
Return type:(file, bool)
canvasapi.util.flatten_kwarg(key, obj)

Recursive call to flatten sections of a kwarg to be combined

Parameters:
  • key (str) – The partial keyword to add to the full keyword
  • obj – The object to translate into a kwarg. If the type is dict, the key parameter will be added to the keyword between square brackets and recursively call this function. If the type is list, or tuple, a set of empty brackets will be appended to the keyword and recursively call this function. Otherwise, the function returns with the final keyword and value.
Returns:

A list of tuples that represent flattened kwargs. The first element is a string representing the key. The second element is the value.

Return type:

list of tuple

canvasapi.util.get_institution_url(base_url)

Clean up a given base URL.

Parameters:base_url (str) – The base URL of the API.
Return type:str
canvasapi.util.is_multivalued(value)

Determine whether the given value should be treated as a sequence of multiple values when used as a request parameter.

In general anything that is iterable is multivalued. For example, list and tuple instances are multivalued. Generators are multivalued, as are the iterable objects returned by zip, itertools.chain, etc. However, a simple int is single-valued. str and bytes are special cases: although these are iterable, we treat each as a single value rather than as a sequence of isolated characters or bytes.

canvasapi.util.normalize_bool(val, param_name)

Normalize boolean-like strings to their corresponding boolean values.

Parameters:
  • val (str or bool) – Value to normalize. Acceptable values: True, “True”, “true”, False, “False”, “false”
  • param_name (str) – Name of the parameter being checked
Return type:

bool

canvasapi.util.obj_or_id(parameter, param_name, object_types)

Accepts either an int (or long or str representation of an integer) or an object. If it is an int, return it. If it is an object and the object is of correct type, return the object’s id. Otherwise, throw an exception.

Parameters:
  • parameter – int, str, long, or object
  • param_name – str
  • object_types – tuple
Return type:

int

canvasapi.util.obj_or_str(obj, attr, object_types)

Accepts an object. If the object has the attribute, return the corresponding string. Otherwise, throw an exception.

Parameters:
  • obj (object) – object from which to retrieve attribute
  • attr (str) – name of the attribute to retrieve
  • object_types (tuple) – tuple containing the types of the object being passed in
Return type:

str