Skip to content

Commit 6ab7367

Browse files
authored
Avoid cmdlet stream writes in NuGet FindVersionAsync path
1 parent f800970 commit 6ab7367

1 file changed

Lines changed: 61 additions & 1 deletion

File tree

src/code/NuGetServerAPICalls.cs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,18 @@ public NuGetServerAPICalls (PSRepositoryInfo repository, PSCmdlet cmdletPassedIn
5858
public override Task<FindResults> FindVersionAsync(string packageName, string version, ResourceType type, ConcurrentQueue<ErrorRecord> errorMsgs, ConcurrentQueue<string> warningMsgs, ConcurrentQueue<string> debugMsgs, ConcurrentQueue<string> verboseMsgs)
5959
{
6060
debugMsgs.Enqueue("In NuGetServerAPICalls::FindVersionAsync()");
61-
FindResults findResponse = FindVersion(packageName, version, type, out ErrorRecord errRecord);
61+
var queryBuilder = new NuGetV2QueryBuilder(new Dictionary<string, string>{
62+
{ "id", $"'{packageName}'" },
63+
});
64+
var filterBuilder = queryBuilder.FilterBuilder;
65+
66+
// We need to explicitly add 'Id eq <packageName>' whenever $filter is used, otherwise arbitrary results are returned.
67+
filterBuilder.AddCriterion($"Id eq '{packageName}'");
68+
filterBuilder.AddCriterion($"NormalizedVersion eq '{packageName}'");
69+
70+
var requestUrl = $"{Repository.Uri}/FindPackagesById()?{queryBuilder.BuildQueryString()}";
71+
string response = HttpRequestCallAsync(requestUrl, debugMsgs, out ErrorRecord errRecord);
72+
FindResults findResponse = new FindResults(stringResponse: new string[] { response }, hashtableResponse: emptyHashResponses, responseType: FindResponseType);
6273
if (errRecord != null)
6374
{
6475
errorMsgs.Enqueue(errRecord);
@@ -617,6 +628,55 @@ private HttpContent HttpRequestCallForContent(string requestUrl, out ErrorRecord
617628
return content;
618629
}
619630

631+
/// <summary>
632+
/// Helper method that makes the HTTP request for the NuGet server protocol url passed in for async find APIs.
633+
/// This helper writes diagnostics to the provided debug queue and avoids cmdlet stream writes.
634+
/// </summary>
635+
private string HttpRequestCallAsync(string requestUrl, ConcurrentQueue<string> debugMsgs, out ErrorRecord errRecord)
636+
{
637+
debugMsgs.Enqueue("In NuGetServerAPICalls::HttpRequestCallAsync()");
638+
errRecord = null;
639+
string response = string.Empty;
640+
641+
try
642+
{
643+
debugMsgs.Enqueue($"Request url is: '{requestUrl}'");
644+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
645+
response = SendRequestAsync(request, _sessionClient).GetAwaiter().GetResult();
646+
}
647+
catch (HttpRequestException e)
648+
{
649+
errRecord = new ErrorRecord(
650+
exception: e,
651+
"HttpRequestFallFailure",
652+
ErrorCategory.ConnectionError,
653+
this);
654+
}
655+
catch (ArgumentNullException e)
656+
{
657+
errRecord = new ErrorRecord(
658+
exception: e,
659+
"HttpRequestFallFailure",
660+
ErrorCategory.ConnectionError,
661+
this);
662+
}
663+
catch (InvalidOperationException e)
664+
{
665+
errRecord = new ErrorRecord(
666+
exception: e,
667+
"HttpRequestFallFailure",
668+
ErrorCategory.ConnectionError,
669+
this);
670+
}
671+
672+
if (string.IsNullOrEmpty(response))
673+
{
674+
debugMsgs.Enqueue("Response is empty");
675+
}
676+
677+
return response;
678+
}
679+
620680
#endregion
621681

622682
#region Private Methods

0 commit comments

Comments
 (0)