Skip to content

Commit

Permalink
Merge pull request #1231 from xavierzwirtz/v3api-tomanyrequests
Browse files Browse the repository at this point in the history
Fixed issue with V3 feeds doing api requests even when the paket.lock is fully specified.
  • Loading branch information
forki committed Nov 17, 2015
2 parents 1317255 + 2e700d1 commit 5283820
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 40 deletions.
11 changes: 10 additions & 1 deletion src/Paket.Core/NuGetV2.fs
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,16 @@ let GetVersions root (sources, packageName:PackageName) =

v2Feeds @ v3Feeds
| NugetV3 source ->
[ tryNuGetV3 (source.BasicAuthentication, source.AutoCompleteUrl, packageName) ]
let resp =
async {
let! autoCompleteUrl = PackageSources.getNugetV3Resource source AutoComplete
return!
tryNuGetV3 (source.Authentication |> Option.map toBasicAuth,
autoCompleteUrl,
packageName)
}

[ resp ]
| LocalNuget path -> [ getAllVersionsFromLocalPath (path, packageName, root) ])
|> Seq.toArray
|> Array.map Async.Choice
Expand Down
7 changes: 4 additions & 3 deletions src/Paket.Core/NuGetV3.fs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ type Catalog =

let getRegistration (source : NugetV3Source) (packageName:PackageName) (version:SemVerInfo) =
async {
let url = sprintf "%s%s/%s.json" source.RegistrationUrl (packageName.ToString().ToLower()) version.AsString
let! rawData = safeGetFromUrl (source.BasicAuthentication, url, acceptJson)
let! registrationUrl = PackageSources.getNugetV3Resource source Registration
let url = sprintf "%s%s/%s.json" registrationUrl (packageName.ToString().ToLower()) version.AsString
let! rawData = safeGetFromUrl (source.Authentication |> Option.map toBasicAuth, url, acceptJson)
return
match rawData with
| None -> failwithf "could not get registration data from %s" url
Expand All @@ -166,7 +167,7 @@ let getCatalog url auth =
let getPackageDetails (source:NugetV3Source) (packageName:PackageName) (version:SemVerInfo) : Async<NuGet.NugetPackageCache> =
async {
let! registrationData = getRegistration source packageName version
let! catalogData = getCatalog registrationData.CatalogEntry source.BasicAuthentication
let! catalogData = getCatalog registrationData.CatalogEntry (source.Authentication |> Option.map toBasicAuth)

let dependencies =
if catalogData.DependencyGroups = null then
Expand Down
99 changes: 63 additions & 36 deletions src/Paket.Core/PackageSources.fs
Original file line number Diff line number Diff line change
Expand Up @@ -56,42 +56,69 @@ type NugetV3SourceResourceJSON =
type NugetV3SourceRootJSON =
{ [<JsonProperty("resources")>]
Resources : NugetV3SourceResourceJSON [] }
type NugetV3Source(url : string,
authentication : NugetSourceAuthentication option,
basicAuthentication : Auth option,
resources : Map<string, string>) =

let getResource (resourceType : string) =
match resources |> Map.tryFind (resourceType.ToLower()) with
| None -> failwithf "could not find an %s endpoint" resourceType
| Some x -> x
let searchautoCompleteService = lazy((getResource "SearchAutoCompleteService"))
let registrationsBaseUrl = lazy((getResource "RegistrationsBaseUrl"))
member this.Url = url
member this.Authentication = authentication
member this.BasicAuthentication = basicAuthentication
member this.AutoCompleteUrl = searchautoCompleteService.Value
member this.RegistrationUrl = registrationsBaseUrl.Value

static member loadFromUrl (url : string) (authentication : NugetSourceAuthentication option) =
async {
let basicAuth = authentication |> Option.map toBasicAuth
let! rawData = safeGetFromUrl(basicAuth, url, acceptJson)
let rawData =
match rawData with
| None -> failwithf "couldnt load resources from %s" url
| Some x -> x

let json = JsonConvert.DeserializeObject<NugetV3SourceRootJSON>(rawData)
let resources =
json.Resources
|> Seq.distinctBy(fun x -> x.Type.ToLower())
|> Seq.map(fun x -> x.Type.ToLower(), x.ID)
|> Map.ofSeq

return NugetV3Source(url, authentication,(basicAuth), resources)
}
type NugetV3Source =
{ Url : string
Authentication : NugetSourceAuthentication option }

type NugetV3ResourceType =
| AutoComplete
| Registration
with
member this.AsString =
match this with
| AutoComplete -> "SearchAutoCompleteService"
| Registration -> "RegistrationsBaseUrl"

let mutable private nugetV3Resources : Map<NugetV3Source * NugetV3ResourceType, string> = Map.empty

let getNugetV3Resource (source : NugetV3Source) (resourceType : NugetV3ResourceType) =
async {
let key = (source, resourceType)
return!
match nugetV3Resources |> Map.tryFind key with
| Some x -> async { return x }
| None ->
async {
let basicAuth = source.Authentication |> Option.map toBasicAuth
let! rawData = safeGetFromUrl(basicAuth, source.Url, acceptJson)
let rawData =
match rawData with
| None -> failwithf "couldnt load resources from %s" source.Url
| Some x -> x
let json = JsonConvert.DeserializeObject<NugetV3SourceRootJSON>(rawData)
let resources =
json.Resources
|> Seq.distinctBy(fun x -> x.Type.ToLower())
|> Seq.map(fun x -> x.Type.ToLower(), x.ID)

let newMap =
lock nugetV3Resources (fun() ->
let newMap =
resources
|> Seq.fold(fun m (res, value) ->
let resType =
match res.ToLower() with
| "searchautocompleteservice" -> Some AutoComplete
| "registrationsbaseurl" -> Some Registration
| _ -> None
match resType with
| None -> m
| Some resType ->
m |> Map.add (source, resType) value
) nugetV3Resources

nugetV3Resources <- newMap

newMap)

return
match newMap |> Map.tryFind key with
| Some x -> x
| None ->
failwithf "could not find an %s endpoint for %s" (resourceType.ToString()) source.Url
}

}
let userNameRegex = Regex("username[:][ ]*[\"]([^\"]*)[\"]", RegexOptions.IgnoreCase ||| RegexOptions.Compiled)
let passwordRegex = Regex("password[:][ ]*[\"]([^\"]*)[\"]", RegexOptions.IgnoreCase ||| RegexOptions.Compiled)

Expand Down Expand Up @@ -156,7 +183,7 @@ type PackageSource =
LocalNuget(source)
else
if source.ToLower().EndsWith("v3/index.json") then
NugetV3 (NugetV3Source.loadFromUrl source auth |> Async.RunSynchronously)
NugetV3({ Url = source; Authentication = auth })
else
Nuget({ Url = source; Authentication = auth })
| _ -> match System.Uri.TryCreate(source, System.UriKind.Relative) with
Expand Down

0 comments on commit 5283820

Please sign in to comment.