. . .

Use ApiOmat with C# and .NET

This guide is outdated and you should use our generated C# SDK. The following guide was written when we didn't provide a C# SDK yet. But we keep it here because it might still be useful for you if you can't or don't want to use the C# SDK.

One of the outstanding features of ApiOmat is the generation of an individual SDK for every single backend. With this SDK it's extremely easy for every developer to include the use of the backend into his application. For the SDKs we currently support Java, Objective C, JavaScript, Python, PHP and Android.

For other programming languages you can use our REST API. It provides the same functionality as the SDKs. In this blog article we're going to show you an example of how to use the REST API in C# and .NET.

The REST API can be used with any .NET HTTP Client Library. When you develop in Windows with .NET 4.5 then you can use the classes System.Net.HttpWebRequest, System.Net.WebClient, as well as System.Net.Http.HttpClient. When developing with Mono in Linux you can also use HttpWebRequest and WebClient, and there are ways to include HttpClient, too. In our example we're using HttpClient.

Let's get to the code example for C#, in which we use the REST interface to save data. This assumes that you already set up an account on the website and created an app and a string attribute in the Dashboard.

The REST resource for saving data is described in our REST API Dokumentation.

using System;
using System.Text;
using System.Threading.Tasks;
// Add the System.Net.Http library to your references
using System.Net.Http;
using System.Net.Http.Headers;
 
namespace apiomat_examples
{
class Program
{
// Your account data and API key
private static string userEmail = "user@mail.com";
private static string userPw = "YourPassword";
private static string apiKey = "1234567890123456789";
 
private static Uri baseURL = new Uri("https://apiomat.org");
private static string appsPath = "/yambas/rest/apps";
 
// Your app, module, class and attribute name
private static string appName = "TestApp2014";
// The basic module is always the name of the app plus "Main"
private static string moduleName = appName + "Main";
private static string className = "TestClass";
private static string attributeName = "testAttribute";
 
static void Main(string[] args)
{
try
{
Uri location = CreateNewDataAsync().Result;
if (location != null)
{
Console.WriteLine(location);
}
else
{
Console.WriteLine("Something went wrong.");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
 
private static async Task<Uri> CreateNewDataAsync()
{
using (var client = new HttpClient())
{
// Set up the basics
client.BaseAddress = baseURL;
// in this example the path is "/yambas/rest/apps/TestApp2014/models/TestApp2014Main/TestClass"
string path = appsPath + "/" + appName + "/models/" + moduleName + "/" + className;
// Headers (auth and API key)
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", GetBase64Auth(userEmail, userPw));
client.DefaultRequestHeaders.Add("x-apiomat-apikey", apiKey);
// Set up the data
string newData = "example";
string jsonBody = "{\"@type\":\"" + moduleName + "$" + className + "\",\"" + attributeName + "\":\"" + newData + "\"}";
StringContent content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
 
// Action!
HttpResponseMessage response = await client.PostAsync(path, content);
 
// Check and return
if (response.IsSuccessStatusCode)
{
return response.Headers.Location;
}
else
{
return null;
}
}
}
 
private static string GetBase64Auth(string user, string pw)
{
byte[] plainTextBytes = System.Text.Encoding.UTF8.GetBytes(user + ":" + pw);
return System.Convert.ToBase64String(plainTextBytes);
}
}
}

Explanation of the code:

At first we declare and initialize some variables in the class. You have to change the account data, as well as the app, class and attribute names according to your data.

From the Main method, the method CreateNewDataAsync() gets called. This method is asynchronous, so you can use it for example in a Windows Store App without blocking the GUI Thread.

In this method the HttpClient gets created, headers get added and the HTTP body gets created. With "await client.PostAsync(path, content);" the request gets executed. As a result the value of the location header gets returned.

With this value you can now access the resource that just got created and send HTTP GET, PUT and DELETE requests to it to work with the data.

Have Fun!

More Information: C# Quickstart, C# SDK

* link only available in Enterprise Documentation