![]() | Welcome to the Dyostem Server API documentation |
[This is preliminary documentation and is subject to change.]
Dyostem Server APIs allow third party software querying dyostem server.
Dyostem API Services are available to all 3rd party systems with a login/password.
This login/password pair is dedicated to a single customer account. If a 3rd party system is used to gather data for several customer accounts, a different login/password pair should be used for each account.
A typical communication with dyostem server could be:
Use OpenSession to open a session and get a token
HttpPostJson method is described in a code snippet below
1static void Main(string[] args) 2{ 3 Uri baseUri = new Uri("http://www.dyostem.com"); 4 5 try 6 { 7 var ret = HttpPostJson(new Uri(baseUri, "Services/ThirdPartySystem.svc/OpenSession"), 8 new{sLogin = "myLogin",sPassword = "myPassword"}); // Replace with your own credentials 9 10 // d.SessionId will contain the token id. 11 var sessionId = Guid.Parse(ret.d.SessionId.ToString()); 12 if (sessionId == Guid.Empty) // if token is {00000000-0000-0000-0000-000000000000} then the authentication has failed 13 { 14 throw new System.Security.Authentication.AuthenticationException(ret.d.Message.ToString()); 15 } 16 17 // TO DO : Insert here code to query the API using the token id 18 19 } 20 catch (Exception e) 21 { 22 var previousColor = Console.ForegroundColor; 23 Console.ForegroundColor = ConsoleColor.Red; 24 Console.WriteLine("ERR: unable to open session. Sytem returned:{0}\r\n{1}", e.Message, e.StackTrace); 25 Console.BackgroundColor = previousColor; 26 } 27}
Use GetMeasureTypes to get available measure types
1ret = HttpPostJson(new Uri(baseUri, "Services/ThirdPartySystem.svc/GetMeasureTypes"), 2 new { sessionId = sessionId }); 3 4var measureTypes = ret.d.MeasureTypes; 5foreach (var measureType in measureTypes) 6{ 7 var typeMeasureId = new Guid(measureType.Id.ToString()); 8 string translatedLabel = measureType.Name.ToString(); 9 10 Console.WriteLine(typeMeasureId + "\t" + translatedLabel); 11}
Use GetVintages to get available vintages
1ret = HttpPostJson(new Uri(baseUri, "Services/ThirdPartySystem.svc/GetVintages"), 2 new { sessionId = sessionId }); 3var vintages = ret.d.Vintages; 4 5foreach (var vintage in vintages) 6{ 7 var id = new Guid(vintage.Id.ToString()); 8 var label = vintage.Label.ToString(); 9 var startDate = (DateTime)vintage.StartDate; 10 var endDate = (DateTime?)vintage.EndDate; 11 12 Console.WriteLine(label + " (" + id + ") from: " + startDate.ToString("d") + " to " + (endDate.HasValue ? endDate.Value.ToString("d") : "Now")); 13}
Use GetAnalyses to get blocks analyses for a specific vintage
UnixTicks extension method is described in the code snippet below.
1var vintageStartDate = (DateTime)vintage.StartDate; 2var vintageEndDate = (DateTime?)vintage.EndDate; 3ret = HttpPostJson(new Uri(baseUri, "Services/ThirdPartySystem.svc/GetAnalyses"), 4 new { 5 sessionId = sessionId, 6 fromTimestamp = string.Format("/Date({0})/", vintageStartDate.UnixTicks()), 7 toTimestamp = (vintageEndDate.HasValue ? string.Format("/Date({0})/", (double?)vintageEndDate.Value.UnixTicks()) : null) 8}); 9 10var blockAnalyses = ret.d.Blocks; 11foreach (var block in blockAnalyses) 12{ 13 Console.WriteLine("Block :{0}", block.Name); 14 foreach (var analysis in block.Analyses) 15 { 16 Console.WriteLine("\tAnalysis :{0}", analysis.Timestamp); 17 foreach (var measure in analysis.Measures) 18 { 19 Console.WriteLine("\t\t{0}: {1} {2}", measure.MeasureTypeName, measure.Value, measure.Unit.HasValue ? measure.Unit.Value : ""); 20 } 21 } 22}
The following method allows to serialize an object to post, POST the query to dyostem API Services and deserialize the response.
It uses the Newtonsoft Json.NET library. (http://www.newtonsoft.com/json)
1/// <summary> 2/// Serialize the object to pass to the service and POST the HTTP Query. 3/// Deserialize the response object and return it. 4/// </summary> 5/// <param name="uri">The service URI</param> 6/// <param name="o">The object to pass to the service</param> 7/// <param name="timeoutInSecond">the maximum number of second to wait before throwing a TimeoutException</param> 8/// <returns>The deserialized response object</returns> 9public static dynamic HttpPostJson(Uri uri, object o, int timeoutInSecond = 120) 10{ 11 try 12 { 13 var httpWebRequest = (HttpWebRequest)WebRequest.Create(uri); 14 15 httpWebRequest.ContentType = "application/json"; 16 //httpWebRequest.ProtocolVersion = HttpVersion.Version10; // Uncomment this line if you want to used the old http version (1.0) 17 httpWebRequest.Method = "POST"; 18 httpWebRequest.Timeout = 1000 * timeoutInSecond; 19 20 if (Debugger.IsAttached) 21 Debug.WriteLine(string.Format("Request:\r\n{0}", httpWebRequest.RequestUri)); 22 23 using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) 24 { 25 var json = Newtonsoft.Json.JsonConvert.SerializeObject(o); 26 if (Debugger.IsAttached) 27 Debug.WriteLine(string.Format("json:\r\n{0}\r\n-------------------------------", json)); 28 streamWriter.Write(json); 29 } 30 31 var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 32 33 var r = ""; 34 using (var st = httpResponse.GetResponseStream()) 35 { 36 if (st != null) 37 using (var streamReader = new StreamReader(st)) 38 { 39 var result = streamReader.ReadToEnd(); 40 r = result; 41 } 42 } 43 44 if (Debugger.IsAttached) 45 Debug.WriteLine(string.Format("Response:\r\n{0}\r\n-------------------------------", r)); 46 47 var resp = Newtonsoft.Json.JsonConvert.DeserializeObject(r); 48 49 return resp; 50 } 51 catch (Exception e) 52 { 53 var previousColor = Console.ForegroundColor; 54 Console.ForegroundColor = ConsoleColor.Red; 55 Console.WriteLine(string.Format("ERR: unable to post JSON data. Sytem returned:{0}\r\n{1}", e.Message, e.StackTrace)); 56 Console.BackgroundColor = previousColor; 57 return null; 58 } 59}
You can also use the following code snippet to convert DateTime object to a format accepted by the service.
1public static class Extensions 2{ 3 public static double UnixTicks(this DateTime dt) 4 { 5 DateTime d1 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); 6 DateTime d2 = dt.ToUniversalTime(); 7 TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks); 8 return ts.TotalMilliseconds; 9 } 10}