using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Web; namespace RevigoIntegrationTest { class Program { // Usage: revigo example.csv static void Main(string[] args) { // our base URL for the server string sBaseURL = "http://revigo.irb.hr"; // load user data StreamReader dataReader = new StreamReader(args[0]); string sData = dataReader.ReadToEnd(); dataReader.Close(); // First stage - Submit the job to the server HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(sBaseURL + "/StartJob"); oRequest.ContentType = "application/x-www-form-urlencoded"; oRequest.Method = "POST"; // send parameters to the server StreamWriter writer = new StreamWriter(oRequest.GetRequestStream()); writer.Write("goList={0}", HttpUtility.UrlEncode(sData)); writer.Write("&cutoff={0}", (0.7).ToString(CultureInfo.InvariantCulture)); // dot is required decimal separator writer.Write("&valueType=pvalue"); writer.Write("&speciesTaxon=0"); writer.Write("&measure=SIMREL"); writer.Close(); // get the response from the server HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse(); StreamReader reader = new StreamReader(oResponse.GetResponseStream()); string sResponse = reader.ReadLine(); reader.Close(); oResponse.Close(); // parse JSON response int iJobIDPos = sResponse.IndexOf("\"jobid\":", StringComparison.InvariantCultureIgnoreCase); int iJobSeparatorPos = sResponse.IndexOf(',', iJobIDPos + 8); long lJobID = Convert.ToInt64(sResponse.Substring(iJobIDPos + 8, iJobSeparatorPos - iJobIDPos - 8)); // Second stage - Wait for the job to complete bool bWorking = true; while (bWorking) { // request Job status from the server oRequest = (HttpWebRequest)WebRequest.Create(sBaseURL + "/QueryJob"); oRequest.ContentType = "application/x-www-form-urlencoded"; oRequest.Method = "GET"; // send parameters to the server writer = new StreamWriter(oRequest.GetRequestStream()); writer.Write("jobid={0}", lJobID); writer.Write("&type=jstatus"); writer.Close(); // get the response from the server oResponse = (HttpWebResponse)oRequest.GetResponse(); reader = new StreamReader(oResponse.GetResponseStream()); sResponse = reader.ReadLine(); reader.Close(); oResponse.Close(); // parse JSON response int iRunningPos = sResponse.IndexOf("\"running\":", StringComparison.InvariantCultureIgnoreCase); int iSeparatorPos = sResponse.IndexOf(',', iRunningPos + 10); int iRunningFlag = Convert.ToInt32(sResponse.Substring(iRunningPos + 10, iSeparatorPos - iRunningPos - 10)); bWorking = iRunningFlag != 0; Thread.Sleep(1000); } // Third stage - Gather the results // request result table for the BIOLOGICAL_PROCESS namespace oRequest = (HttpWebRequest)WebRequest.Create(sBaseURL + "/QueryJob"); oRequest.ContentType = "application/x-www-form-urlencoded"; oRequest.Method = "GET"; // send parameters to the server writer = new StreamWriter(oRequest.GetRequestStream()); writer.Write("jobid={0}", lJobID); writer.Write("&namespace=1"); writer.Write("&type=table"); writer.Close(); // get the response from the server oResponse = (HttpWebResponse)oRequest.GetResponse(); reader = new StreamReader(oResponse.GetResponseStream()); sResponse = reader.ReadToEnd(); reader.Close(); oResponse.Close(); // and now we have table with results Console.WriteLine(sResponse); Console.WriteLine("Finished!"); Console.ReadLine(); } } }