Skip to content

Commit

Permalink
ACR Integration: Find-PSResource methods and response class (#1499)
Browse files Browse the repository at this point in the history
  • Loading branch information
alerickson authored Jan 2, 2024
1 parent b7eae30 commit 3df7063
Show file tree
Hide file tree
Showing 5 changed files with 661 additions and 310 deletions.
70 changes: 28 additions & 42 deletions src/code/ACRResponseUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

using Microsoft.PowerShell.PSResourceGet.UtilClasses;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Text.Json;

namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
{
Expand All @@ -26,67 +27,52 @@ public ACRResponseUtil(PSRepositoryInfo repository) : base(repository)
#endregion

#region Overriden Methods

public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults)
{
// in FindHelper:
// serverApi.FindName() -> return responses, and out errRecord
// check outErrorRecord
//
// v2Converter.ConvertToPSResourceInfo(responses) -> return PSResourceResult
// acrConverter.ConvertToPSResourceInfo(responses) -> return PSResourceResult
// check resourceResult for error, write if needed
string[] responses = responseResults.StringResponse;

foreach (string response in responses)
Hashtable[] responses = responseResults.HashtableResponse;
foreach (Hashtable response in responses)
{
var elemList = ConvertResponseToXML(response);
if (elemList.Length == 0)
{
// this indicates we got a non-empty, XML response (as noticed for V2 server) but it's not a response that's meaningful (contains 'properties')
Exception notFoundException = new ResourceNotFoundException("Package does not exist on the server");
string responseConversionError = String.Empty;
PSResourceInfo pkg = null;

yield return new PSResourceResult(returnedObject: null, exception: notFoundException, isTerminatingError: false);
}
string packageName = string.Empty;
string packageMetadata = null;

foreach (var element in elemList)
foreach (DictionaryEntry entry in response)
{
if (!PSResourceInfo.TryConvertFromXml(element, out PSResourceInfo psGetInfo, Repository, out string errorMsg))
{
Exception parseException = new XmlParsingException(errorMsg);

yield return new PSResourceResult(returnedObject: null, exception: parseException, isTerminatingError: false);
}
packageName = (string)entry.Key;
packageMetadata = (string)entry.Value;
}

// Unlisted versions will have a published year as 1900 or earlier.
if (!psGetInfo.PublishedDate.HasValue || psGetInfo.PublishedDate.Value.Year > 1900)
try
{
using (JsonDocument pkgVersionEntry = JsonDocument.Parse(packageMetadata))
{
yield return new PSResourceResult(returnedObject: psGetInfo, exception: null, isTerminatingError: false);
PSResourceInfo.TryConvertFromACRJson(packageName, pkgVersionEntry, out pkg, Repository, out responseConversionError);
}
}
}
}

#endregion

#region V2 Specific Methods

public XmlNode[] ConvertResponseToXML(string httpResponse)
{

//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml(httpResponse);
catch (Exception e)
{
responseConversionError = e.Message;
}

XmlNodeList elemList = doc.GetElementsByTagName("m:properties");
if (!String.IsNullOrEmpty(responseConversionError))
{
yield return new PSResourceResult(returnedObject: null, new ConvertToPSResourceException(responseConversionError), isTerminatingError: false);
}

XmlNode[] nodes = new XmlNode[elemList.Count];
for (int i = 0; i < elemList.Count; i++)
{
nodes[i] = elemList[i];
yield return new PSResourceResult(returnedObject: pkg, exception: null, isTerminatingError: false);
}

return nodes;
}

#endregion

}
}
Loading

0 comments on commit 3df7063

Please sign in to comment.