C# Coding example to use OData

<< Click to Display Table of Contents >>

Navigation:  Application integration > Bizagi RESTful API > Bizagi API examples >

C# Coding example to use OData

Overview

The following example shows how you can create a code in C# to invoke OData methods from an external systems. Although there are different programming languages from where you can invoke OData, this example shows the coding steps you need to fulfil.

 

What do you need to do

Recall that OData relies on OAuth 2.0 protocol. First you need to register the external application in the Work Portal to obtain the keys. See Getting started.

 

OData_Workportal4

 

We recommend using the Grant type client credentials, and make sure you select the API in the scope. Then, you need to start coding.  The coding steps to invoke an OData method are the following:

 

1. Obtain the OAuth 2.0 token.

2. Execute the method. Depending on the method, you have to set a GET or POST invocation.

 

Obtaining the access token

The first step is obtaining an OAuth 2.0 access token. We recommend creating a class to create a constructor with the access token, so is easier to serialize and manipulate.

 

Class with an OAuth response

public class OAuthResponse{
 private string tokenType;
    private string accessToken;
    public string TokenType{
           get{return tokenType;}

 }
    public string AccessToken{
           get{return accessToken;}

 }
 public OAuthResponse(string result){
         JObject resultObject = JObject.Parse(result);
         accessToken = resultObject["access_token"].ToString();
         tokenType = resultObject["token_type"].ToString();
 }
}

 

Create a method to get the access token

Now you need to create a method to get the access token. Keep in mind that this method has three inputs:

uri: URL of your Bizagi project, e.g. http://[ServerName]/[projectName]

ClientId: OAuth 2.0 Client ID key generated from the Bizagi Work Portal. See Getting started.

ClientSecret: OAuth 2.0 Client Secret key generated from the Bizagi Work Portal. See Getting started.

 

public static OAuthResponse AcquireRequestToken(string uri, string clientId, string clientSecret){

 //Set the OAuth keys formated in BASE64
    string authorization = Authorization(clientId, clientSecret);

 //Get the token
    HttpWebRequest request = CreateRequest(uri + "/oauth2/server/token", authorization, "application/x-www-form-urlencoded", "POST");
    SetMethodPost("grant_type=client_credentials&scope=api", request);
    var response = (System.Net.HttpWebResponse)request.GetResponse();{
           using (var reader = new System.IO.StreamReader(response.GetResponseStream())){
                   return new OAuthResponse(reader.ReadToEnd());
              }
 }
}

 

You also need the following method  that formats the OAuth keys in BASE64 as expected by Bizagi:

 

private static string Authorization(string clientId, string clientSecret){
 string concatenateKeys = clientId + ":" + clientSecret;
    var plainTextBytes = Encoding.UTF8.GetBytes(concatenateKeys);
    return "Basic " + Convert.ToBase64String(plainTextBytes);
}

 

The next step in the code is invoking the OData method.

 

Invoking a GET method

After obtaining the token from the previous method, you have to execute the Bizagi OData service. We have created another method called ExecuteMethod with the following inputs:

uri: URL of the ODATA method within your Bizagi project, e.g. http://[ServerName]/[projectName]/odata/data/processes You can consult our service index to see our OData methods.

token: token obtained in the method AcquireRequestToken. Bear in mind that this is declared as a OAuthResponse object.

method: The HTTP method, set this as GET.

 

public static void ExecuteMethod(string uri, OAuthResponse token, string method){
 var request = CreateRequest(uri, token.TokenType + " " + token.AccessToken, "application/json", method);
 var response = (System.Net.HttpWebResponse)request.GetResponse();
    var reader = new System.IO.StreamReader(response.GetResponseStream());
    //Service response
    JObject resultObject = JObject.Parse(reader.ReadToEnd());
}
 

This method sets HTTP headers for the invocation:

 
private static HttpWebRequest CreateRequest(string uri, string authorization, string contentType, string method){

 var request = (HttpWebRequest)WebRequest.Create(uri);
    request.Headers.Add("Authorization", authorization);
    request.Headers.Add("Content-Type", contentType);
    request.Method = method;
    return request;
}

 

Invoking a POST method

After obtaining the token, you have to execute the Bizagi OData service. We have created another method called ExecuteMethod with the following inputs:

uri: URL of the ODATA method within your Bizagi project Bizagi project, e.g. http://[ServerName]/[projectName]/odata/searches(fde242da-qaadr23)

token: token obtainined in the method AcquireRequestToken. Bear in mind that this is declared as a OAuthResponse object.

method: The HTTP method, set this as POST.

startParameters: This is a JSON string input send as the body of the message. The structure of the JSON depends on the method. You can consult our service index to see our OData methods.

 

public static void ExecuteMethod(string uri, OAuthResponse token, string method, string startParameters = null){

 var request = CreateRequest(uri, token.TokenType + " " + token.AccessToken, "application/json", method);
 if (method == "post"){

         //This sets the body of the requests using the JSON string
         SetMethodPost(startParameters, request);
 }
 var response = (System.Net.HttpWebResponse)request.GetResponse();
 var reader = new System.IO.StreamReader(response.GetResponseStream());
 //Service response
 JObject resultObject = JObject.Parse(reader.ReadToEnd());
}

 

The following method set the body of the HTTP request for a POST invocation:

 

private static void SetMethodPost(string startParameters, HttpWebRequest request){

 byte[] body = Encoding.UTF8.GetBytes(startParameters);

 request.ContentLength = body.Length;

 request.GetRequestStream().Write(body, 0, body.Length);

}