Make calls to the Canvas API directly from the client is made simple by a proxy combined with client side javascript that is setup for all Canvas API endpoints. The definitions for the Canvas API endpoints can be found in client/js/libs/canvas/. Each file in that directory corresponds to a Canvas API endpoint and includes links to the relevant documentation and examples.
"use strict";
import React from "react";
import { connect } from "react-redux";
import canvasRequest from "libs/canvas/action";
import { list_accounts } from "libs/canvas/constants/accounts";
const select = (state) => {
const accounts = state.accounts;
return {
accounts
}
};
class Accounts extends React.Component {
componentWillMount(){
const params = {};
const body = {};
this.props.canvasRequest(list_accounts, params, body);
}
render(){
return<div>
{this.props.accounts}
</div>;
}
}
export default connect(select, { canvasRequest })(Accounts);
In the example above we need only import the canvasRequest action. ll Canvas API requests use this one action to make requests.
Only Canvas API endpoints that are whitelisted with the application are authorized. All other calls will result in an 'unauthorized' being returned from the server. Add API calls to the 'canvas_api_permissions' attribute of the application as a comma separated list. ie
my_applications.canvas_api_permissions = "LIST_ACCOUNTS,GET_SINGLE_ACCOUNT"
Definitions for the API endpoints can be found in libs/canvas_urls.rb.
The developer is responsible for adding to this list each API endpoint they wish to call.
The Canvas API code is autogenerated by running:
rake canvas:api
The code is found in canvas_api.rake and uses the json documentation for the Canvas API provided by Instructure to build all everything needed to interact with the API.