-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTemplateFactory.cs
More file actions
181 lines (147 loc) · 7.08 KB
/
Copy pathTemplateFactory.cs
File metadata and controls
181 lines (147 loc) · 7.08 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using NSwag;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;
using Vano.Tools.Azure.Model;
namespace Vano.Tools.Azure
{
public static class TemplateFactory
{
public static async Task<IEnumerable<Template>> GetTemplates()
{
List<Template> templates = new List<Template>();
IEnumerable<Template> swaggerTemplates = await GetTemplatesFromSwagger();
templates.AddRange(swaggerTemplates);
IEnumerable<Template> gitHubTemplates = await GetTemplatesFromGitHubRepo();
templates.AddRange(gitHubTemplates);
return templates;
}
#region Swagger
// For more info:
// https://learn.microsoft.com/en-us/rest/api/appservice/
// https://github.com/Azure/azure-rest-api-specs/blob/main/specification/web/resource-manager/readme.md
public static async Task<IEnumerable<Template>> GetTemplatesFromSwagger()
{
List<Template> templates = new List<Template>();
string swaggerTemplatesFolder = Template.GetSwaggerTemplatesFolder();
await DownloadTemplatesFromGitHubRepo(swaggerTemplatesFolder, owner: "Azure", repo: "azure-rest-api-specs", path: "specification/web/resource-manager/Microsoft.Web/stable/2024-11-01");
foreach (string filePath in Directory.GetFiles(swaggerTemplatesFolder))
{
IEnumerable<Template> templatesFromSwaggerFile = await GetTemplatesFromSwagger(filePath);
templates.AddRange(templatesFromSwaggerFile);
}
return templates;
}
public static async Task<IEnumerable<Template>> GetTemplatesFromSwagger(string swaggerFilePath)
{
List<Template> templates = new List<Template>();
try
{
OpenApiDocument document = await OpenApiDocument.FromFileAsync(swaggerFilePath);
foreach (KeyValuePair<string, OpenApiPathItem> pathItem in document.Paths)
{
foreach (KeyValuePair<string, OpenApiOperation> operationItem in pathItem.Value)
{
try
{
Template template = new Template()
{
Category = "Swagger [" + operationItem.Value.Tags.FirstOrDefault() + "]",
Name = operationItem.Value.OperationId,
Summary = operationItem.Value.Summary,
Verb = operationItem.Key.ToUpper(),
Path = pathItem.Key,
Body = operationItem.Value.Parameters.Where(p => p.Kind == OpenApiParameterKind.Body).FirstOrDefault()?.ToSampleJson()?.ToString(),
};
templates.Add(template);
}
catch
{
// DO NOTHING
}
}
}
}
catch
{
// DO NOTHING
}
return templates;
}
#endregion
#region GitHub Templates
public static async Task<IEnumerable<Template>> GetTemplatesFromGitHubRepo()
{
List<Template> templates = new List<Template>();
string gitHubTemplatesFolder = Template.GetGitHubTemplatesFolder();
await DownloadTemplatesFromGitHubRepo(gitHubTemplatesFolder, owner: "jvano", repo: "VisualARM", path: "Templates");
foreach(string filePath in Directory.GetFiles(gitHubTemplatesFolder))
{
TemplateDocument doc = TemplateDocument.FromFile(filePath);
templates.AddRange(doc.Templates);
}
return templates;
}
public static async Task DownloadTemplatesFromGitHubRepo(string downloadFolder, string owner, string repo, string path)
{
IEnumerable<Tuple<string, string>> templateFiles = await GetTemplatesFilesFromGitHubRepo(owner, repo, path);
if (templateFiles.Any())
{
using (HttpClient client = new HttpClient())
{
foreach (Tuple<string, string> templateFile in templateFiles)
{
try
{
string name = templateFile.Item1 as string;
string downloadUrl = templateFile.Item2 as string;
Trace.WriteLine($"Downloading {name} from {downloadUrl}...");
byte[] fileBytes = await client.GetByteArrayAsync(downloadUrl);
string localFile = Path.Combine(downloadFolder, name);
// if file exists is overwritten.
File.WriteAllBytes(localFile, fileBytes);
}
catch (Exception e)
{
Trace.WriteLine(e.Message);
}
}
}
}
}
public static async Task<IEnumerable<Tuple<string, string>>> GetTemplatesFilesFromGitHubRepo(string owner, string repo, string path)
{
List<Tuple<string, string>> templateFiles = new List<Tuple<string, string>>();
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("VisualARM", "1.0"));
string apiUrl = $"https://api.github.com/repos/{owner}/{repo}/contents/{path}";
HttpResponseMessage response = await client.GetAsync(apiUrl);
if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
var items = JsonSerializer.Deserialize<JsonElement>(json);
foreach (var item in items.EnumerateArray())
{
string type = item.GetProperty("type").GetString();
if (type == "file")
{
string name = item.GetProperty("name").GetString();
string itemPath = item.GetProperty("path").GetString();
string downloadUrl = item.GetProperty("download_url").GetString();
templateFiles.Add(new Tuple<string, string>(name, downloadUrl));
}
}
}
}
return templateFiles;
}
#endregion
}
}