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.
Step 1. Install latest version of Java
If you don't already have Java installed, navigate to https://java.com/en/download/ and download the latest installer. Run the installer to install the latest version of Java. In case you're wondering: the code generation leverages Java, but it does output C# code.
Step 2. Download the Swagger Codegen JAR File
Download the latest version of the Swagger Codegen JAR 2.3.0+.
Windows users can use Invoke-WebRequest in PowerShell 3.0+ (e.g., Invoke-WebRequest -OutFile swagger-codegen-cli.jar).
For more information and download options visit https://github.com/swagger-api/swagger-codegen.
Step 3. Generate the SDK Source Files
The SDK source files are generated via a few simple command line steps.
Detailed description of the switch options can be found at https://github.com/swagger-api/swagger-codegen.
To generate SDK source files, navigate to the folder containing swagger-codegen-cli.jar and run the following commands in PowerShell 3.0+ to generate C# SDK source files for the Ed-Fi-hosted instance at https://api.ed-fi.org/v3/api/.
Wait for the Swagger Codegen to finish generating code and return to the command line. A Visual Studio Solution named EdFi.OdsApi.Sdk.sln will be created with the SDK artifacts.
Step 4. Use the SDK in a Sample C# Program
- Open EdFi.OdsApi.Sdk.sln in Visual Studio.
- Right-click on the solution and click "Restore NuGetPackages."
- Make sure Copy Local is set to true for the RestSharp reference.
- Build the solution.
- Right-click on the solution and add a new project. Choose the type Visual C# > Console Application. Name the project "EdFi.OdsApi.SdkClient".
Right click on 'Edfi.odsApi.SdkClient' and set it up as startup project.
In Solution Explorer, right-click on the "EdFi.OdsApi.SdkClient" project references node and click Add Reference.
In the Add Reference dialog box, select the projects tab and select EdFi.OdsApi.Sdk, and then click OK.
- Use the Package Manager Console to install the RestSharp and Newtonsoft libraries. At the
PM>prompt, enter "install-package restsharp -version 105.1.0 -project Edfi.odsApi.SdkClient" and "install-package Newtonsoft.Json -project Edfi.odsApi.SdkClient"
Open the Program.cs file and add the following
usingstatements at the top of the file:
Edit the Program.cs file and paste the following into the
Mainmethod. The client and key are using a publicly available sandbox environment with sample data hosted by the Ed-Fi Alliance.
Add a .cs file named TokenRetriever.cs and copy the following code to help with OAuth integration.
- 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.
- No labels