Windows 101
This recipe provides an example of reading data from a Kumulos API method and reading the result into a local model object.
Ingredients¶
This recipe assumes that you have working knowledge of C# and an applicable toolchain (ASP.NET / Windows Phone / Xamarin).
Recipe¶
The process to read data from a Kumulos API method is:
- Create a new C# project with references to the Kumulos SDK binary and the Newtonsoft.Json Nuget package
- Instantiate a KumulosHttp client with your Api Key and Secret Key
- Call an API method
- Map the result payload to a local model object
using System;
using Kumulos.NET;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Kumulos;
using System.Collections.Generic;
namespace CSharpExample
{
class MainClass
{
private static string apiKey = "YOUR_API_KEY";
private static string secretKey = "YOUR_SECRET_KEY";
public static void Main (string[] args)
{
KumulosHttp apiClient = new KumulosHttp (apiKey, secretKey);
Task<Kumulos.Payload> apiCall = apiClient.CallAsync ("getOptionTypesWithOptions");
apiCall.Wait ();
List<OptionTypeWithOptions> optionTypesWOptions = new List<OptionTypeWithOptions>();
foreach (KumulosObject obj in apiCall.Result) {
optionTypesWOptions.Add(OptionTypeWithOptions.CreateOptionTypeWithOptionsFromKumulosObject(obj));
}
Console.WriteLine ("Results:");
foreach (OptionTypeWithOptions optionType in optionTypesWOptions) {
Console.WriteLine(optionType.Write());
}
Console.Write("Done");
}
}
class OptionTypeWithOptions {
public static OptionTypeWithOptions CreateOptionTypeWithOptionsFromKumulosObject(KumulosObject obj) {
List<Option> options = new List<Option> ();
foreach(KumulosObject option in obj.GetPayload ("options")) {
options.Add (Option.CreateOptionFromKumulosObject (option));
}
DateTime? timeUpdated = null;
if (obj.GetString ("timeUpdated") != "0000-00-00 00:00:00") {
timeUpdated = DateTime.Parse (obj.GetString ("timeUpdated"));
}
return new OptionTypeWithOptions (
obj.GetInt("optionTypeID"),
obj.GetString("title"),
obj.GetInt("displayOrder"),
DateTime.Parse(obj.GetString("timeCreated")),
timeUpdated,
options.ToArray()
);
}
public OptionTypeWithOptions(int objectTypeId, string title, int displayOrder, DateTime timeCreated, DateTime? timeUpdated, Option[] options) {
this.objectTypeId = objectTypeId;
this.title = title;
this.displayOrder = displayOrder;
this.timeCreated = timeCreated;
this.timeUpdated = timeUpdated;
this.options = options;
}
private int objectTypeId;
private string title;
private int displayOrder;
private DateTime timeCreated;
private DateTime? timeUpdated;
private Option[] options;
public string GetTitle() {
return title;
}
public string Write() {
return string.Format ("ID: {0} Title: {1}\nOptions:\n{2}", this.objectTypeId, this.title, this.WriteOptions ());
}
private string WriteOptions() {
string result = "";
foreach (Option opt in this.options) {
result += string.Format ("\tOptionID: {0} Name: {1}", opt.GetId (), opt.GetTitle ()) + Environment.NewLine;
}
return result;
}
}
class Option {
public static Option CreateOptionFromKumulosObject(KumulosObject obj) {
DateTime? timeUpdated = null;
if (obj.GetString ("timeUpdated") != "0000-00-00 00:00:00") {
timeUpdated = DateTime.Parse (obj.GetString ("timeUpdated"));
}
return new Option (
obj.GetInt("optionID"),
obj.GetString("title"),
obj.GetDouble("pricePerUnit"),
obj.GetInt("isVegetarian") == 1 ? true : false,
obj.GetInt("type"),
DateTime.Parse(obj.GetString("timeCreated")),
timeUpdated
);
}
public Option(int optionId, string title, double pricePerUnit, bool isVegetarian, int type, DateTime timeCreated, DateTime? timeUpdated) {
this.optionId = optionId;
this.title = title;
this.pricePerUnit = pricePerUnit;
this.isVegetarian = isVegetarian;
this.type = type;
this.timeCreated = timeCreated;
this.timeUpdated = timeUpdated;
}
private int optionId;
private string title;
private double pricePerUnit;
private bool isVegetarian;
private int type;
private DateTime timeCreated;
private DateTime? timeUpdated;
public string GetTitle()
{
return title;
}
public int GetId() {
return optionId;
}
}
}