Background
Working on a Moodle project, where I wanted to add and update content using Moodle API.
I couldn't find any .net wrappers to be able to talk to Moodle with a strict contract.
I couldn't find any .net wrappers to be able to talk to Moodle with a strict contract.
The Solution
To solve the issue I downloaded the Moodle API documentation. Wrote a piece of code that generated the proxy classes to connect to Moodle's Rest API using HttpClient.
The Nitty Gritty
The project (find download link below), uses the namespace Moodle.API.Wrapper.
It basically consists of two folders major namespaces "Moodle.API.Wrapper.Controllers" and "Moodle.API.Wrapper.Models".
The controllers namespace is then divided into further namespaces belong to each area as organized by the API documentation. Inside these namespaces there are classes again, as organized in the API documentation. The structure is identified through the function name's naming convention. e.g. the API call: core_competency_list_competency_frameworks is organized into Moodle.API.Wrapper.Controllers.Core namespace inside a class called Competency. The class then contains the function called ListCompetencyFramework. Note that the function names are converted to C# naming conventions.
It basically consists of two folders major namespaces "Moodle.API.Wrapper.Controllers" and "Moodle.API.Wrapper.Models".
The controllers namespace is then divided into further namespaces belong to each area as organized by the API documentation. Inside these namespaces there are classes again, as organized in the API documentation. The structure is identified through the function name's naming convention. e.g. the API call: core_competency_list_competency_frameworks is organized into Moodle.API.Wrapper.Controllers.Core namespace inside a class called Competency. The class then contains the function called ListCompetencyFramework. Note that the function names are converted to C# naming conventions.
The signature of the functions looks like this:
public CompetencyFrameworksModel ListCompetencyFrameworks(CompetencyFrameworksInputModel competencyFrameworksInputModel)
{
return Post<CompetencyFrameworksModel,CompetencyFrameworksInputModel>("core_competency_list_competency_frameworks", competencyFrameworksInputModel);
}
All the required parameters are converted to Model classes clearly identified by the keyword "InputModel" if the class is to be used as input to the API.
Each controller class is inherited from BaseController which contains the implementations for the Post method. The post method does all the heavy lifting of converting classes into rest format and vice versa.
Naturally, all the converted Models are found inside the Model namespace. To be able to make a call to the API using this wrapper, all you need to do is add a reference to the wrapper and use code like this:
var competencyController = new Moodle.API.Wrapper.Controllers.Competency();
competencyController.SetupController(securityToken, url,WriteProgress);
var frameworks = competencyController.ListCompetencyFrameworks(new Moodle.API.Wrapper.Models.Core.CompetencyFrameworksInputModel {
context = new Moodle.API.Wrapper.Models.Core.ContextInputModel {
// assign values here
}
});
public Func<string, Task> WriteProgress;
Download Link
Click here to download the zip file.
Git Hub Repo here
Hi, Have been looking for this moodle wrapper like a needle. But maybe some help will be appreciated on you implemented it to come up with a working solution.
ReplyDelete