API Call - .Net (C# Example)

Integrating a .Net application with EduBrite (c# Example)

You can integrate any .Net application with EduBrite by adding api calls in your c# application. Integration could be as simple as single sign on, or more sophisticated which shows data pulled from EduBrite by making specific API calls.

Follow the below example. This example assumes that encryption is not enabled in microsite's integration settings.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net;

namespace ConsoleApplication
{
    class ApiState
    {
        public string sessionId;
        public string sessionInfo;
        public string apiUrl = "http://site11.edubrite.com/";
        public string apiUserName = "api_10fc58c0-7c32-11e3-afb2-a1819f9d3329";
        public string apiPassword = "gkhNp47_a0yrnwGNlbtTQCZaNozw_KnPy3-zTaEExL3vdqbAMODLO5-8ULnt_7QIMV5hU9YF-HPWNb7LjBtejUg20JD61tDsPen-AonP8Wk*|444182626000";
    }
    class Program
    {
        static void Main(string[] args)
        {
            ApiState state = new ApiState();

//First call to connect will initialize the session_id cookie
            call(state, null, "connect.do", null).Wait();
            var parameters = new Dictionary<string, string>
                {
                    {"dispatch", "connect" },
                    { "username", state.apiUserName},
                    { "password", state.apiPassword }
                };

//Second call to connect will initialize the session_id as well as session_info cookie
            call(state, null, "connect.do", parameters).Wait();
            if(state.sessionInfo != null)
            {

//Once both cookies are set, you can start making API calls, e.g. get Test History for user mwhite
                System.Diagnostics.Debug.WriteLine("Login successful, sessionInfo == " + state.sessionInfo);
                parameters = new Dictionary<string, string>
                {
                    {"dispatch", "list" },
                    { "pageSize", "10"},
                    { "xml", "true" }
                };
                call(state, "mwhite", "testhistory.do", parameters).Wait();
            }
        }

        static async Task<String> call(ApiState state, String realUser, String api, Dictionary<string, string> parameters)
        {
            CookieContainer cookies = new CookieContainer();
            HttpClientHandler handler = new HttpClientHandler();
            handler.CookieContainer = cookies;
            string result = null;
            using (var client = new HttpClient(handler))
            {
                client.BaseAddress = new Uri(state.apiUrl);
                if (realUser != null)
                {
                    client.DefaultRequestHeaders.Add("REAL_UNAME", realUser);
                }
                FormUrlEncodedContent content = null;
                if (parameters != null)
                {
                    content = new FormUrlEncodedContent(parameters);
                }
                Uri uri = new Uri(state.apiUrl);
                if (state.sessionId != null)
                {
                    handler.CookieContainer.Add(uri, new Cookie("SESSION_ID", state.sessionId));
                }
                if (state.sessionInfo != null)
                {
                    handler.CookieContainer.Add(uri, new Cookie("SESSION_INFO", state.sessionInfo));
                }
                HttpResponseMessage response = await client.PostAsync("oltpublish/site/"+api, content);
                result = await response.Content.ReadAsStringAsync();
                System.Diagnostics.Debug.WriteLine(result);
                IEnumerable<Cookie> responseCookies = cookies.GetCookies(uri).Cast<Cookie>();
                foreach (Cookie cookie in responseCookies)
                {
                    if (cookie.Name == "SESSION_ID")
                    {
                        System.Diagnostics.Debug.WriteLine("connect " + cookie.Name + ": " + cookie.Value);
                        state.sessionId = cookie.Value;
                    }
                    else if (cookie.Name == "SESSION_INFO")
                    {
                        System.Diagnostics.Debug.WriteLine("connect " + cookie.Name + ": " + cookie.Value);
                        state.sessionInfo = cookie.Value;
                    }
                }
            }
            return result;
        }
    }
}

 

 


Rating: