Skip to content

Commit

Permalink
Proof of concept, Fixes compatibility issues mentioned in ticket #998
Browse files Browse the repository at this point in the history
- WARNING: I am not a F# developer and this code could possibly break alternative NuGet server versions :-)
- "It works on my machine" using the current master of Klondike (https://github.com/themotleyfool/Klondike)
  • Loading branch information
Daniel Mueller committed Aug 19, 2015
1 parent 71b69f3 commit fd394fc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
14 changes: 7 additions & 7 deletions src/Paket.Core/NuGetV2.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Contains NuGet support.
/// Contains NuGet support.
module Paket.NuGetV2

open System
Expand Down Expand Up @@ -30,7 +30,7 @@ type NugetPackageCache =

let rec private followODataLink getUrlContents url =
async {
let! raw = getUrlContents url
let! raw = getUrlContents url acceptXml
let doc = XmlDocument()
doc.LoadXml raw
let feed =
Expand Down Expand Up @@ -83,8 +83,8 @@ let getAllVersionsFromNuGet2(auth,nugetURL,package) =
async {
let url = sprintf "%s/package-versions/%s?includePrerelease=true" nugetURL package
verbosefn "getAllVersionsFromNuGet2 from url '%s'" url
let! raw = safeGetFromUrl(auth, url)
let getUrlContents url = getFromUrl(auth, url)
let! raw = safeGetFromUrl(auth, url, acceptJson)
let getUrlContents url acceptJson = getFromUrl(auth, url, acceptJson)
match raw with
| None -> let! result = getAllVersionsFromNugetOData(getUrlContents, nugetURL, package)
return result
Expand Down Expand Up @@ -215,15 +215,15 @@ let getDetailsFromNuGetViaODataFast auth nugetURL package (version:SemVerInfo) =
async {
try
let url = sprintf "%s/Packages?$filter=Id eq '%s' and NormalizedVersion eq '%s'" nugetURL package (version.Normalize())
let! raw = getFromUrl(auth,url)
let! raw = getFromUrl(auth,url,acceptXml)
if verbose then
tracefn "Response from %s:" url
tracefn ""
tracefn "%s" raw
return parseODataDetails(nugetURL,package,version,raw)
with _ ->
let url = sprintf "%s/Packages?$filter=Id eq '%s' and Version eq '%s'" nugetURL package (version.ToString())
let! raw = getFromUrl(auth,url)
let! raw = getFromUrl(auth,url,acceptXml)
if verbose then
tracefn "Response from %s:" url
tracefn ""
Expand All @@ -238,7 +238,7 @@ let getDetailsFromNuGetViaOData auth nugetURL package (version:SemVerInfo) =
return! getDetailsFromNuGetViaODataFast auth nugetURL package version
with _ ->
let url = sprintf "%s/Packages(Id='%s',Version='%s')" nugetURL package (version.ToString())
let! response = safeGetFromUrl(auth,url)
let! response = safeGetFromUrl(auth,url,acceptXml)

let! raw =
match response with
Expand Down
6 changes: 3 additions & 3 deletions src/Paket.Core/NuGetV3.fs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ let getSearchAPI(auth,nugetUrl) =
| None -> None
| Some v3Path ->
let serviceData =
safeGetFromUrl(auth,v3Path)
safeGetFromUrl(auth,v3Path,acceptJson)
|> Async.RunSynchronously

match serviceData with
Expand All @@ -62,7 +62,7 @@ let internal findVersionsForPackage(auth, nugetURL, package, includingPrerelease
async {
match getSearchAPI(auth,nugetURL) with
| Some url ->
let! response = safeGetFromUrl(auth,sprintf "%s?id=%s&take=%d%s" url package (max maxResults 100000) (if includingPrereleases then "&prerelease=true" else "")) // Nuget is showing old versions first
let! response = safeGetFromUrl(auth,sprintf "%s?id=%s&take=%d%s" url package (max maxResults 100000) (if includingPrereleases then "&prerelease=true" else ""), acceptXml) // Nuget is showing old versions first
match response with
| Some text ->
let versions =
Expand Down Expand Up @@ -94,7 +94,7 @@ let private getPackages(auth, nugetURL, packageNamePrefix, maxResults) = async {
match getSearchAPI(auth,nugetURL) with
| Some url ->
let query = sprintf "%s?q=%s&take=%d" url packageNamePrefix maxResults
let! response = safeGetFromUrl(auth,query)
let! response = safeGetFromUrl(auth,query,acceptJson)
match response with
| Some text -> return extractPackages text
| None -> return [||]
Expand Down
8 changes: 4 additions & 4 deletions src/Paket.Core/RemoteDownload.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ let getSHA1OfBranch origin owner project branch =
match origin with
| ModuleResolver.SingleSourceFileOrigin.GitHubLink ->
let url = sprintf "https://api.github.com/repos/%s/%s/commits/%s" owner project branch
let! document = getFromUrl(None, url)
let! document = getFromUrl(None, url, null)
let json = JObject.Parse(document)
return json.["sha"].ToString()
| ModuleResolver.SingleSourceFileOrigin.GistLink ->
let url = sprintf "https://api.github.com/gists/%s/%s" project branch
let! document = getFromUrl(None, url)
let! document = getFromUrl(None, url, null)
let json = JObject.Parse(document)
let latest = json.["history"].First.["version"]
return latest.ToString()
Expand All @@ -44,7 +44,7 @@ let downloadDependenciesFile(rootPath,parserF,remoteFile:ModuleResolver.Resolved
| ModuleResolver.GistLink ->
rawGistFileUrl remoteFile.Owner remoteFile.Project dependenciesFileName
| ModuleResolver.HttpLink url -> url.Replace(remoteFile.Name,Constants.DependenciesFileName)
let! result = safeGetFromUrl(None,url)
let! result = safeGetFromUrl(None,url,null)

match result with
| Some text when parserF text ->
Expand Down Expand Up @@ -83,7 +83,7 @@ let downloadRemoteFiles(remoteFile:ResolvedSourceFile,destination) = async {
let projectPath = fi.Directory.FullName

let url = sprintf "https://api.github.com/gists/%s" remoteFile.Project
let! document = getFromUrl(None, url)
let! document = getFromUrl(None, url, null)
let json = JObject.Parse(document)
let files = json.["files"] |> Seq.map (fun i -> i.First.["filename"].ToString(), i.First.["raw_url"].ToString())

Expand Down
15 changes: 13 additions & 2 deletions src/Paket.Core/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ open Paket.Logging
open Chessie.ErrorHandling
open Paket.Domain

let acceptXml = "application/atom+xml,application/xml"
let acceptJson = "application/atom+json,application/json"

let notNullOrEmpty = not << System.String.IsNullOrEmpty

type Auth =
{ Username : string
Password : string }
Expand Down Expand Up @@ -168,10 +173,12 @@ let downloadFromUrl (auth:Auth option, url : string) (filePath: string) =
}

/// [omit]
let getFromUrl (auth:Auth option, url : string) =
let getFromUrl (auth:Auth option, url : string, contentType : string) =
async {
try
use client = createWebClient(url,auth)
if notNullOrEmpty contentType then
client.Headers.Add(HttpRequestHeader.Accept, contentType)
let s = client.DownloadStringTaskAsync(Uri(url)) |> Async.AwaitTask
return! s
with
Expand Down Expand Up @@ -200,10 +207,14 @@ let getXmlFromUrl (auth:Auth option, url : string) =
}

/// [omit]
let safeGetFromUrl (auth:Auth option, url : string) =
let safeGetFromUrl (auth:Auth option, url : string, contentType : string) =
async {
try
use client = createWebClient(url,auth)

if notNullOrEmpty contentType then
client.Headers.Add(HttpRequestHeader.Accept, contentType)

let s = client.DownloadStringTaskAsync(Uri(url)) |> Async.AwaitTask
let! raw = s
return Some raw
Expand Down
2 changes: 1 addition & 1 deletion tests/Paket.Tests/NuGetOData/ODataSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ let ``can calculate v3 path``() =

[<Test>]
let ``can read all versions from single page with multiple entries``() =
let getUrlContentsStub _ = async { return File.ReadAllText "NuGetOData/NUnit.xml" }
let getUrlContentsStub _ _ = async { return File.ReadAllText "NuGetOData/NUnit.xml" }

let versions = getAllVersionsFromNugetOData(getUrlContentsStub, fakeUrl, "NUnit")
|> Async.RunSynchronously
Expand Down

0 comments on commit fd394fc

Please sign in to comment.