This section outlines how to use code generation to create an Ed-Fi ODS / API Client SDK using a Windows environment targeting C#. The high-level steps are:
Each step is outlined in detail below.
This step ensures you have the prerequisite software on your development machine. In case you're wondering: the code generation leverages Java, but it does output C# code.
The Ed-Fi ODS / API Client SDK source code is contained in an Ed-Fi repository called Ed-Fi-ODS-API-SDK.
Follow these steps to download the repository archive and extract it:
The SDK source files are generated via a few simple command line steps.
java -jar sdk-generate.jar csharp --url https://{Domain root of API}/metadata/{section}/api-docs --baseDir {destination folder} --apiPackage {API namespace} --helperPackage {SDK namespace} --modelPackage {Model namespace} |
A brief description of the switch options follows:
To generate SDK source files, navigate to C:\Ed-Fi-ODS-API-SDK\target\scala-2.10 and run the following commands to generate C# SDK source files for the Ed-Fi-hosted instance at https://api.ed-fi.org:
java -jar sdk-generate.jar csharp --url https://api.ed-fi.org/api/metadata/resources/api-docs --baseDir C:\Ed-Fi-ODS-API-SDK\CSharpSDK --apiPackage EdFi.OdsApi.Api.Resources --modelPackage EdFi.OdsApi.Models.Resources --helperPackage EdFi.OdsApi.Sdk --projectName EdFiClientSDK java -jar sdk-generate.jar csharp --url https://api.ed-fi.org/api/metadata/descriptors/api-docs --baseDir C:\Ed-Fi-ODS-API-SDK\CSharpSDK --apiPackage EdFi.OdsApi.Api.Descriptors --modelPackage EdFi.OdsApi.Models.Descriptors --helperPackage EdFi.OdsApi.Sdk --projectName EdFiClientSDK java -jar sdk-generate.jar csharp --url https://api.ed-fi.org/api/metadata/types/api-docs --baseDir C:\Ed-Fi-ODS-API-SDK\CSharpSDK --apiPackage EdFi.OdsApi.Api.Types --modelPackage EdFi.OdsApi.Models.Types --helperPackage EdFi.OdsApi.Sdk --projectName EdFiClientSDK java -jar sdk-generate.jar csharp --url https://api.ed-fi.org/api/metadata/assessment/api-docs --baseDir C:\Ed-Fi-ODS-API-SDK\CSharpSDK --apiPackage EdFi.OdsApi.Api.AssessmentComposite --modelPackage EdFi.OdsApi.Models.AssessmentComposite --helperPackage EdFi.OdsApi.Sdk --projectName EdFiClientSDK java -jar sdk-generate.jar csharp --url https://api.ed-fi.org/api/metadata/enrollment/api-docs --baseDir C:\Ed-Fi-ODS-API-SDK\CSharpSDK --apiPackage EdFi.OdsApi.Api.EnrollmentComposite --modelPackage EdFi.OdsApi.Models.EnrollmentComposite --helperPackage EdFi.OdsApi.Sdk --projectName EdFiClientSDK java -jar sdk-generate.jar csharp --url https://api.ed-fi.org/api/metadata/school-and-student/api-docs --baseDir C:\Ed-Fi-ODS-API-SDK\CSharpSDK --apiPackage EdFi.OdsApi.Api.SchoolandStudent --modelPackage EdFi.OdsApi.Models.School_and_Student --helperPackage EdFi.OdsApi.Sdk --projectName EdFiClientSDK |
PM>
prompt, enter "install-package restsharp".Edit the Program.cs file and paste and add the following using statements at the top of the file:
using EdFi.OdsApi.Sdk; using EdFi.OdsApi.Models.Resources; using EdFi.OdsApi.Api.Resources; using RestSharp; |
Edit the Program.cs file and paste the following into the Main method. The client and key are using a publicly available sandbox environment with sample data hosted by the Ed-Fi Alliance.
// Trust all SSL certs -- needed unless signed SSL certificates are configured. System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true); // Oauth configuration var oauthUrl = "https://api.ed-fi.org/api"; var clientKey = "RvcohKz9zHI4"; var clientSecret = "E1iEFusaNf81xzCxwHfbolkC"; // RestSharp dependency, install via NuGet var client = new RestClient("https://api.ed-fi.org/api/api/v2.0/2017"); // TokenRetriever makes the oauth calls var tokenRetriever = new TokenRetriever(oauthUrl, clientKey, clientSecret); // Plug Oauth into RestSharp's authentication scheme client.Authenticator = new BearerTokenAuthenticator(tokenRetriever); // GET schools var api = new SchoolsApi(client); var response = api.GetSchoolsAll(null, null); // offset, limit var httpReponseCode = response.StatusCode; // returns System.Net.HttpStatusCode.OK var schools = response.Data; Console.WriteLine("Response code is " + httpReponseCode); foreach (var school in schools) { Console.WriteLine(school.nameOfInstitution); } Console.WriteLine(); Console.WriteLine("Hit ENTER key to continue..."); Console.ReadLine(); |
With that, you're done!
This exercise leveraged a publicly available instance of the API, which contains the surface for a core implementation. If you're working with a specific platform host and you already have a key/secret pair, a great next step is to use these same techniques to generate an SDK for that platform. If the platform host has extended the data model, your new code will automatically include those structures in the data access components in the generated code.
The following link is a ZIP archive containing a C# sample program that uses the client SDK: The Sample program works against the Ed-Fi ODS / API sandbox hosted at https://apidocs.ed-fi.org. |