/// <summary> /// Example code to send Rosette API a ping to check its reachability. /// Requires Nuget Package: /// rosette_api /// </summary> static void Main(string[] args) { //To use the C# API, you must provide an API key string apikey = "Your API key"; //You may set the API key via command line argument: //ping yourapikeyhere if (args.Length != 0) { apikey = args[0]; } try { CAPI NewCAPI = new CAPI(apikey); //The results of the API call will come back in the form of a Dictionary Dictionary<string, Object> pingResult = NewCAPI.Ping(); Console.WriteLine(new JavaScriptSerializer().Serialize(pingResult)); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } }
public void CAPITest() { List<TestDataStructure> mockData = Setup(); CMockData cmd = new CMockData(); foreach (TestDataStructure td in mockData) { System.Diagnostics.Debug.WriteLine("Evaluating " + td.outputDataFilename); Console.WriteLine("Evaluating " + td.outputDataFilename); Dictionary<object, object> tdInputData = null; if (td.inpFilename != null) { string tdInput = cmd.getFileData(td.inpFilename); if (tdInput != null) { tdInputData = new JavaScriptSerializer().Deserialize<Dictionary<object, object>>(tdInput); } } Dictionary<string, object> result = new Dictionary<string,object>(); var fakeResponse = new HttpResponseMessage(); string tdStatus = cmd.getFileData(td.outputStatusFilename); fakeResponse.StatusCode = (HttpStatusCode)(Convert.ToInt32(tdStatus)); string tdOutput = cmd.getFileData(td.outputDataFilename); if (tdOutput.Length > 200) { System.Diagnostics.Debug.WriteLine("GZipping"); byte[] compress = Compress(Encoding.ASCII.GetBytes(tdOutput)); fakeResponse.Content = new ByteArrayContent(compress); fakeResponse.Content.Headers.ContentEncoding.Add("gzip"); MemoryStream stream = new MemoryStream(Decompress(compress)); StreamReader reader = new StreamReader(stream); tdOutput = reader.ReadToEnd(); } else { fakeResponse.Content = new StringContent(tdOutput); } var fakeHandler = new FakeHttpMessageHandler(fakeResponse, td.inpFilename); HttpClient httpClient = new HttpClient(fakeHandler); CAPI c = new CAPI(td.inpFilename, null, 3, httpClient); string morphofeature = null; if (td.endpoint.IndexOf("morphology") != -1) { morphofeature = td.endpoint.Remove(0, td.endpoint.IndexOf("/")); td.endpoint = td.endpoint.Remove(td.endpoint.IndexOf("/")); } try { switch (td.endpoint) { case "categories": result = c.Categories(tdInputData); break; case "entities": result = c.Entity(tdInputData); break; case "entities/linked": result = c.EntitiesLinked(tdInputData); break; case "info": result = c.Info(); break; case "language": result = c.Entity(tdInputData); break; case "morphology": result = c.Morphology(tdInputData, morphofeature); break; case "name": if (td.inpFilename.Contains("matched")) { result = c.MatchedName(tdInputData); } else { result = c.TranslatedName(tdInputData); } break; case "ping": result = c.Ping(); break; case "relationships": result = c.Relationships(tdInputData); break; case "sentences": result = c.Sentences(tdInputData); break; case "sentiment": result = c.Sentiment(tdInputData); break; case "tokens": result = c.Tokens(tdInputData); break; } } catch(RosetteException e) { result = new Dictionary<string, object>(){ {"message", e.Message}, {"code", e.Code}, {"requestId", e.RequestID}, {"file", e.File}, {"line", e.Line} }; } Dictionary<string, object> outputDict = new JavaScriptSerializer().Deserialize<Dictionary<string,object>>(tdOutput); if (result.Keys.Contains("file")) { Assert.AreEqual(result["code"], Convert.ToInt32(tdStatus)); System.Diagnostics.Debug.WriteLine("Status matched"); Console.WriteLine("Status matched"); } else { bool allResultsMatched = true; foreach (string key in outputDict.Keys) { if (key != "requestId") { Assert.IsTrue(result.Keys.Contains(key)); if (result.Keys.Contains(key)) { System.Diagnostics.Debug.WriteLine("Key found"); } else { System.Diagnostics.Debug.WriteLine("Key NOT found"); allResultsMatched = false; } if (result[key] is Object) { Assert.AreEqual(new JavaScriptSerializer().Serialize(result[key]), new JavaScriptSerializer().Serialize(outputDict[key])); if (new JavaScriptSerializer().Serialize(result[key]) == new JavaScriptSerializer().Serialize(outputDict[key])) { System.Diagnostics.Debug.WriteLine("Results Matched"); } else { System.Diagnostics.Debug.WriteLine("Results NOT Matched"); allResultsMatched = false; } } else { Assert.AreEqual(outputDict[key], result[key]); if (outputDict[key] == result[key]) { System.Diagnostics.Debug.WriteLine("Results Matched"); } else { System.Diagnostics.Debug.WriteLine("Results NOT Matched"); allResultsMatched = false; } } } } if (allResultsMatched) { Console.WriteLine("All results matched"); } else { throw new Exception("An Error occurred during the test."); } } } }