-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathSampleRequestManager.cs
More file actions
144 lines (121 loc) · 4.79 KB
/
SampleRequestManager.cs
File metadata and controls
144 lines (121 loc) · 4.79 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
142
143
144
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
using System.Xml.Linq;
namespace Amazon.Lambda.TestTool.SampleRequests;
/// <summary>
/// This class manages the sample Lambda input requests. This includes the pre-canned requests and saved requests.
/// </summary>
public class SampleRequestManager(string preferenceDirectory)
{
public const string SavedRequestGroup = "Saved Requests";
public const string SavedRequestDirectory = "SavedRequests";
public IDictionary<string, IList<LambdaRequest>> GetSampleRequests()
{
var content = GetEmbeddedResource("manifest.xml");
XDocument xmlDoc = XDocument.Parse(content);
var query = from item in xmlDoc.Descendants("request")
select new
{
Name = item.Element("name")!.Value,
Filename = item.Element("filename")!.Value
};
var requests = from item in xmlDoc.Descendants("request")
select new LambdaRequest
{
Group = item.Attribute("category")?.Value ?? string.Empty,
Name = item.Element("name")?.Value ?? string.Empty,
Filename = item.Element("filename")?.Value ?? string.Empty,
};
var hash = new Dictionary<string, IList<LambdaRequest>>();
foreach (var request in requests)
{
IList<LambdaRequest>? r;
if (!hash.TryGetValue(request.Group, out r))
{
r = new List<LambdaRequest>();
hash[request.Group] = r;
}
r.Add(request);
}
var savedRequestDirectory = GetSavedRequestDirectory();
if(Directory.Exists(savedRequestDirectory))
{
var savedRequestFiles = Directory.GetFiles(GetSavedRequestDirectory(), "*.json");
if (savedRequestFiles.Length > 0)
{
var savedRequests = new List<LambdaRequest>();
hash[SavedRequestGroup] = savedRequests;
foreach (var file in savedRequestFiles)
{
var r = new LambdaRequest
{
Filename = $"{SavedRequestDirectory}@{Path.GetFileName(file)}",
Group = SavedRequestGroup,
Name = Path.GetFileNameWithoutExtension(file)
};
savedRequests.Add(r);
}
}
}
foreach (var key in hash.Keys.ToList())
{
hash[key] = hash[key].OrderBy(x => x.Name).ToList();
}
return hash;
}
public static bool TryDetermineSampleRequestName(string value, out string? sampleName)
{
sampleName = null;
if (value == null)
return false;
if (!value.StartsWith(SavedRequestDirectory))
return false;
// The minus 6 is for the "@" and the trailing ".json"
sampleName = value.Substring(SavedRequestDirectory.Length + 1, value.Length - SavedRequestDirectory.Length - 6);
return true;
}
public string GetRequest(string name)
{
if(name.StartsWith(SavedRequestDirectory + "@"))
{
name = name.Substring(name.IndexOf("@") + 1);
var savedRequestDirectory = GetSavedRequestDirectory();
var sanitizedName = Path.GetFileName(name);
var path = Path.Combine(savedRequestDirectory, sanitizedName);
return File.ReadAllText(path);
}
return GetEmbeddedResource(name);
}
public string SaveRequest(string name, string content)
{
var filename = $"{name}.json";
var savedRequestDirectory = GetSavedRequestDirectory();
if (!Directory.Exists(savedRequestDirectory))
Directory.CreateDirectory(savedRequestDirectory);
var sanitizedFilename = Path.GetFileName(filename);
File.WriteAllText(Path.Combine(savedRequestDirectory, sanitizedFilename), content);
return $"{SavedRequestDirectory}@{filename}";
}
public string GetSaveRequestRelativePath(string name)
{
var relativePath = $"{SavedRequestDirectory}{Path.DirectorySeparatorChar}{name}";
if (!name.EndsWith(".json"))
relativePath += ".json";
return relativePath;
}
private string GetEmbeddedResource(string name)
{
using (var stream =
typeof(SampleRequestManager).Assembly.GetManifestResourceStream(
"Amazon.Lambda.TestTool.Resources.SampleRequests." + name))
using(var reader = new StreamReader(stream!))
{
return reader.ReadToEnd();
}
}
public string GetSavedRequestDirectory()
{
var path = Path.Combine(preferenceDirectory, SavedRequestDirectory);
return path;
}
}