Ejemplo n.º 1
0
        private string InternalExecute(HttpCommand httpCommand)
        {
            HttpResponseMessage res = null;

            try
            {
                res = httpCommand.Execute();
            }
            catch (AggregateException ex)
            {
                ex.Handle(e => {
                    if (e is HttpRequestException)
                    {
                        throw new ConnectionException("No se pudo conectar");
                    }
                    if (e is TaskCanceledException)
                    {
                        throw new Exceptions.TimeoutException("Tiempo de espera agotado");
                    }
                    throw new Exception("Error en la conexión a la API ");
                });
            }

            switch (res.StatusCode)
            {
            case System.Net.HttpStatusCode.Forbidden:
                throw new InvalidCredentialsException("Api is current unavailable or you don't have access");

            case System.Net.HttpStatusCode.InternalServerError:
                throw new Exception("Server error");

            case System.Net.HttpStatusCode.Unauthorized:
                throw new InvalidCredentialsException("User is not authorized");

            default:
                break;
            }


            if (res.StatusCode != System.Net.HttpStatusCode.Created &&
                res.StatusCode != System.Net.HttpStatusCode.OK)
            {
                JObject jsonObj = new JObject();
                var     resStsr = res.Content.ReadAsStringAsync();
                var     rest    = resStsr.Result;
                //var error = JsonConvert.DeserializeObject<Error>(rest);
                //var sb = new System.Text.StringBuilder();
                //foreach (var item in error.errors)
                //{
                //    sb.AppendLine(item.mensaje);
                //}
                //throw new Exception(sb.ToString());
            }

            var resStr = res.Content.ReadAsStringAsync();

            resStr.Wait();

            return(resStr.Result);
        }
Ejemplo n.º 2
0
        public string Execute(HttpCommand httpCommand)
        {
            try
            {
                var res = policy.Execute(() => InternalExecute(httpCommand));

                return(res);
            }
            catch (BrokenCircuitException e)
            {
                throw new Exception("API seems to be not working. Retry in 1 minute");
            }
            //return Execute(httpCommand, false);
        }