Skip to content

Commit

Permalink
Nuget V3 API refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorium committed Aug 26, 2016
1 parent 4211376 commit 988c171
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/Paket.Core/NuGetV2.fs
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,8 @@ let GetVersions force root (sources, packageName:PackageName) =
if not (String.containsIgnoreCase "teamcity" source.Url || String.containsIgnoreCase"feedservice.svc" source.Url ) then
yield getVersionsCached "Json" tryGetPackageVersionsViaJson (nugetSource, auth, source.Url, packageName) ]

match NuGetV3.getAllVersionsAPI(source.Authentication,source.Url) with
let apiV3 = NuGetV3.getAllVersionsAPI(source.Authentication,source.Url) |> Async.AwaitTask
match apiV3 |> Async.RunSynchronously with
| None -> v2Feeds
| Some v3Url -> (getVersionsCached "V3" tryNuGetV3 (nugetSource, auth, v3Url, packageName)) :: v2Feeds
| NuGetV3 source ->
Expand Down
53 changes: 28 additions & 25 deletions src/Paket.Core/NuGetV3.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ open Paket.Utils
open Paket.Xml
open Paket.PackageSources
open Paket.Requirements
open Paket.Logging

/// [omit]
type JSONResource =
Expand All @@ -27,10 +28,10 @@ type JSONRootData =
{ Resources : JSONResource [] }

/// [omit]
let private searchDict = new System.Collections.Concurrent.ConcurrentDictionary<_,_>()
let private searchDict = new System.Collections.Concurrent.ConcurrentDictionary<_,System.Threading.Tasks.Task<_>>()

/// [omit]
let private allVersionsDict = new System.Collections.Concurrent.ConcurrentDictionary<_,_>()
let private allVersionsDict = new System.Collections.Concurrent.ConcurrentDictionary<_,System.Threading.Tasks.Task<_>>()

/// Calculates the NuGet v3 URL from a NuGet v2 URL.
let calculateNuGet3Path(nugetUrl:string) =
Expand Down Expand Up @@ -59,37 +60,38 @@ let calculateNuGet2Path(nugetUrl:string) =

/// [omit]
let getSearchAPI(auth,nugetUrl) =
match searchDict.TryGetValue nugetUrl with
| true,v -> v
| _ ->
try
searchDict.GetOrAdd(nugetUrl, fun nugetUrl ->
async {
match calculateNuGet3Path nugetUrl with
| None -> None
| None -> return None
| Some v3Path ->
let source = { Url = v3Path; Authentication = auth }
Some (PackageSources.getNuGetV3Resource source AutoComplete |> Async.RunSynchronously)
with
| _ -> None
|> fun result ->
searchDict.[nugetUrl] <- result
result
let! v3res = PackageSources.getNuGetV3Resource source AutoComplete |> Async.Catch
return
match v3res with
| Choice1Of2 s -> Some s
| Choice2Of2 ex ->
if verbose then traceWarnfn "getAllVersionsAPI: %s" (ex.ToString())
None
} |> Async.StartAsTask)

/// [omit]
let getAllVersionsAPI(auth,nugetUrl) =
match allVersionsDict.TryGetValue nugetUrl with
| true,v -> v
| _ ->
try
allVersionsDict.GetOrAdd(nugetUrl, fun nugetUrl ->
async {
match calculateNuGet3Path nugetUrl with
| None -> None
| None -> return None
| Some v3Path ->
let source = { Url = v3Path; Authentication = auth }
Some (PackageSources.getNuGetV3Resource source AllVersionsAPI |> Async.RunSynchronously)
with
| _ -> None
|> fun result ->
allVersionsDict.[nugetUrl] <- result
result
let! v3res = PackageSources.getNuGetV3Resource source AllVersionsAPI |> Async.Catch
return
match v3res with
| Choice1Of2 s -> Some s
| Choice2Of2 ex ->
if verbose then traceWarnfn "getAllVersionsAPI: %s" (ex.ToString())
None
} |> Async.StartAsTask)


/// [omit]
let extractAutoCompleteVersions(response:string) =
Expand Down Expand Up @@ -153,7 +155,8 @@ let extractPackages(response:string) =
JsonConvert.DeserializeObject<JSONVersionData>(response).Data

let private getPackages(auth, nugetURL, packageNamePrefix, maxResults) = async {
match getSearchAPI(auth,nugetURL) with
let! apiRes = getSearchAPI(auth,nugetURL) |> Async.AwaitTask
match apiRes with
| Some url ->
let query = sprintf "%s?q=%s&take=%d" url packageNamePrefix maxResults
let! response = safeGetFromUrl(auth |> Option.map toBasicAuth,query,acceptJson)
Expand Down
3 changes: 2 additions & 1 deletion src/Paket.Core/PublicAPI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@ type Dependencies(dependenciesFileName: string) =
|> Seq.choose (fun source ->
match source with
| NuGetV2 s ->
match NuGetV3.getSearchAPI(s.Authentication,s.Url) with
let res = NuGetV3.getSearchAPI(s.Authentication,s.Url) |> Async.AwaitTask |> Async.RunSynchronously
match res with
| Some _ -> Some(NuGetV3.FindPackages(s.Authentication, s.Url, searchTerm, maxResults))
| None -> Some(NuGetV2.FindPackages(s.Authentication, s.Url, searchTerm, maxResults))
| NuGetV3 s -> Some(NuGetV3.FindPackages(s.Authentication, s.Url, searchTerm, maxResults))
Expand Down

0 comments on commit 988c171

Please sign in to comment.