1- // Copyright (c) .NET Foundation. All rights reserved.
1+ // Copyright (c) .NET Foundation. All rights reserved.
22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
44using System ;
55using System . Diagnostics ;
66using System . IO ;
7- using System . Net . Http ;
87using System . Threading . Tasks ;
8+ using Azure ;
9+ using Azure . Storage . Blobs ;
10+ using Azure . Storage . Blobs . Models ;
911using Microsoft . Extensions . Logging ;
1012using Newtonsoft . Json ;
1113using NuGet . Services . Metadata . Catalog . Helpers ;
@@ -14,46 +16,43 @@ namespace NuGet.Services.Metadata.Catalog
1416{
1517 public class DownloadsV1JsonClient : IDownloadsV1JsonClient
1618 {
17- private readonly HttpClient _httpClient ;
19+ private readonly BlobClient _blobClient ;
1820 private readonly ILogger < DownloadsV1JsonClient > _logger ;
1921
20- public DownloadsV1JsonClient ( HttpClient httpClient , ILogger < DownloadsV1JsonClient > logger )
22+ public DownloadsV1JsonClient ( BlobClient blobClient , ILogger < DownloadsV1JsonClient > logger )
2123 {
22- _httpClient = httpClient ?? throw new ArgumentNullException ( nameof ( httpClient ) ) ;
24+ _blobClient = blobClient ?? throw new ArgumentNullException ( nameof ( blobClient ) ) ;
2325 _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
2426 }
2527
26- public async Task < DownloadData > ReadAsync ( string url )
28+ public async Task < DownloadData > ReadAsync ( )
2729 {
2830 var downloadData = new DownloadData ( ) ;
29- await ReadAsync ( url , downloadData . SetDownloadCount ) ;
31+ await ReadAsync ( downloadData . SetDownloadCount ) ;
3032 return downloadData ;
3133 }
3234
33- public async Task ReadAsync ( string url , AddDownloadCount addCount )
35+ public async Task ReadAsync ( AddDownloadCount addCount )
3436 {
3537 var stopwatch = Stopwatch . StartNew ( ) ;
3638 var packageCount = 0 ;
37-
39+
3840 await Retry . IncrementalAsync (
3941 async ( ) =>
4042 {
41- _logger . LogInformation ( "Attempting to download {Url}" , url ) ;
42- using ( var response = await _httpClient . GetAsync ( url ) )
43+ _logger . LogInformation ( "Attempting to download {Url}" , _blobClient . Uri . GetLeftPart ( UriPartial . Path ) ) ;
44+ using ( BlobDownloadStreamingResult result = await _blobClient . DownloadStreamingAsync ( ) )
45+ using ( var textReader = new StreamReader ( result . Content ) )
46+ using ( var jsonReader = new JsonTextReader ( textReader ) )
4347 {
44- response . EnsureSuccessStatusCode ( ) ;
45- using ( var textReader = new StreamReader ( await response . Content . ReadAsStreamAsync ( ) ) )
46- using ( var jsonReader = new JsonTextReader ( textReader ) )
48+ DownloadsV1Reader . Load ( jsonReader , ( id , version , count ) =>
4749 {
48- DownloadsV1Reader . Load ( jsonReader , ( id , version , count ) =>
49- {
50- packageCount ++ ;
51- addCount ( id , version , count ) ;
52- } ) ;
53- }
50+ packageCount ++ ;
51+ addCount ( id , version , count ) ;
52+ } ) ;
5453 }
5554 } ,
56- ex => ex is HttpRequestException ,
55+ ex => ex is RequestFailedException ,
5756 maxRetries : 5 ,
5857 initialWaitInterval : TimeSpan . Zero ,
5958 waitIncrement : TimeSpan . FromSeconds ( 20 ) ) ;
0 commit comments