66using BaGet . Core ;
77using Microsoft . Azure . Cosmos . Table ;
88using Microsoft . Extensions . Logging ;
9- using Newtonsoft . Json ;
109using NuGet . Versioning ;
1110
1211namespace BaGet . Azure
@@ -64,7 +63,7 @@ public async Task<bool> AddDownloadAsync(
6463 id . ToLowerInvariant ( ) ,
6564 version . ToNormalizedString ( ) . ToLowerInvariant ( ) ) ;
6665
67- var result = await _table . ExecuteAsync ( operation ) ;
66+ var result = await _table . ExecuteAsync ( operation , cancellationToken ) ;
6867 var entity = result . Result as PackageDownloadsEntity ;
6968
7069 if ( entity == null )
@@ -80,6 +79,7 @@ public async Task<bool> AddDownloadAsync(
8079 catch ( StorageException e )
8180 when ( attempt < MaxPreconditionFailures && e . IsPreconditionFailedException ( ) )
8281 {
82+ attempt ++ ;
8383 _logger . LogWarning (
8484 e ,
8585 $ "Retrying due to precondition failure, attempt {{Attempt}} of { MaxPreconditionFailures } ..",
@@ -111,10 +111,9 @@ public async Task<bool> ExistsAsync(
111111 version . ToNormalizedString ( ) . ToLowerInvariant ( ) ,
112112 MinimalColumnSet ) ;
113113
114- var result = await _table . ExecuteAsync ( operation , cancellationToken ) ;
115- var entity = result . Result as PackageEntity ;
114+ var execution = await _table . ExecuteAsync ( operation , cancellationToken ) ;
116115
117- return entity != null ;
116+ return execution . Result is PackageEntity ;
118117 }
119118
120119 public async Task < IReadOnlyList < Package > > FindAsync ( string id , bool includeUnlisted , CancellationToken cancellationToken )
@@ -142,7 +141,7 @@ public async Task<IReadOnlyList<Package>> FindAsync(string id, bool includeUnlis
142141 token = segment . ContinuationToken ;
143142
144143 // Write out the properties for each entity returned.
145- results . AddRange ( segment . Results . Select ( AsPackage ) ) ;
144+ results . AddRange ( segment . Results . Select ( r => r . AsPackage ( ) ) ) ;
146145 }
147146 while ( token != null ) ;
148147
@@ -173,7 +172,7 @@ public async Task<Package> FindOrNullAsync(
173172 return null ;
174173 }
175174
176- return AsPackage ( entity ) ;
175+ return entity . AsPackage ( ) ;
177176 }
178177
179178 public async Task < bool > HardDeletePackageAsync ( string id , NuGetVersion version , CancellationToken cancellationToken )
@@ -212,72 +211,5 @@ private async Task<bool> TryUpdatePackageAsync(TableOperation operation, Cancell
212211
213212 return true ;
214213 }
215-
216- private Package AsPackage ( PackageEntity entity )
217- {
218- var targetFrameworks = JsonConvert . DeserializeObject < List < string > > ( entity . TargetFrameworks )
219- . Select ( f => new TargetFramework { Moniker = f } )
220- . ToList ( ) ;
221-
222- return new Package
223- {
224- Id = entity . Id ,
225- NormalizedVersionString = entity . NormalizedVersion ,
226- OriginalVersionString = entity . OriginalVersion ,
227-
228- Authors = JsonConvert . DeserializeObject < string [ ] > ( entity . Authors ) ,
229- Description = entity . Description ,
230- Downloads = entity . Downloads ,
231- HasReadme = entity . HasReadme ,
232- HasEmbeddedIcon = entity . HasEmbeddedIcon ,
233- IsPrerelease = entity . IsPrerelease ,
234- Language = entity . Language ,
235- Listed = entity . Listed ,
236- MinClientVersion = entity . MinClientVersion ,
237- Published = entity . Published ,
238- RequireLicenseAcceptance = entity . RequireLicenseAcceptance ,
239- SemVerLevel = ( SemVerLevel ) entity . SemVerLevel ,
240- Summary = entity . Summary ,
241- Title = entity . Title ,
242- ReleaseNotes = entity . ReleaseNotes ,
243- IconUrl = ParseUri ( entity . IconUrl ) ,
244- LicenseUrl = ParseUri ( entity . LicenseUrl ) ,
245- ProjectUrl = ParseUri ( entity . ProjectUrl ) ,
246- RepositoryUrl = ParseUri ( entity . RepositoryUrl ) ,
247- RepositoryType = entity . RepositoryType ,
248- Tags = JsonConvert . DeserializeObject < string [ ] > ( entity . Tags ) ,
249- Dependencies = ParseDependencies ( entity . Dependencies ) ,
250- PackageTypes = ParsePackageTypes ( entity . PackageTypes ) ,
251- TargetFrameworks = targetFrameworks ,
252- } ;
253- }
254-
255- private Uri ParseUri ( string input )
256- {
257- return string . IsNullOrEmpty ( input ) ? null : new Uri ( input ) ;
258- }
259-
260- private List < PackageDependency > ParseDependencies ( string input )
261- {
262- return JsonConvert . DeserializeObject < List < DependencyModel > > ( input )
263- . Select ( e => new PackageDependency
264- {
265- Id = e . Id ,
266- VersionRange = e . VersionRange ,
267- TargetFramework = e . TargetFramework ,
268- } )
269- . ToList ( ) ;
270- }
271-
272- private List < PackageType > ParsePackageTypes ( string input )
273- {
274- return JsonConvert . DeserializeObject < List < PackageTypeModel > > ( input )
275- . Select ( e => new PackageType
276- {
277- Name = e . Name ,
278- Version = e . Version
279- } )
280- . ToList ( ) ;
281- }
282214 }
283215}
0 commit comments