/*
         * CONSUMO REST API
         */

        protected async Task <TData> RemoteRequestAsync <TData>(Task <TData> task)
            where TData : HttpResponseMessage,
        new()
        {
            TData data = new TData();

            if (!IsConnected)
            {
                var strngResponse = "No hay una conexión de red";
                data.StatusCode = HttpStatusCode.BadRequest;
                data.Content    = new StringContent(strngResponse);

                _userDialogs.Toast(strngResponse, TimeSpan.FromSeconds(3));
                return(data);
            }

            IsReachable = await _connectivity.IsReachable(GlobalSetting.ApiHostName);

            if (!IsReachable)
            {
                var strngResponse = "No fue posible acceder al servidor";
                data.StatusCode = HttpStatusCode.BadRequest;
                data.Content    = new StringContent(strngResponse);

                _userDialogs.Toast(strngResponse, TimeSpan.FromSeconds(3));
                return(data);
            }

            data = await Policy
                   .Handle <WebException>()
                   .Or <ApiException>()
                   .Or <TaskCanceledException>()
                   .WaitAndRetryAsync
                   (
                retryCount: 1,
                sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))
                   )
                   .ExecuteAsync(async() =>
            {
                var result = await task;

                //Logout the user
                if (result.StatusCode == HttpStatusCode.Unauthorized)
                {
                    ClearAllRunningTask();
                    // await _userDialogs.AlertAsync("No se pudo validar su sesión. Por favor, vuelva a ingresar sus credenciales de acceso.");
                    MessagingCenter.Send <ApiManager>(this, MessagingKeys.ExpiredSession);
                }
                runningTasks.Remove(task.Id);

                return(result);
            });

            return(data);
        }
예제 #2
0
 public async Task <bool> IsReachableHost(string host, int msTimeout)
 {
     return(await connectivity.IsReachable(host, msTimeout));
 }