Skip to content

Commit 554d032

Browse files
alericksonanamnavi
authored andcommitted
Beginning of impl for converting xml to PSResourceInfo obj (PowerShell#823)
1 parent 72deb4c commit 554d032

File tree

7 files changed

+202
-19
lines changed

7 files changed

+202
-19
lines changed

src/code/FindPSResource.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ protected override void ProcessRecord()
185185

186186
private void ProcessResourceNameParameterSet()
187187
{
188+
// This is for testing/debugging purposes, will be removed later
189+
HttpFindPSResource httpFindPSResource = new HttpFindPSResource();
190+
httpFindPSResource.FindName(Name.FirstOrDefault(), Repository.FirstOrDefault(), Prerelease, out string errRecord);
191+
192+
188193
if (!MyInvocation.BoundParameters.ContainsKey(nameof(Name)))
189194
{
190195
// only cases where Name is allowed to not be specified is if Type or Tag parameters are

src/code/HttpFindPSResource.cs

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1+
using System.Xml;
12
using Microsoft.PowerShell.PowerShellGet.UtilClasses;
23
using NuGet.Versioning;
34
using System.Collections.Generic;
5+
using System.Xml.Schema;
46

57
namespace Microsoft.PowerShell.PowerShellGet.Cmdlets
68
{
79
internal class HttpFindPSResource : IFindPSResource
810
{
911
V2ServerAPICalls v2ServerAPICall = new V2ServerAPICalls();
1012

13+
#region Constructor
14+
15+
public HttpFindPSResource() {}
16+
17+
#endregion
18+
1119
#region Methods
1220

1321
/// <summary>
@@ -36,16 +44,6 @@ public PSResourceInfo FindAll(PSRepositoryInfo repository, bool includePrereleas
3644

3745
/*
3846
// Convert to PSResourceInfo object
39-
if (!PSResourceInfo.TryConvert(
40-
metadataToParse: pkg,
41-
psGetInfo: out PSResourceInfo currentPkg,
42-
repositoryName: repositoryName,
43-
type: _type,
44-
errorMsg: out string errorMsg))
45-
{
46-
errRecord = $"Error parsing IPackageSearchMetadata to PSResourceInfo with message: {errorMsg}";
47-
yield break;
48-
}
4947
*/
5048
return currentPkg;
5149
}
@@ -148,11 +146,25 @@ public PSResourceInfo FindCommandName(string[] commandNames, PSRepositoryInfo re
148146
/// - Include prerelease: http://www.powershellgallery.com/api/v2/FindPackagesById()?id='PowerShellGet'
149147
/// Implementation Note: Need to filter further for latest version (prerelease or non-prerelease dependening on user preference)
150148
/// </summary>
151-
public PSResourceInfo FindName(string packageName, PSRepositoryInfo repository, bool includePrerelease, out string errRecord)
149+
/// // Note, change repository back to PSRepositoryInfo
150+
public PSResourceInfo FindName(string packageName, string repository, bool includePrerelease, out string errRecord)
152151
{
153152
// Same API calls for both prerelease and non-prerelease
154153
var response = v2ServerAPICall.FindName(packageName, repository, out errRecord);
155154

155+
var elemList = ConvertResponseToXML(response);
156+
157+
// Loop through and try to convert each xml entry into a PSResourceInfo object
158+
for (int i = 0; i < elemList.Length; i++)
159+
{
160+
PSResourceInfo.TryConvertFromXml(
161+
elemList[i],
162+
out PSResourceInfo2 psGetInfo,
163+
"PSGallery",
164+
null,
165+
out string errorMsg);
166+
}
167+
156168
PSResourceInfo currentPkg = null;
157169
if (!string.IsNullOrEmpty(errRecord))
158170
{
@@ -366,6 +378,27 @@ public PSResourceInfo FindNamesAndVersionGlobbing(string[] packageNames, Version
366378
}
367379

368380

381+
#endregion
382+
383+
#region HelperMethods
384+
385+
// TODO: in progress
386+
public XmlNode[] ConvertResponseToXML(string httpResponse) {
387+
388+
//Create the XmlDocument.
389+
XmlDocument doc = new XmlDocument();
390+
doc.LoadXml(httpResponse);
391+
392+
XmlNodeList elemList = doc.GetElementsByTagName("m:properties");
393+
394+
XmlNode[] nodes = new XmlNode[elemList.Count];
395+
for (int i=0; i<elemList.Count; i++)
396+
{
397+
nodes[i] = elemList[i];
398+
}
399+
return nodes;
400+
}
401+
369402
#endregion
370403
}
371404
}

src/code/IFindPSResource.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public interface IFindPSResource
5454
/// - Include prerelease: http://www.powershellgallery.com/api/v2/FindPackagesById()?id='PowerShellGet'
5555
/// Implementation Note: Need to filter further for latest version (prerelease or non-prerelease dependening on user preference)
5656
/// </summary>
57-
PSResourceInfo FindName(string packageName, PSRepositoryInfo repository, bool includePrerelease, out string errRecord);
57+
/// // TODO: change repository from string to PSRepositoryInfo
58+
PSResourceInfo FindName(string packageName, string repository, bool includePrerelease, out string errRecord);
5859

5960
/// <summary>
6061
/// Find method which allows for searching for single name with wildcards and returns latest version.

src/code/IServerAPICalls.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ public interface IServerAPICalls
9292
/// - Include prerelease: http://www.powershellgallery.com/api/v2/FindPackagesById()?id='PowerShellGet'
9393
/// Implementation Note: Need to filter further for latest version (prerelease or non-prerelease dependening on user preference)
9494
/// </summary>
95-
string FindName(string packageName, PSRepositoryInfo repository, out string errRecord);
95+
/// // TODO: change repository from string to PSRepositoryInfo
96+
string FindName(string packageName, string repository, out string errRecord);
9697

9798
/// <summary>
9899
/// Find method which allows for searching for single name with version range.
@@ -114,7 +115,6 @@ public interface IServerAPICalls
114115
/// Implementation Note: filter additionally and verify ONLY package name was a match.
115116
/// </summary>
116117
string FindNameGlobbingWithPrerelease(string packageName, PSRepositoryInfo repository, out string errRecord);
117-
{
118118

119119

120120
/// <summary>

src/code/PSResourceInfo.cs

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Xml;
12
// Copyright (c) Microsoft Corporation. All rights reserved.
23
// Licensed under the MIT License.
34

@@ -9,6 +10,7 @@
910
using System.Globalization;
1011
using System.Linq;
1112
using System.Management.Automation;
13+
using System.Xml.Serialization;
1214

1315
using Dbg = System.Diagnostics.Debug;
1416

@@ -535,7 +537,7 @@ public static bool TryConvert(
535537
licenseUri: ParseMetadataLicenseUri(metadataToParse),
536538
name: ParseMetadataName(metadataToParse),
537539
packageManagementProvider: null,
538-
powershellGetFormatVersion: null,
540+
powershellGetFormatVersion: null,
539541
prerelease: ParsePrerelease(metadataToParse),
540542
projectUri: ParseMetadataProjectUri(metadataToParse),
541543
publishedDate: ParseMetadataPublishedDate(metadataToParse),
@@ -560,6 +562,45 @@ public static bool TryConvert(
560562
}
561563
}
562564

565+
// TODO: in progress
566+
// write a serializer
567+
public static bool TryConvertFromXml(
568+
XmlNode entry, // XmlDocument doc that is already loaded
569+
out PSResourceInfo2 psGetInfo,
570+
string repositoryName,
571+
ResourceType? type,
572+
out string errorMsg)
573+
{
574+
psGetInfo = null;
575+
errorMsg = String.Empty;
576+
577+
if (entry == null)
578+
{
579+
errorMsg = "TryConvertXmlToPSResourceInfo: Invalid XmlNodeList object. Object cannot be null.";
580+
return false;
581+
}
582+
583+
try
584+
{
585+
//XmlNodeList elemList = doc.GetElementsByTagName("m:properties");
586+
587+
var xNodeReader = new XmlNodeReader(entry);
588+
var xmlSerializer = new XmlSerializer(typeof(PSResourceInfo2));
589+
psGetInfo = xmlSerializer.Deserialize(xNodeReader) as PSResourceInfo2;
590+
591+
// Note: still need to add some info to the psGetInfo obj,
592+
return true;
593+
}
594+
catch (Exception ex)
595+
{
596+
errorMsg = string.Format(
597+
CultureInfo.InvariantCulture,
598+
@"TryReadPSGetInfo: Cannot parse PSResourceInfo from XmlNode with error: {0}",
599+
ex.Message);
600+
return false;
601+
}
602+
}
603+
563604
#endregion
564605

565606
#region Private static methods
@@ -949,6 +990,94 @@ private PSObject ConvertToCustomObject()
949990

950991
#endregion
951992

993+
// TODO: tmp class for testing/debugging
994+
#region PSResourceInfo2
995+
996+
public sealed class PSResourceInfo2
997+
{
998+
#region Properties
999+
1000+
public string Author { get; set;}
1001+
public string Copyright { get; set; }
1002+
public string Description { get; set;}
1003+
public string IconUri { get; set; }
1004+
public ResourceIncludes Includes { get; set;}
1005+
public DateTime? InstalledDate { get; set; }
1006+
public string InstalledLocation { get; set; }
1007+
public bool IsPrerelease { get; set;}
1008+
public string LicenseUri { get; set;}
1009+
public string Name { get; set;}
1010+
public string PackageManagementProvider { get; set;}
1011+
public string PowerShellGetFormatVersion { get; set;}
1012+
public string Prerelease { get; set;}
1013+
public string ProjectUri { get; set;}
1014+
public DateTime? PublishedDate { get; set;}
1015+
public string ReleaseNotes { get; set;}
1016+
public string Repository { get; set; }
1017+
public string RepositorySourceLocation { get; set; }
1018+
public string[] Tags { get; set;}
1019+
public ResourceType Type { get; set;}
1020+
public DateTime? UpdatedDate { get; set;}
1021+
public Version Version { get; set;}
1022+
1023+
#endregion
1024+
1025+
#region Constructors
1026+
1027+
private PSResourceInfo2() { }
1028+
1029+
private PSResourceInfo2(
1030+
string author,
1031+
string copyright,
1032+
string description,
1033+
string iconUri,
1034+
ResourceIncludes includes,
1035+
DateTime? installedDate,
1036+
string installedLocation,
1037+
bool isPrelease,
1038+
string licenseUri,
1039+
string name,
1040+
string packageManagementProvider,
1041+
string powershellGetFormatVersion,
1042+
string prerelease,
1043+
string projectUri,
1044+
DateTime? publishedDate,
1045+
string releaseNotes,
1046+
string repository,
1047+
string repositorySourceLocation,
1048+
string[] tags,
1049+
ResourceType type,
1050+
DateTime? updatedDate,
1051+
Version version)
1052+
{
1053+
Author = author ?? string.Empty;
1054+
Copyright = copyright ?? string.Empty;
1055+
Description = description ?? string.Empty;
1056+
IconUri = iconUri;
1057+
Includes = includes ?? new ResourceIncludes();
1058+
InstalledDate = installedDate;
1059+
InstalledLocation = installedLocation ?? string.Empty;
1060+
IsPrerelease = isPrelease;
1061+
LicenseUri = licenseUri;
1062+
Name = name ?? string.Empty;
1063+
PackageManagementProvider = packageManagementProvider ?? string.Empty;
1064+
PowerShellGetFormatVersion = powershellGetFormatVersion ?? string.Empty;
1065+
Prerelease = prerelease ?? string.Empty;
1066+
ProjectUri = projectUri;
1067+
PublishedDate = publishedDate;
1068+
ReleaseNotes = releaseNotes ?? string.Empty;
1069+
Repository = repository ?? string.Empty;
1070+
RepositorySourceLocation = repositorySourceLocation ?? string.Empty;
1071+
Tags = tags ?? Utils.EmptyStrArray;
1072+
Type = type;
1073+
UpdatedDate = updatedDate;
1074+
Version = version ?? new Version();
1075+
}
1076+
#endregion
1077+
1078+
}
1079+
#endregion
1080+
9521081
#region Test Hooks
9531082

9541083
public static class TestHooks

src/code/Utils.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ private static void RestoreDirContents(
11901190

11911191
#region HttpRequest
11921192

1193-
public static async Task<JObject> SendRequestAsync(HttpRequestMessage message, HttpClient s_client)
1193+
public static async Task<JObject> SendV3RequestAsync(HttpRequestMessage message, HttpClient s_client)
11941194
{
11951195
try
11961196
{
@@ -1204,6 +1204,20 @@ public static async Task<JObject> SendRequestAsync(HttpRequestMessage message, H
12041204
}
12051205
}
12061206

1207+
public static async Task<string> SendV2RequestAsync(HttpRequestMessage message, HttpClient s_client)
1208+
{
1209+
try
1210+
{
1211+
HttpResponseMessage response = await s_client.SendAsync(message);
1212+
response.EnsureSuccessStatusCode();
1213+
return response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
1214+
}
1215+
catch (HttpRequestException e)
1216+
{
1217+
throw new HttpRequestException("Error occured while trying to retrieve response: " + e.Message);
1218+
}
1219+
}
1220+
12071221
#endregion
12081222
}
12091223

src/code/V2ServerAPICalls.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,10 @@ public string FindCommandNameWithPrerelease(string[] commandNames, PSRepositoryI
165165
/// - Include prerelease: http://www.powershellgallery.com/api/v2/FindPackagesById()?id='PowerShellGet'
166166
/// Implementation Note: Need to filter further for latest version (prerelease or non-prerelease dependening on user preference)
167167
/// </summary>
168-
public string FindName(string packageName, PSRepositoryInfo repository, out string errRecord) {
168+
/// // TODO: change repository from string to PSRepositoryInfo
169+
public string FindName(string packageName, string repository, out string errRecord) {
169170
// Make sure to include quotations around the package name
170-
var requestUrlV2 = $"{repository.Uri}/FindPackagesById()?id='{packageName}'";
171+
var requestUrlV2 = $"{repository}/FindPackagesById()?id='{packageName}'";
171172

172173
return HttpRequestCall(requestUrlV2, out errRecord);
173174
}
@@ -284,7 +285,7 @@ private static string HttpRequestCall(string requestUrlV2, out string errRecord)
284285
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrlV2);
285286

286287
// We can have this return a Task, or the response (json string)
287-
var response = Utils.SendRequestAsync(request, s_client).GetAwaiter().GetResult();
288+
var response = Utils.SendV2RequestAsync(request, s_client).GetAwaiter().GetResult();
288289

289290
// Do we want to check if response is 200?
290291
// response will be json metadata object that will get returned

0 commit comments

Comments
 (0)