-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathNuGetServerResponseUtil.cs
More file actions
91 lines (70 loc) · 3.25 KB
/
NuGetServerResponseUtil.cs
File metadata and controls
91 lines (70 loc) · 3.25 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.PowerShell.PSResourceGet.UtilClasses;
using System;
using System.Collections.Generic;
using System.Xml;
namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
{
internal class NuGetServerResponseUtil : ResponseUtil
{
#region Members
internal override PSRepositoryInfo Repository { get; set; }
#endregion
#region Constructor
public NuGetServerResponseUtil(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);
}
// Unlisted versions will have a published year as 1900 or earlier.
if (!psGetInfo.PublishedDate.HasValue || psGetInfo.PublishedDate.Value.Year > 1900)
{
yield return new PSResourceResult(returnedObject: psGetInfo, exception: null, isTerminatingError: false);
}
}
}
}
#endregion
#region NuGet.Server Specific Methods
public XmlNode[] ConvertResponseToXML(string httpResponse) {
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml(httpResponse);
XmlNodeList elemList = doc.GetElementsByTagName("m:properties");
XmlNode[] nodes = new XmlNode[elemList.Count];
for (int i = 0; i < elemList.Count; i++)
{
nodes[i] = elemList[i];
}
return nodes;
}
#endregion
}
}