Esempio n. 1
0
        public async Task <string> GetDataAsync(CancellationToken cancellationToken)
        {
            _logger.Debug("========= Begin =========");

            try
            {
                var result = await _remoteService.MakeRequest(
                    (service, ct) => service.GetAllPhotosAsync(ct),
                    new RequestOptions
                {
                    RetryCount        = 2,
                    Timeout           = 2,
                    CancellationToken = cancellationToken
                }).ConfigureAwait(false);

                return($"Done. Count: {result.Count}");
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
            }
            finally
            {
                _logger.Debug("========== End ==========");
            }

            return("Failed. See log.");
        }
        public Task <string> StatusCodeAsync(int statusCode, CancellationToken cancellationToken)
        {
            return(_remoteService.MakeRequest(
                       (s, ct) => s.ResponseStatusAsync(statusCode, ct),
                       new RequestOptions
            {
                CancellationToken = cancellationToken,

                // YP: ignore some API errors
                ShouldRetry = ex => ex is ApiException apiException &&
                              (int)apiException.StatusCode >= 500
            }));
        public async Task <string> CheckExpiredSslAsync(CancellationToken cancellationToken)
        {
            try
            {
                return(await _remoteService.MakeRequest(
                           (s, ct) => s.GetHome(ct),
                           new RequestOptions
                {
                    CancellationToken = cancellationToken
                }));
            }
            catch (Exception ex)
            {
                _logger.Error(ex);

                return(ex.Message);
            }
        }
Esempio n. 4
0
 /// <summary>
 ///     Make a safe remote request with getting result and logging errors.
 ///     Returns <see langword="null"/> when an exception was thrown during execution.
 /// </summary>
 /// <param name="remoteService">Instance of <see cref="IRemoteService{TApiService}"/>.</param>
 /// <param name="operation">
 ///     Delegate that encapsulates request operation (call the method from <see cref="TApiService"/>).
 /// </param>
 /// <param name="cancellationToken">Token for canceling the request.</param>
 /// <param name="logger">Instance of <see cref="ILogger"/> (optional).</param>
 /// <typeparam name="TApiService">The type of API service.</typeparam>
 /// <typeparam name="TResult">The type of request result.</typeparam>
 /// <returns>Task with result.</returns>
 public static async Task <TResult> SafeRequest <TApiService, TResult>(
     this IRemoteService <TApiService> remoteService,
     Func <TApiService, CancellationToken, Task <TResult> > operation,
     CancellationToken cancellationToken,
     ILogger?logger = null)
 {
     try
     {
         return(await remoteService.MakeRequest(
                    operation,
                    new RequestOptions
         {
             CancellationToken = cancellationToken
         }));
     }
     catch (Exception ex)
     {
         logger?.Error(ex);
         return(default !);
        public async Task MakeRequest_WithNullOptions_GetsResult()
        {
            var data = await _remoteService.MakeRequest((s, ct) => s.GetData(ct), null);

            Assert.Equal(ResultData, data);
        }