-
Notifications
You must be signed in to change notification settings - Fork 681
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Protocol] Mirror nuget.org's undocumented protocols (#249)
NuGet.org uses RDF to generate its V3 API. This adds properties that aren't part of the official API, however, certain clients depend on these undocumented properties.
- Loading branch information
1 parent
f4e1d02
commit 55ce835
Showing
17 changed files
with
249 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System.IO; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
|
||
namespace BaGet.Protocol | ||
{ | ||
internal static class HttpClientExtensions | ||
{ | ||
private static readonly JsonSerializer Serializer = JsonSerializer.Create(Settings); | ||
|
||
private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings | ||
{ | ||
DateTimeZoneHandling = DateTimeZoneHandling.Utc, | ||
DateParseHandling = DateParseHandling.DateTimeOffset, | ||
NullValueHandling = NullValueHandling.Ignore, | ||
}; | ||
|
||
public static async Task<ResponseAndResult<T>> DeserializeUrlAsync<T>( | ||
this HttpClient httpClient, | ||
string documentUrl) | ||
{ | ||
using (var response = await httpClient.GetAsync( | ||
documentUrl, | ||
HttpCompletionOption.ResponseHeadersRead)) | ||
{ | ||
if (response.StatusCode != HttpStatusCode.OK) | ||
{ | ||
return new ResponseAndResult<T>( | ||
HttpMethod.Get, | ||
documentUrl, | ||
response.StatusCode, | ||
response.ReasonPhrase, | ||
hasResult: false, | ||
result: default); | ||
} | ||
|
||
using (var stream = await response.Content.ReadAsStreamAsync()) | ||
using (var textReader = new StreamReader(stream)) | ||
using (var jsonReader = new JsonTextReader(textReader)) | ||
{ | ||
return new ResponseAndResult<T>( | ||
HttpMethod.Get, | ||
documentUrl, | ||
response.StatusCode, | ||
response.ReasonPhrase, | ||
hasResult: true, | ||
result: Serializer.Deserialize<T>(jsonReader)); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
|
||
namespace BaGet.Protocol | ||
{ | ||
public class ProtocolException : Exception | ||
{ | ||
public ProtocolException( | ||
string message, | ||
HttpMethod method, | ||
string requestUri, | ||
HttpStatusCode statusCode, | ||
string reasonPhrase) : base(message) | ||
{ | ||
Method = method ?? throw new ArgumentNullException(nameof(method)); | ||
RequestUri = requestUri ?? throw new ArgumentNullException(nameof(requestUri)); | ||
StatusCode = statusCode; | ||
ReasonPhrase = reasonPhrase ?? throw new ArgumentNullException(nameof(reasonPhrase)); | ||
} | ||
|
||
public HttpMethod Method { get; } | ||
public string RequestUri { get; } | ||
public HttpStatusCode StatusCode { get; } | ||
public string ReasonPhrase { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
|
||
namespace BaGet.Protocol | ||
{ | ||
internal class ResponseAndResult<T> | ||
{ | ||
public ResponseAndResult( | ||
HttpMethod method, | ||
string requestUri, | ||
HttpStatusCode statusCode, | ||
string reasonPhrase, | ||
bool hasResult, | ||
T result) | ||
{ | ||
Method = method ?? throw new ArgumentNullException(nameof(method)); | ||
RequestUri = requestUri ?? throw new ArgumentNullException(nameof(requestUri)); | ||
StatusCode = statusCode; | ||
ReasonPhrase = reasonPhrase ?? throw new ArgumentNullException(nameof(reasonPhrase)); | ||
HasResult = hasResult; | ||
Result = result; | ||
} | ||
|
||
public HttpMethod Method { get; } | ||
public string RequestUri { get; } | ||
public HttpStatusCode StatusCode { get; } | ||
public string ReasonPhrase { get; } | ||
public bool HasResult { get; } | ||
public T Result { get; } | ||
|
||
public T GetResultOrThrow() | ||
{ | ||
if (!HasResult) | ||
{ | ||
throw new ProtocolException( | ||
$"The HTTP request failed.{Environment.NewLine}" + | ||
$"Request: {Method} {RequestUri}{Environment.NewLine}" + | ||
$"Response: {(int)StatusCode} {ReasonPhrase}", | ||
Method, | ||
RequestUri, | ||
StatusCode, | ||
ReasonPhrase); | ||
} | ||
|
||
return Result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace BaGet.Protocol | ||
{ | ||
public class AutocompleteContext | ||
{ | ||
public static readonly AutocompleteContext Default = new AutocompleteContext | ||
{ | ||
Vocab = "http://schema.nuget.org/schema#" | ||
}; | ||
|
||
[JsonProperty("@vocab")] | ||
public string Vocab { get; set; } | ||
} | ||
} |
Oops, something went wrong.