.NET - Canvas API Implementation

garth
Community Champion
4
11893

What is this about?

If you are a .NET developer and are interested in leveraging the Canvas API to automate tasks at your institution, you might be interested in ths demo project.  I will provide a Visual Studio project that you can download and use as starting point to develop your own tools.

References

I can try to guide you to some reference material for those getting started that will hopefully help.

There are a few basics that would help to understand when working with the associated project:

How does it work?

The associated source code will create a command line tool that accepts a config file.  The config file contains the following information:

  • "accessToken" - your access token
  • "apiUrl" - the url to your Canvas site, I would you Beta or Test
  • "apiCalls" - array of API calls that you would like to run, including parameters

For simplicity and purposes of demo, the access token and API url are in this config file.

The format of the API call list is an array of pipe delimited strings in the following format:

  • [method name] | [param1],[value1] | [param2],[value2] | .... | [param(n)],[value(n)]

These strings will be parsed and passed to the API library where they will be converted into actual calls to the Canvas API, at the site defined by the "apiUrl" variable.

This sample config includes GET, POST, and PUT calls, to demonstrate each type of verb.

Each verb is implemented in the base class: clsHttpMethods.cs

Full response details are logged by NLog.  Grab these responses and inspect them for full details.  If your test produces an error or throws an exception, full details can also be found in the log file.  If you use my default nlog.config, log files will be found in this location:  c:\\logs\canvasApiTest\*.log

Comments

The purpose of this project is not to replace tools like Postman, the purpose is to help other developers using .NET to get started with the API.  I tried to keep things as clear as possible.

With myapi.config, I have been able to test any API call I have needed to date.  If you follow my code, you will need to add a C# method to handle each additional API call you want to make.  My "long hand" approach to creating matching C# methods could be modified, but hopefully helps to illustrate the concepts.  Share your approaches.

There is plenty of room for optimization, optimize to meet your needs and share any enhancements you feel people could benefit from.

Project Source Code

Source code can be found on BitBucket here:  Canvas API Implementation

The source published on BitBucket compiles and runs with Visual Studio 2015 and .NET Framework 4.5.2

IMPORTANT:  Make sure you edit the myapi.config file with your settings, and modify API variable values to match your environment.

All code is provided as-is for demonstration purposes only.

2016.12.03 - UPDATE - File Upload via POST

  • I have added a class that walks through the steps of uploading a file.  This code was pulled from another project and put together quickly as a response to a community question, make sure you test failure scenarios in your environment.
4 Comments