Versions Compared
Key
- This line was added.
- This line was removed.
- Formatting was changed.
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:
Table of Contents | ||
---|---|---|
|
Each step is outlined in detail below.
Step 1. Install and Configure Required Software
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.
- Install the Java SDK. Version 8 or newer is required.(http://www.oracle.com/technetwork/java/javase/downloads/index.htm)
- Install the Scala Build Tool Tool (sbt).(http://www.scala-sbt.org/release/tutorial/Installing-sbt-on-Windows.html)
- Set the Windows PATH environment variable to find the jdk bin directory.
Step 2. Download the Ed-Fi ODS Client SDK Source 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:
- Navigate to https://github.com/Ed-Fi-Alliance/Ed-Fi-ODS-API-SDK/tree/v2.2to GitHub and download the repository archive to your local drive.
- In Windows Explorer, right-click on the downloaded ZIP files and select “Properties.” On the General tab, press the Unblock button to allow the contents of the contained scripts to execute properly.
- In Windows Explorer, right-click on the downloaded ZIP file and select Extract All… Enter “C:\” for the target folder. The ZIP files contain an embedded folder ending in “-2.0” (i.e., C:\Ed-Fi-ODS-API-SDK-2.0).
- After the extractions are complete, rename the folder to remove the “-2.0” from the folder name (i.e., C:\Ed-Fi-ODS-API-SDK).
Step 3. Build the Swagger codegen jar File
- Open a command prompt to C:\Ed-Fi-ODS-API-SDK and type "sbt assembly". This will build the Swagger codegen jar file in C:\Ed-Fi-ODS-API-SDK\target\scala-2.10 using the Scala Build Tool installed above.
Step 4. Generate the SDK Source Files
The SDK source files are generated via a few simple command line steps.
Code Block |
---|
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:
- target-language. csharp or java (we'll use csharp).
- --url. https://{Domain root of API}/metadata/{section}/api-docs (where section is generally "resource", "descriptors", "types", or an API Profile or Composite Resource name).
- --baseDir. The local folder where you would like the code files to be created.
- --apiPackage. Namespace for the classes that expose methods that map to resources and verbs (e.g., EdFi.OdsApi.Api.{section}).
- --modelPackage. Namespace for the entities that are exchanged with the Ed-Fi ODS / API (e.g., EdFi.OdsApi.Models.{section}).
- --helperPackage. Namespace for the general SDK classes that support the Ed-Fi ODS / API interaction (e.g., EdFi.OdsApi.Sdk).
- --projectName. The name of the Visual Studio project for the generated SDK.
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:
Code Block | ||
---|---|---|
| ||
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 |
Step 5. Use the SDK in a Sample C# Program
- Open up Visual Studio and create a new project. Choose the type Visual C# > Console Application. Name the project "EdFiClientSDK".
- Add folders to the project, one for each of the folders generated from the previous step, i.e.,:
- Api
- \Resources
- \Descriptors
- \Types
- \AssessmentComposite
- \EnrollmentComposite
- \School_and_Student
- Models
- \Resources
- \Descriptors
- \Types
- \AssessmentComposite
- \EnrollmentComposite
- \School_and_Student
- Sdk
- Api
- Right-click on each of the folders and add the existing classes for each package. The files can be found at the following location: C:\Ed-Fi-ODS-API-SDK\CSharpSDK\EdFiClientSDK\EdFi\OdsApi. Be sure to select all of the generated SDK files for each package.
- Use the Package Manager Console to install the RestSharp library. At the
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:
Code Block language c# 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.
Code Block language c# // 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();
- Build the project and run it without debugging (Ctrl+F5) and you should see the following results:
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.
Include Page | ||||
---|---|---|---|---|
|
Panel | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||
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. |