-
Notifications
You must be signed in to change notification settings - Fork 5
The RequestContext
Every API or utility function call in the SDK requires a RequestContext object as a parameter. The RequestContext is a class that holds the default configuration values used to make Canvas API requests. The two required values for setting up a RequestContext are: 1) an OAuth String token (obtained from a given Canvas instance), and 2) the base API url for the Canvas instance calls are to be made against - this url will typically end in /api. Once the SDK is installed, setting up a RequestContext can be as simple as:
from canvas_sdk import RequestContext
oauth_token = 'secret-token'
base_api_url = 'https://my.canvas.instance/api'
rc = RequestContext(oauth_token, base_api_url)There are a number of optional context parameters for the API requests made using the RequestContext. You can view the full list from master here or better yet use the instructions on the Home page to generate the documentation for the SDK. Below is an example where the timeout value for HTTP requests is set, as well as a default per_page value for API methods that return a list of data:
from canvas_sdk import RequestContext
oauth_token = 'secret-token'
base_api_url = 'https://my.canvas.instance/api'
timeout = 60 # Seconds a request will wait for a response
per_page = 40 # API calls with a "per_page" optional parameter will default to this
rc = RequestContext(oauth_token, base_api_url, timeout=timeout, per_page=per_page)It may be convenient to store a set of configuration values for the RequestContext in a dictionary and use it for initialization. The below example uses a dictionary of SDK default values that is stored in a config module to initialize a context.
# config.py
SDK_DEFAULTS = {
'auth_token': 'my-secret-token',
'base_api_url': 'https://my.canvas.instance/api',
'timeout': 60,
'per_page': 40,
'max_retries': 5,
...
}