public static List <CustomerData> LoadStreamForProcessing(Stream stream, string fileName, string yourName) { string[] items; CustomerData customerData; var itemsToProcess = new List <CustomerData>(); stream.Position = 0; using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { string line; int value; while (!reader.EndOfStream) { line = reader.ReadLine(); customerData = new CustomerData(); items = line.Split(new char[] { ',' }); if (items.Length > 0) { customerData.customer = items[0]; } if (items.Length > 1) { if (int.TryParse(items[1], out value)) { customerData.value = value; } else { customerData.uploaderror = string.Format("Invalid value {0}", items[1]); } } customerData.id = Guid.NewGuid(); customerData.property = yourName; customerData.action = "order created"; customerData.file = fileName; customerData.uploadstatus = 1; // to be processed itemsToProcess.Add(customerData); } } return(itemsToProcess); }
public async static void SendDataToEvil(Dictionary <CustomerData, EvilReturn> results, CustomerData customerData, int totalBeingProcessed, Guid requestId, Action <Dictionary <CustomerData, EvilReturn>, Guid> finishedAction) { var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://evilapi-env.ap-southeast-2.elasticbeanstalk.com/upload"); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { var json = new JavaScriptSerializer().Serialize(new { property = customerData.property, customer = customerData.customer, action = customerData.action, value = customerData.value, file = customerData.file }); streamWriter.Write(json); } EvilReturn result; try { var httpResponse = await httpWebRequest.GetResponseAsync(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var resultText = streamReader.ReadToEnd(); result = (new JavaScriptSerializer()).Deserialize <EvilReturn>(resultText); } } catch (Exception ex) { result = new EvilReturn() { added = "false", errors = new string[] { ex.Message } }; } var checkResult = await CheckData(customerData, result.hash); customerData.checkResult = checkResult.Status; customerData.checkerror = checkResult.Message; results.Add(customerData, result); if (results.Count == totalBeingProcessed) { if (finishedAction != null) { finishedAction(results, requestId); } } }
public async static Task <CheckResult> CheckData(CustomerData customerData, string hash) { if (string.IsNullOrWhiteSpace(hash)) { return(new CheckResult() { Status = 2, Message = "Empty hash" }); } try { var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://evilapi-env.ap-southeast-2.elasticbeanstalk.com/check?hash=" + hash); httpWebRequest.Method = "GET"; var serializer = (new JavaScriptSerializer()); var httpResponse = await httpWebRequest.GetResponseAsync(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var resultText = streamReader.ReadToEnd(); if (resultText.IndexOf("\"errors\":", StringComparison.CurrentCultureIgnoreCase) == -1) { var goodResult = serializer.Deserialize <CustomerData>(resultText); if (customerData.property == goodResult.property && customerData.customer == goodResult.customer && customerData.action == goodResult.action && customerData.file == goodResult.file && customerData.value == goodResult.value) { return(new CheckResult() { Status = 1 }); // has been run and matched } else { return(new CheckResult() { Status = 3, Message = "Check did not match" }); // has been run but did not match } } else { var evilResult = serializer.Deserialize <EvilReturn>(resultText); return(new CheckResult() { Status = 4, Message = evilResult.errors[0] }); // Invalid check } } } catch (Exception ex) { //results.Add(customerData, new EvilReturn() { added = "false", errors = new string[] { ex.Message } }); // Exception when running the check. return(new CheckResult() { Status = 5, Message = ex.Message }); } }