public MultResponse Product(MultRequest request, string trakingId) { log.Trace("this is the service -> Product"); MultResponse mult = new MultResponse(); try { int result = 1; foreach (int i in request.Factors) { mult.Product = result *= i; } log.Trace(HttpStatusCode.OK); if (trakingId != null) { var op = new Operation() { name = "Product", date = DateTime.Now, calculation = string.Join("*", request.Factors) }; JournalList.Add(new KeyValuePair <string, Operation>(trakingId, op)); } } catch (Exception e) { log.Error("Error in the controller Product " + e); log.Error(HttpStatusCode.InternalServerError); throw new Exception(); } return(mult); }
public static void MultiplicationPetition(MultRequest calc, int id) { try { var httpWebRequest = HeaderBuilder("Calculator", "mult", id); string json = JsonConvert.SerializeObject(calc); SendRequest(httpWebRequest, json); MultResponse response = null; HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var result = streamReader.ReadToEnd(); response = JsonConvert.DeserializeObject <MultResponse>(result); } Console.WriteLine("Product: " + response.Product + "\n\npress enter to continue."); Console.ReadKey(); } catch (WebException webex) { WebResponse errResp = webex.Response; using (Stream respStream = errResp.GetResponseStream()) { StreamReader reader = new StreamReader(respStream); string text = reader.ReadToEnd(); } } } // class
public string Multiply(MultRequest petition) { MultResponse result = new MultResponse(); logger.Debug("------- Multiply Method -------"); try { if (petition == null || petition.Multipliers == null) { return(Error400().ErrorMessage.ToString()); } string key = Request.Headers.GetValues("X_Evi_Tracking_Id").FirstOrDefault(); JournalService.Storing(Operations.Mult(petition, result), "Multiplication", key); logger.Debug(Operations.Mult(petition, result)); var hasonServer = JsonConvert.SerializeObject(result); return(hasonServer); } catch (Exception ex) { return(JsonConvert.SerializeObject(Error500(ex))); } }
public void Mult(string trackingId) { #region MultiplicationStuff Console.WriteLine("------Multplication Operation------"); Console.WriteLine("Type the integer multiplication you want to do, multipliers must have 10 or less digits(ex:13 * 12 * 5): "); char symb = '*'; string mult = Console.ReadLine(); mult = CheckingSymbol.HaveOperationSymbol(symb, mult); int[] numbers; if (ParsingComprobation.CanBeParsed(symb, mult, true)) { numbers = ParsingComprobation.Parse(ParsingComprobation.SeparateString(symb, mult)); } else { return; } #endregion #region Connection MultRequest multiply = new MultRequest(); MultResponse result = new MultResponse(); multiply.Multipliers = numbers; HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"{url}mult"); request.Method = "POST"; request.ContentType = "application/json"; if (trackingId != "") { request.Headers.Add("X_Evi_Tracking_Id", trackingId); } using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream())) { var jason = JsonConvert.SerializeObject(multiply); dataStream.Write(jason); dataStream.Close(); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader stRead = new StreamReader(response.GetResponseStream())) { Console.WriteLine("The server operation's result is:"); result = JsonConvert.DeserializeObject <MultResponse>(stRead.ReadToEnd()); Console.WriteLine(result.Result); stRead.Close(); } #endregion }
public void Mult(string trackingId) { #region MultiplicationStuff Console.WriteLine("------Multplication Operation------"); Console.WriteLine("Type the multiplication you want to do(ex:13 * 12 * 5): "); char symb = '*'; string mult = Console.ReadLine(); string[] nums = mult.Split(symb); int[] numbers = new int[nums.Length]; for (int i = 0; i < nums.Length; i++) { numbers[i] = int.Parse(nums[i].Trim()); Console.WriteLine(numbers[i]); } #endregion #region Connection MultRequest multiply = new MultRequest(); MultResponse result = new MultResponse(); multiply.Multipliers = numbers; HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"{url}mult"); request.Method = "POST"; request.ContentType = "application/json"; if (trackingId != "") { request.Headers.Add("X_Evi_Tracking_Id", trackingId); } using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream())) { var jason = JsonConvert.SerializeObject(multiply); dataStream.Write(jason); dataStream.Close(); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader stRead = new StreamReader(response.GetResponseStream())) { Console.WriteLine("The server operation's result is:"); result = JsonConvert.DeserializeObject <MultResponse>(stRead.ReadToEnd()); Console.WriteLine(result.Result); stRead.Close(); } #endregion }
public void MultResponse_CorrectlyFormatsResponseForJournal() { var product = TestHelper.GeneratePositiveNumber(); var expected = product.ToString(); var response = new MultResponse { Product = product }; var formattedResponse = response.GetFormatedResponse(); formattedResponse.Should().Be(expected); }
public string Multiply(MultRequest petition) { int[] nums = petition.Multipliers; MultResponse result = new MultResponse(); result.Result = 1; for (int i = 0; i < nums.Length; i++) { result.Result *= nums[i]; } var hasonServer = JsonConvert.SerializeObject(result); return(hasonServer); }
public void Mult(string Id_Tracking) { Reqmult.Factors = new int[] { }; Console.WriteLine("------- Operation Multiply -------"); Console.WriteLine("Write the operation( Ej: 1*2*3 )"); separator = '*'; try { Console.WriteLine("Operation:"); op = Console.ReadLine(); string[] numbers = op.Trim().Split(separator); Reqmult.Factors = numbers.Select(x => int.Parse(x)).ToArray(); HttpWebRequest req = (HttpWebRequest)WebRequest.Create($"{url}mult"); req.Method = "POST"; req.ContentType = "application/json"; if (Id_Tracking != "") { req.Headers.Add("X_Evi_Tracking_Id", Id_Tracking); } // Opens a stream,converts the request to Json and send it using (StreamWriter sw = new StreamWriter(req.GetRequestStream())) { string jsonRequest = JsonConvert.SerializeObject(Reqmult); sw.Write(jsonRequest); sw.Close(); } MultResponse response = new MultResponse(); HttpWebResponse Response = (HttpWebResponse)req.GetResponse(); using (StreamReader sr = new StreamReader(Response.GetResponseStream(), Encoding.UTF8)) { Console.WriteLine("The server responds: "); response = JsonConvert.DeserializeObject <MultResponse>(sr.ReadToEnd()); Console.WriteLine($"{response.Product}"); sr.Close(); Response.Close(); } } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e.Message); Console.ForegroundColor = ConsoleColor.Gray; logger.Error(e.Message); } }
public string MultPost(MultRequest calc) { var headerRequest = this.Request.Headers; int id = ServerCalc.getIdHeader(headerRequest); MultResponse response = server.ServerCalc.MultiplyingCalculation(calc.Factors); if (id != -1) { var fecha = DateTime.UtcNow; fecha.ToString("O"); Query addquery = new Query(Operations.Mult, string.Join("*", calc.Factors) + "=" + response.Product, fecha); ServerCalc.writeQuery(id, addquery); } return(JsonConvert.SerializeObject(response)); }
public string Mult(MultRequest Reqmult) { MultResponse response = new MultResponse(); string operation = ""; logger.Trace("----- Method Multiply -----"); try { if (Reqmult == null || Reqmult.Factors == null) { return(Error400().ErrorMessage.ToString()); //throw new ArgumentNullException(); } //response.Product = 1; response.Product = Reqmult.Factors.Aggregate(1, (a, b) => a * b); foreach (int item in Reqmult.Factors) { //response.Product *= item; operation += $"{item} * "; } operation = operation.Substring(0, operation.Length - 3); logger.Trace($"{operation} = {response.Product}"); if (Request.Headers.GetValues("X_Evi_Tracking_Id") != null && Request.Headers.GetValues("X_Evi_Tracking_Id").Any()) { string key = Request.Headers.GetValues("X_Evi_Tracking_Id").First(); JournalService.StoreOperation(CreateOperation("Mul", $"{operation} = {response.Product}", DateTime.Now, key)); } string json = JsonConvert.SerializeObject(response); return(json); } catch (ArgumentNullException e) { logger.Error(e.Message); return(e.Message); } catch (Exception) { return(Error500().ErrorMessage.ToString()); } }
public MultResponse Postmult([FromBody] MultRequest factores) { MultResponse multip = new MultResponse { Product = 1 }; foreach (int num in factores.Factors) { multip.Product = multip.Product * num; } if (Request.Headers["X-Evi-Tracking-Id"].Any()) { var identification = Request.Headers["X-Evi-Tracking-Id"]; var currentUser = UsersHandler.KnownOrNot(identification); currentUser.Operations.Add(new MultOperation(factores, multip)); } return(multip); }
public MultResponse mult([FromBody] MultRequest value) { logger.Trace("Service called: mult"); /* An invalid request has been received. * This may mean the HTTP requests and/or the HTTP body may contains some errors which should be fixed * */ if (value.Factors == null) { logger.Error(HttpStatusCode.BadRequest); throw new HttpResponseException(HttpStatusCode.BadRequest); } try { MultResponse result = new MultResponse(); // Execute operation: Mult String forJournal = ""; result.Product = 1; foreach (int number in value.Factors) { result.Product *= number; forJournal += number + " * "; } forJournal = forJournal.Substring(0, forJournal.Length - 1); forJournal += " = " + result.Product; if (Request != null && Request.Headers != null) { var headers = Request.Headers; if (headers.Contains("X-Evi-Tracking-Id")) { // Must store this request’s details inside it’s journal string trackingId = headers.GetValues("X-Evi-Tracking-Id").First(); logger.Info("X-Evi-Tracking-Id = " + trackingId); Operation operation = new Operation { OperationDes = "Mult", Calculation = forJournal, Date = DateTime.Now.ToString("u") }; Journal.store(trackingId, operation); logger.Trace("Mult success!!"); } } return(result); } catch (Exception ex) { logger.Error(ex); /* * An unexpected error condition was triggered which made impossible to fulfill the request. Please try again or contact support * */ throw new HttpResponseException(HttpStatusCode.InternalServerError); } }