Exemple #1
0
        private async Task <IEnumerable <SellbriteOrder> > GetOrdersByPageAsync(DateTime startDateUtc, DateTime endDateUtc, int page, int limit, CancellationToken token)
        {
            var mark   = Mark.CreateNew();
            var orders = new List <SellbriteOrder>();
            var url    = string.Format("{0}?min_ordered_at={1}&max_ordered_at={2}&page={3}&limit={4}", SellbriteEndPoint.OrdersUrl, startDateUtc.FromUtcToRFC3339(), endDateUtc.FromUtcToRFC3339(), page, limit);

            try
            {
                SellbriteLogger.LogStarted(this.CreateMethodCallInfo(url, mark, additionalInfo: this.AdditionalLogInfo()));

                var response = await base.GetAsync <Order[]>(url, token, mark).ConfigureAwait(false);

                if (response.Length != 0)
                {
                    orders.AddRange(response.Select(o => o.ToSvOrder()));
                }

                SellbriteLogger.LogEnd(this.CreateMethodCallInfo(url, mark, methodResult: orders.ToJson(), additionalInfo: this.AdditionalLogInfo()));
            }
            catch (Exception ex)
            {
                var sellbriteException = new SellbriteException(this.CreateMethodCallInfo(url, mark, additionalInfo: this.AdditionalLogInfo()), ex);
                SellbriteLogger.LogTraceException(sellbriteException);
                throw sellbriteException;
            }

            return(orders);
        }
Exemple #2
0
        private async Task <IEnumerable <SellbriteProduct> > GetProductsByPageAsync(DateTime startDateUtc, DateTime endDateUtc, int page, int limit, CancellationToken token)
        {
            var mark     = Mark.CreateNew();
            var products = new List <SellbriteProduct>();
            var url      = string.Format("{0}?min_modified_at={1}&max_modified_at={2}&page={3}&limit={4}", SellbriteEndPoint.ProductsUrl, startDateUtc.FromUtcToRFC3339(), endDateUtc.FromUtcToRFC3339(), page, limit);

            try
            {
                SellbriteLogger.LogStarted(this.CreateMethodCallInfo(url, mark, additionalInfo: this.AdditionalLogInfo()));

                var response = await base.GetAsync(url, token).ConfigureAwait(false);

                products = JsonConvert.DeserializeObject <IEnumerable <Product> >(response).Select(p => p.ToSvProduct()).ToList();

                SellbriteLogger.LogEnd(this.CreateMethodCallInfo(url, mark, methodResult: products.ToJson(), additionalInfo: this.AdditionalLogInfo()));
            }
            catch (Exception ex)
            {
                var sellbriteException = new SellbriteException(this.CreateMethodCallInfo(url, mark, additionalInfo: this.AdditionalLogInfo()), ex);
                SellbriteLogger.LogTraceException(sellbriteException);
                throw sellbriteException;
            }

            return(products);
        }
        /// <summary>
        ///	Retries function until it succeed or failed
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="funcToThrottle"></param>
        /// <param name="onRetryAttempt">Retry attempts</param>
        /// <param name="extraLogInfo"></param>
        /// <param name="onException"></param>
        /// <returns></returns>
        public Task <TResult> ExecuteAsync <TResult>(Func <Task <TResult> > funcToThrottle, Action <Exception, TimeSpan, int> onRetryAttempt, Func <string> extraLogInfo, Action <Exception> onException)
        {
            return(Policy.Handle <SellbriteNetworkException>()
                   .WaitAndRetryAsync(_retryAttempts,
                                      retryCount => TimeSpan.FromSeconds(this.GetDelayBeforeNextAttempt(retryCount)),
                                      (exception, timeSpan, retryCount, context) =>
            {
                onRetryAttempt?.Invoke(exception, timeSpan, retryCount);
            })
                   .ExecuteAsync(async() =>
            {
                try
                {
                    return await funcToThrottle().ConfigureAwait(false);
                }
                catch (Exception exception)
                {
                    if (exception is SellbriteNetworkException)
                    {
                        throw exception;
                    }

                    SellbriteException sellbriteException = null;

                    var exceptionDetails = string.Empty;

                    if (extraLogInfo != null)
                    {
                        exceptionDetails = extraLogInfo();
                    }

                    if (exception is HttpRequestException)
                    {
                        sellbriteException = new SellbriteNetworkException(exceptionDetails, exception);
                    }
                    else
                    {
                        sellbriteException = new SellbriteException(exceptionDetails, exception);
                        onException?.Invoke(sellbriteException);
                    }

                    throw sellbriteException;
                }
            }));
        }