Ejemplo n.º 1
0
        public SqrtResponse Sqrt(SqrtRequest request, string trakingId)
        {
            log.Trace("this is the service -> Sqrt");
            SqrtResponse sqrt = new SqrtResponse();

            try
            {
                sqrt.Square = (int)Math.Sqrt(request.Number);
                log.Trace(HttpStatusCode.OK);
                if (trakingId != null)
                {
                    var op = new Operation()
                    {
                        name        = "Sqrt",
                        date        = DateTime.Now,
                        calculation = string.Join("^", request.Number)
                    };
                    JournalList.Add(new KeyValuePair <string, Operation>(trakingId, op));
                }
            }
            catch (Exception e)
            {
                log.Error("Error in the controller Sqrt " + e);
                log.Error(HttpStatusCode.InternalServerError);
                throw new Exception();
            }
            return(sqrt);
        }
Ejemplo n.º 2
0
        public SqrtResponse sqrt([FromBody] SqrtRequest value)
        {
            logger.Trace("Service called: sqrt");

            /* 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 == null)
            {
                logger.Error(HttpStatusCode.BadRequest);
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            try
            {
                SqrtResponse result = new SqrtResponse();

                // Execute operation: Sub
                String forJournal = "";

                result.Square = Math.Sqrt(value.Number);
                forJournal    = "√" + value.Number + " = " + result.Square;


                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 = "Sqrt",
                            Calculation  = forJournal,
                            Date         = DateTime.Now.ToString("u")
                        };

                        Journal.store(trackingId, operation);
                        logger.Trace("Sqrt 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);
            }
        }
        public IHttpActionResult Square(SqrtRequest request)
        {
            string dataJournal = "";

            IEnumerable <string> headerValues;

            logger.Trace("Service called: square root");

            // 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 (request == null)
            {
                logger.Error(HttpStatusCode.BadRequest);

                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            try
            {
                SqrtResponse result = new SqrtResponse();


                // Execute operation: Square root
                result.Square = Math.Sqrt(request.Number);

                dataJournal = "√" + request.Number + " = " + result.Square;

                logger.Trace("Square success!!");

                // If tracking ID exists create operation instance and store in journal
                if (Request.Headers.TryGetValues("X-Evi-Tracking-Id", out headerValues))
                {
                    string id;

                    id = headerValues.FirstOrDefault();

                    var operation = new Operation()
                    {
                        Name        = "square root",
                        Calculation = dataJournal,
                        Date        = DateTime.Now
                    };

                    Journal.Store(id, operation);
                }

                return(Ok(result));
            }
            catch (Exception ex)
            {
                logger.Error(ex);

                // An unexpected error condition was triggered which made impossible to fulfill the request
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }
Ejemplo n.º 4
0
        public void SqrtResponse_CorrectlyFormatsResponseForJournal()
        {
            var sqrt     = TestHelper.GeneratePositiveNumber();
            var expected = sqrt.ToString();
            var response = new SqrtResponse
            {
                Square = sqrt
            };

            var formattedResponse = response.GetFormatedResponse();

            formattedResponse.Should().Be(expected);
        }
Ejemplo n.º 5
0
        public SqrtResponse Postsqrt([FromBody] SqrtRequest entry)
        {
            SqrtResponse raiz = new SqrtResponse
            {
                Square = Math.Sqrt(entry.Number)
            };

            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 SqrtOperation(entry, raiz));
            }
            return(raiz);
        }
Ejemplo n.º 6
0
 public override Task <SqrtResponse> Sqrt(SqrtRequest request, ServerCallContext context)
 {
     if (request.Number >= 0)
     {
         var res = new SqrtResponse()
         {
             SquareRoot = Math.Sqrt(request.Number)
         };
         return(Task.FromResult(res));
     }
     else
     {
         throw new RpcException(new Status(StatusCode.InvalidArgument,
                                           $"number {request.Number} has to be greater or equal than 0"));
     }
 }
Ejemplo n.º 7
0
        public string SqrtPost(SqrtRequest calc)
        {
            var headerRequest = this.Request.Headers;
            int id            = ServerCalc.getIdHeader(headerRequest);

            SqrtResponse response = server.ServerCalc.SquareRooCalculation(calc.Number);

            if (id != -1)
            {
                var fecha = DateTime.UtcNow;
                fecha.ToString("O");
                Query addquery = new Query(Operations.Sqrt, $"√{calc.Number}={response.Square}", fecha);
                ServerCalc.writeQuery(id, addquery);
            }

            return(JsonConvert.SerializeObject(response));
        }
Ejemplo n.º 8
0
        public static async Task <SqrtResponse> Execute(string host, int port, SqrtRequest sqrtRequest)
        {
            var clientCert = File.ReadAllText("ssl/client.crt");
            var clientKey  = File.ReadAllText("ssl/client.key");
            var caCrt      = File.ReadAllText("ssl/ca.crt");

            var channelCredentials = new SslCredentials(caCrt, new KeyCertificatePair(clientCert, clientKey));

            Channel      channel  = null;
            SqrtResponse response = null;

            try
            {
                channel = new Channel(host, port, channelCredentials);

                await channel.ConnectAsync();

                var client = new SqrtService.SqrtServiceClient(channel);

                response = await client.SqrtAsync(sqrtRequest, deadline : DateTime.UtcNow.AddMilliseconds(5000));
            }
            catch (RpcException ex) when(ex.Status.StatusCode == StatusCode.InvalidArgument)
            {
                Console.WriteLine($"RPC Exception: {ex}");
                Console.WriteLine($"Status Code: {ex.Status.StatusCode}");
                Console.WriteLine($"Status Detail: {ex.Status.Detail}");
            }
            catch (Exception)
            {
                // log
                throw;
            }
            finally
            {
                if (channel != null)
                {
                    await channel.ShutdownAsync();
                }
            }

            return(response);
        }