-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathV2ResponseUtil.cs
More file actions
141 lines (116 loc) · 5.58 KB
/
V2ResponseUtil.cs
File metadata and controls
141 lines (116 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.PowerShell.PSResourceGet.UtilClasses;
using NuGet.Versioning;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
{
internal class V2ResponseUtil : ResponseUtil
{
#region Members
internal override PSRepositoryInfo Repository { get; set; }
#endregion
#region Constructor
public V2ResponseUtil(PSRepositoryInfo repository) : base(repository)
{
this.Repository = repository;
}
#endregion
#region Overriden Methods
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults, bool isResourceRequestedWithWildcard = false)
{
// in FindHelper:
// serverApi.FindName() -> return responses, and out errRecord
// check outErrorRecord
//
// v2Converter.ConvertToPSResourceInfo(responses) -> return PSResourceResult
// check resourceResult for error, write if needed
string[] responses = responseResults.StringResponse;
foreach (string 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");
yield return new PSResourceResult(returnedObject: null, exception: notFoundException, isTerminatingError: false);
}
foreach (var element in elemList)
{
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);
}
// For V2 resources, specifically PSGallery, return unlisted version resources only when not requested with wildcard name
// Unlisted versions will have a published year as 1900 or earlier.
if (!isResourceRequestedWithWildcard || !psGetInfo.PublishedDate.HasValue || psGetInfo.PublishedDate.Value.Year > 1900)
{
yield return new PSResourceResult(returnedObject: psGetInfo, exception: null, isTerminatingError: false);
}
}
}
}
#endregion
#region V2 Specific Methods
public XmlNode[] ConvertResponseToXML(string httpResponse) {
NuGetVersion emptyVersion = new NuGetVersion("0.0.0.0");
NuGetVersion firstVersion = emptyVersion;
NuGetVersion lastVersion = emptyVersion;
bool shouldFixOrder = true;
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml(httpResponse);
XmlNodeList entryNode = doc.GetElementsByTagName("entry");
XmlNode[] nodes = new XmlNode[entryNode.Count];
for (int i = 0; i < entryNode.Count; i++)
{
XmlNode node = entryNode[i];
nodes[i] = node;
var entryChildNodes = node.ChildNodes;
foreach (XmlElement childNode in entryChildNodes)
{
var entryKey = childNode.LocalName;
if (entryKey.Equals("properties"))
{
var propertyChildNodes = childNode.ChildNodes;
foreach (XmlElement propertyChild in propertyChildNodes)
{
var propertyKey = propertyChild.LocalName;
var propertyValue = propertyChild.InnerText;
if (propertyKey.Equals("NormalizedVersion"))
{
if (!NuGetVersion.TryParse(propertyValue, out NuGetVersion parsedNormalizedVersion))
{
// if a version couldn't be parsed, keep ordering as is.
shouldFixOrder = false;
}
if (i == 0)
{
firstVersion = parsedNormalizedVersion;
}
else
{
// later version element
lastVersion = parsedNormalizedVersion;
}
break; // don't care about rest of the childNode's properties
}
}
break; // don't care about rest of the childNode's keys
}
}
}
// order the array in descending order if not already.
if (shouldFixOrder && firstVersion.CompareTo(lastVersion) < 0)
{
nodes = nodes.Reverse().ToArray();
}
return nodes;
}
#endregion
}
}