Esempio n. 1
0
        /// <summary>
        /// Downloads a resource from specified url to a destination file
        /// </summary>
        /// <param name="url">Url to download</param>
        /// <param name="fileName">Destination file path</param>
        /// <param name="attempts">Number of times to attempt the download</param>
        /// <param name="cancellationToken">Cancellation token</param>
        private async Task DownloadToFileAsync(string url, string fileName, int attempts, CancellationToken cancellationToken)
        {
            if (attempts < 1)
            {
                throw new ArgumentException("Must attempt at least one time", nameof(attempts));
            }

            for (int i = 0; i < attempts; i++)
            {
                try
                {
                    using (Stream libraryStream = await _requestHandler.GetStreamAsync(url, cancellationToken).ConfigureAwait(false))
                    {
                        await FileHelpers.SafeWriteToFileAsync(fileName, libraryStream, cancellationToken).ConfigureAwait(false);

                        break;
                    }
                }
                catch (ResourceDownloadException)
                {
                    // rethrow last exception
                    if (i == attempts - 1)
                    {
                        throw;
                    }
                }

                await Task.Delay(200).ConfigureAwait(false);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Downloads a resource from specified url to a destination file
 /// </summary>
 /// <param name="url"></param>
 /// <param name="fileName"></param>
 /// <param name="cancellationToken"></param>
 /// <returns></returns>
 private async Task DownloadToFileAsync(string url, string fileName, CancellationToken cancellationToken)
 {
     using (Stream libraryStream = await _requestHandler.GetStreamAsync(url, cancellationToken))
     {
         await FileHelpers.SafeWriteToFileAsync(fileName, libraryStream, cancellationToken);
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Gets JSON via a get request to the provided URL and converts it to a JObject
        /// </summary>
        /// <param name="webRequestHandler"></param>
        /// <param name="url"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static async Task <JObject> GetJsonObjectViaGetAsync(this IWebRequestHandler webRequestHandler, string url, CancellationToken cancellationToken)
        {
            JObject result = null;

            using (Stream stream = await webRequestHandler.GetStreamAsync(url, cancellationToken))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    string jsonText = await reader.ReadToEndAsync();

                    result = await Task.Factory.StartNew(() => ((JObject)JsonConvert.DeserializeObject(jsonText)),
                                                         cancellationToken,
                                                         TaskCreationOptions.None,
                                                         TaskScheduler.Default);
                }
            }

            return(result);
        }