© 2018 Capita Business Services Ltd. All rights reserved.

Capita Education Software Solutions is a trading name of Capita Business Services Ltd. Our Registered office is 30 Berners Street, London, W1T 3LR and our registered number is 02299747. Further information about Capita plc can be found in our legal statement.

SIMS ID - Sample Code - Base API calls and Classes

Sample code basic classes

An example code project can be downloaded from here. The code has been extended to contain a .Net Core 3.1 version and a .Net Framework 4.51 alternative.  They do the same thing but .Net Core 3.1 is does not appear to be supported in older versions of Visual Studio. Both versions have identical code. 

The required key bits of code are all listed below.

Main Application

// Set the log file
PostitCommon.Log.LogFile = @"c:\temp\Demo.log";
// Auth request captures all the info needed to access a call
PostitCommon.AuthRequest arq = new PostitCommon.AuthRequest();
// Set the values
arq.Client_ID = "sas-oneroster-...."; // "<Ask partner support>";
arq.Scopes = "onerosterapi organisation partner rapapi";
arq.STS = "https://simsid-partner-stsserver.azurewebsites.net/connect/token";
arq.Organisation_ID = "<Ask partner support>";
arq.Secret = "<Ask partner support>";
arq.OCP_APIM_Key = "<Ask partner support>";
string folder = @"c:\temp\rapor"; // Where to save output files to
// Get a token
PostitCommon.BearerToken bt1 = new PostitCommon.BearerToken(arq, httpClient);
if (!string.IsNullOrEmpty(bt1.Token))
{
    // Get RAPing
    foreach (string s in new string[]{ "students","LearnerParent","Staff","Organisation"})
    {
        Run("https://simsid-partner-rapapi.azurewebsites.net/api/v1/Organisation/" 
          + arq.Organisation_ID + "/"+s+"/", 
           bt1, arq.OCP_APIM_Key, 
           "RAP"+s+".json", 
             folder);
    }

    // Get One Rostering 
    foreach (string s in new string[] { "Teacher", "classes", "academicSessions", 
           "categories" , "enrollments", "Gradingperiods", "Schools", "Students", "Terms", "Users"})
    {
        Run("https://simsid-partner-oneroster.azurewebsites.net/ims/oneroster/v1p1/" 
                        + s + "/", 
                         bt1, 
                         arq.OCP_APIM_Key, 
                         "OR_" + s + ".json", 
                          folder);
    }
}

}  

Run

 static HttpClient httpClient = new HttpClient();
 static void Run(string call, PostitCommon.BearerToken bt1,string key ,string file, string folder)
 {
            PostitCommon.Call c1 = new PostitCommon.Call(call);
            string resp = c1.Execute(bt1.Token, httpClient, key);
            PostitCore.Dump.ToFile(resp, file, folder);

 }

Auth Request

amespace PostitCommon
{
    public class AuthRequest
    {
        /// <summary>
        /// Token server address
        /// partner support to provide
        /// </summary>
        public string STS { get; set; }
        /// <summary>
        /// For each school you get a client id and a secret
        /// From SIMS ID
        /// </summary>
        public string Client_ID { get; set; }
        /// <summary>
        /// For each school you get a client id and a secret
        /// From SIMS ID
        /// </summary>
        public string Secret { get; set; }
        /// <summary>
        /// Each school has an org id
        /// From SIMS ID
        /// </summary>
        public string Organisation_ID { get; set; }
        /// <summary>
        /// The scopes define access required
        /// e.g. partnerserverapplication partner
        /// </summary>
        public string Scopes { get; set; }
        /// <summary>
        /// The OCP APIM Key - one key per vendor possibly per app.
        /// From partner support
        /// </summary>
        public string OCP_APIM_Key { get; set; }

    }
}

Bearer Token Request

using System.Collections.Generic;
using System.Net.Http;
namespace PostitCommon
{
    public class BearerToken
    {
        public string Token = "";
        //int HttpCode = 500;
        public BearerToken(AuthRequest arq, HttpClient httpClient)
        {
            string grant_type = "client_credentials";
            string client_id = arq.Client_ID; 
            string client_secret = arq.Secret;
            var form = new Dictionary<string, string>
                {
                    {"grant_type", grant_type},
                    {"client_id", client_id},
                    {"client_secret", client_secret},
                    { "scope", arq.Scopes },
                    { "acr_values","orgselected:"+arq.Organisation_ID }
                };
            var httpClientResponseTask = httpClient.PostAsync(arq.STS, new FormUrlEncodedContent(form));
            httpClientResponseTask.Wait();
            var result = httpClientResponseTask.Result;
            var responseStringTask = result.Content.ReadAsStringAsync();
            responseStringTask.Wait();
            //HttpCode = responseStringTask.Result.;
            string responseString = responseStringTask.Result;
            var jo2 = Newtonsoft.Json.JsonConvert.DeserializeObject(responseString);
            Newtonsoft.Json.Linq.JObject jo = Newtonsoft.Json.Linq.JObject.Parse(jo2.ToString());
            Token = (string)jo.SelectToken("access_token");

        }
    }

Call Code

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Xml;
using System.IO;
namespace PostitCommon
{
    public class Call
    {
        public string ErrorMessage = "";
        private string URL { get; set; }
        private string Name { get; set; }
        private string Content_type { get; set; }
        public Call(string Url)
        {
            URL = Url;
            Name = "Direct URL";
        }
        public string Execute(string BearerToken, HttpClient httpClient, string OCP_APIM_Key)
        {
            string rc = "";
            try
            {
                var request = new HttpRequestMessage()
                {
                    RequestUri = new Uri(URL),
                    Method = HttpMethod.Get,
                };
                if (Content_type != null && Content_type != "")
                {
                    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(Content_type));
                }
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", BearerToken);
                request.Headers.Add("Ocp-Apim-Subscription-Key", OCP_APIM_Key);
                var httpClientResponseTask = httpClient.SendAsync(request);
                httpClientResponseTask.Wait();
                var result = httpClientResponseTask.Result;
                var responseStringTask = result.Content.ReadAsStringAsync();
                rc = responseStringTask.Result.ToString();
            }
            catch (Exception ex)
            {
                ErrorMessage = ">>>>+" + Name + ": " + ex.Message;
                PostitCommon.Log.Write(ErrorMessage);
            }
            return rc;
        }
    }
}

Dump.ToFile

using System.IO;
namespace PostitCore
{
    public class Dump
    {
        public static void ToFile(string content, string name, string folder)
        {
            string loc = Path.Combine(folder, name);
            if  (! Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            using (StreamWriter w = new StreamWriter(loc))
            {
                w.Write(content);
                w.Flush();
                w.Close();
            }
        }
    }
}