public QueueTaskResponse queue_tasks(string projectId, string token, QueueTaskRequest tasks) { string uri = "https://worker-aws-us-east-1.iron.io:443/2/projects/" + projectId + "/tasks"; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); request.ContentType = "application/json"; request.Headers.Add("Authorization", "OAuth " + token); request.UserAgent = "IronMQ .Net Client"; request.Method = "POST"; // We hand code the JSON payload here. You can automatically convert it, if you prefer //string body = "{\"tasks\": [ { \"code_name\": \"" + worker + "\", \"payload\": \"{\\\"key\\\": \\\"value\\\", \\\"fruits\\\": [\\\"apples\\\", \\\"oranges\\\"]}\"} ] }"; string body = JsonConvert.SerializeObject(tasks); if (body != null) { using (System.IO.StreamWriter write = new System.IO.StreamWriter(request.GetRequestStream())) { write.Write(body); write.Flush(); } } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream())) { //return reader.ReadToEnd(); return JsonConvert.DeserializeObject<QueueTaskResponse>(reader.ReadToEnd()); } }
public Dictionary<string, AlchemyWeightedData> GetResponse(string SearchParam, string Category, bool userIronIO) { var sherlockRankList = new List<AlchemyWeightedData>(); var aggDict = new Dictionary<string, AlchemyWeightedData>(); try { //call google to get the search results - URL GoogleHelper gHelp = new GoogleHelper(); IList<GoogleSearchResult> googleresultList = gHelp.GetSearchResults(SearchParam); IList<AlchemyWeightedData> aggrgAlchemyWeight = new List<AlchemyWeightedData>(); IronMQHelper iron = new IronMQHelper(); var tasksToBeQueued = new QueueTaskRequest(); var listOfTasks = new List<QueueTaskRequest.Task>(); var response1 = iron.CreateIronMQ("SherlockMQProd", IronIOAuthToken, IronIOProjectID); //iterate through the GoogleSearchResult and pass each URL to Alchemy to get a weihted score foreach (var googleResult in googleresultList) { //distribute the Alchemi calls to different IronIO worker threads. //IronMQHelper iron = new IronMQHelper(); var payload = new AlchemyPayloadToIron { Url = googleResult.GSearchResultURL, Category = Category }; var taskToIron = new QueueTaskRequest.Task() { code_name = "alchemy", payload = JsonConvert.SerializeObject(payload) }; listOfTasks.Add(taskToIron); tasksToBeQueued.tasks = listOfTasks; //var alchWtData = CallGetRankedNamedEntities(googleResult.GSearchResultURL, Category); } QueueTaskResponse response = iron.queue_tasks(IronIOProjectID, IronIOAuthToken, tasksToBeQueued); foreach (var ironTask in response.tasks) { string responseFromIronTask = iron.GetTaskResponse(IronIOProjectID, ironTask.id, IronIOAuthToken); IList<AlchemyWeightedData> alchWtData = JsonConvert.DeserializeObject<IList<AlchemyWeightedData>>(responseFromIronTask); foreach (var alchResponse in alchWtData) { if (aggDict.ContainsKey(alchResponse.TextResponse)) { //key already exists - modify var toModifyAlchemyWtData = aggDict[alchResponse.TextResponse]; toModifyAlchemyWtData.RelevanceScore = (toModifyAlchemyWtData.RelevanceScore + alchResponse.RelevanceScore) / 2; aggDict[alchResponse.TextResponse] = toModifyAlchemyWtData; } else { //new key, just add if (aggDict.Count < 3) aggDict.Add(alchResponse.TextResponse, alchResponse); } } } //get output from workers and aggregate the data } catch (Exception e) { } return aggDict; }