-
Notifications
You must be signed in to change notification settings - Fork 62
/
RestSourceTriggerFunctions.cs
301 lines (254 loc) · 12 KB
/
RestSourceTriggerFunctions.cs
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// -----------------------------------------------------------------------
// <copyright file="RestSourceTriggerFunctions.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------
namespace Microsoft.WinGet.RestSource.Helpers
{
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Msix.Utils.Logger;
using Microsoft.WindowsPackageManager.Rest.Diagnostics;
using Microsoft.WindowsPackageManager.Rest.Utils;
using Microsoft.WinGet.RestSource.Exceptions;
using Microsoft.WinGet.RestSource.Interfaces;
using Microsoft.WinGet.RestSource.Utils.Constants;
using Microsoft.WinGet.RestSource.Utils.Models;
using Microsoft.WinGet.RestSource.Utils.Models.Schemas;
using Newtonsoft.Json;
/// <summary>
/// Trigger azure functions for rest source.
/// Currently it supports
/// GET, PUT, POST and DELETE calls to package manifests endpoint.
/// GET, DELETE packages endpoint.
/// DELETE versions endpoint.
/// </summary>
public class RestSourceTriggerFunctions : IRestSourceTriggerFunction
{
private const string PackageManifests = "packageManifests";
private const string Packages = "packages";
private const string Versions = "versions";
private readonly string azFuncPackageManifestsEndpoint;
private readonly string azFuncPackageManifestsEndpointFormat;
private readonly string azFuncPackagesEndpoint;
private readonly string azFuncPackagesEndpointFormat;
private readonly string azFuncVersionsEndpointFormat;
private readonly string azFuncVersionsSpecificEndpointFormat;
/// <summary>
/// Initializes a new instance of the <see cref="RestSourceTriggerFunctions"/> class.
/// </summary>
/// <param name="azFuncRestSourceEndpoint">Azure function Rest Source Endpoint.</param>
public RestSourceTriggerFunctions(
string azFuncRestSourceEndpoint)
{
this.azFuncPackageManifestsEndpoint = $"{azFuncRestSourceEndpoint}{PackageManifests}";
this.azFuncPackageManifestsEndpointFormat = this.azFuncPackageManifestsEndpoint + "/{0}/";
this.azFuncPackagesEndpoint = $"{azFuncRestSourceEndpoint}{Packages}";
this.azFuncPackagesEndpointFormat = this.azFuncPackagesEndpoint + "/{0}/";
this.azFuncVersionsEndpointFormat = this.azFuncPackagesEndpointFormat + $"{Versions}";
this.azFuncVersionsSpecificEndpointFormat = this.azFuncVersionsEndpointFormat + "/{1}";
}
/// <inheritdoc />
public async Task<PackageManifest> GetPackageManifestAsync(
HttpClient httpClient,
string packageIndetifier,
string azFuncHostKey,
LoggingContext loggingContext)
{
Logger.Info($"{loggingContext} Start Get PackageManifest {packageIndetifier}");
var endpoint = string.Format(this.azFuncPackageManifestsEndpointFormat, packageIndetifier);
var httpMethod = HttpMethod.Get;
HttpResponseMessage responseMessage = await AzureFunctionHelper.TriggerAzureFunctionAsync(
httpClient,
httpMethod,
endpoint,
azFuncHostKey);
Logger.Info($"{loggingContext} Get PackageManifest {packageIndetifier} response StatusCode '{responseMessage.StatusCode}'");
if (!responseMessage.IsSuccessStatusCode)
{
throw new RestSourceCallException(endpoint, httpMethod, responseMessage.StatusCode);
}
if (responseMessage.StatusCode == HttpStatusCode.NoContent)
{
return null;
}
return JsonConvert.DeserializeObject<ApiResponse<PackageManifest>>(
await responseMessage.Content.ReadAsStringAsync()).Data;
}
/// <inheritdoc />
public async Task PostPackageManifestAsync(
HttpClient httpClient,
PackageManifest packageManifest,
string azFuncHostKey,
LoggingContext loggingContext)
{
string requestBody = JsonConvert.SerializeObject(packageManifest);
Logger.Info($"{loggingContext} Start Post PackageManifest {packageManifest.PackageIdentifier} Body {requestBody}");
string endpoint = this.azFuncPackageManifestsEndpoint;
var httpMethod = HttpMethod.Post;
HttpResponseMessage responseMessage = await AzureFunctionHelper.TriggerAzureFunctionAsync(
httpClient,
httpMethod,
this.azFuncPackageManifestsEndpoint,
azFuncHostKey,
requestBody);
Logger.Info($"{loggingContext} Post PackageManifest response {packageManifest.PackageIdentifier} StatusCode '{responseMessage.StatusCode}'");
if (!responseMessage.IsSuccessStatusCode)
{
throw new RestSourceCallException(endpoint, httpMethod, responseMessage.StatusCode);
}
}
/// <inheritdoc />
public async Task PutPackageManifestAsync(
HttpClient httpClient,
PackageManifest packageManifest,
string azFuncHostKey,
LoggingContext loggingContext)
{
string requestBody = JsonConvert.SerializeObject(packageManifest);
Logger.Info($"{loggingContext} Start Put PackageManifest {packageManifest.PackageIdentifier} Body {requestBody}");
string endpoint = string.Format(this.azFuncPackageManifestsEndpointFormat, packageManifest.PackageIdentifier);
var httpMethod = HttpMethod.Put;
HttpResponseMessage responseMessage = await AzureFunctionHelper.TriggerAzureFunctionAsync(
httpClient,
httpMethod,
endpoint,
azFuncHostKey,
requestBody);
Logger.Info($"{loggingContext} Put PackageManifest {packageManifest.PackageIdentifier} response StatusCode '{responseMessage.StatusCode}'");
if (!responseMessage.IsSuccessStatusCode)
{
throw new RestSourceCallException(endpoint, httpMethod, responseMessage.StatusCode);
}
}
/// <inheritdoc />
public async Task DeletePackageManifestAsync(
HttpClient httpClient,
string packageIdentifier,
string azFuncHostKey,
LoggingContext loggingContext)
{
Logger.Info($"{loggingContext} Start Delete PackageManifest {packageIdentifier}");
string endpoint = string.Format(this.azFuncPackageManifestsEndpointFormat, packageIdentifier);
var httpMethod = HttpMethod.Delete;
HttpResponseMessage responseMessage = await AzureFunctionHelper.TriggerAzureFunctionAsync(
httpClient,
httpMethod,
endpoint,
azFuncHostKey);
Logger.Info($"{loggingContext} Delete PackageManifest {packageIdentifier} response StatusCode '{responseMessage.StatusCode}'");
if (!responseMessage.IsSuccessStatusCode)
{
throw new RestSourceCallException(endpoint, httpMethod, responseMessage.StatusCode);
}
}
/// <inheritdoc />
public async Task<IReadOnlyList<Package>> GetAllPackagesAsync(
HttpClient httpClient,
string azFuncHostKey,
LoggingContext loggingContext)
{
Logger.Info($"{loggingContext} Start Get All Packages");
var packages = new List<Package>();
string continuationToken = null;
do
{
var pagedPackages = await this.GetPackagesAsync(
httpClient,
azFuncHostKey,
loggingContext,
continuationToken);
// This can happen with a brand new source.
if (pagedPackages == null)
{
break;
}
packages.AddRange(pagedPackages.Data);
continuationToken = pagedPackages.ContinuationToken;
}
while (continuationToken != null);
return packages;
}
/// <inheritdoc />
public async Task<ApiResponse<List<Package>>> GetPackagesAsync(
HttpClient httpClient,
string azFuncHostKey,
LoggingContext loggingContext,
string continuationToken = null)
{
Logger.Info($"{loggingContext} Start Get Packages ContinuationToken {continuationToken}");
Dictionary<string, string> headers = null;
if (!string.IsNullOrEmpty(continuationToken))
{
headers = new Dictionary<string, string>()
{
{ HeaderConstants.ContinuationToken, continuationToken },
};
}
string endpoint = this.azFuncPackagesEndpoint;
var httpMethod = HttpMethod.Get;
HttpResponseMessage responseMessage = await AzureFunctionHelper.TriggerAzureFunctionAsync(
httpClient,
httpMethod,
endpoint,
azFuncHostKey,
headers: headers);
Logger.Info($"{loggingContext} Get Package response ContinuationToken {continuationToken} StatusCode '{responseMessage.StatusCode}'");
if (!responseMessage.IsSuccessStatusCode)
{
throw new RestSourceCallException(endpoint, httpMethod, responseMessage.StatusCode);
}
if (responseMessage.StatusCode == HttpStatusCode.NoContent)
{
return null;
}
return JsonConvert.DeserializeObject<ApiResponse<List<Package>>>(
await responseMessage.Content.ReadAsStringAsync());
}
/// <inheritdoc />
public async Task DeletePackageAsync(
HttpClient httpClient,
string packageIdentifier,
string azFuncHostKey,
LoggingContext loggingContext)
{
Logger.Info($"{loggingContext} Start Delete Package {packageIdentifier}");
string endpoint = string.Format(this.azFuncPackagesEndpointFormat, packageIdentifier);
var httpMethod = HttpMethod.Delete;
HttpResponseMessage responseMessage = await AzureFunctionHelper.TriggerAzureFunctionAsync(
httpClient,
httpMethod,
endpoint,
azFuncHostKey);
Logger.Info($"{loggingContext} Delete Package {packageIdentifier} response StatusCode '{responseMessage.StatusCode}'");
if (!responseMessage.IsSuccessStatusCode)
{
throw new RestSourceCallException(endpoint, httpMethod, responseMessage.StatusCode);
}
}
/// <inheritdoc />
public async Task DeleteVersionAsync(
HttpClient httpClient,
string packageIdentifier,
string version,
string azFuncHostKey,
LoggingContext loggingContext)
{
Logger.Info($"{loggingContext} Start Delete Version {packageIdentifier} {version}");
string endpoint = string.Format(this.azFuncVersionsSpecificEndpointFormat, packageIdentifier, version);
var httpMethod = HttpMethod.Delete;
HttpResponseMessage responseMessage = await AzureFunctionHelper.TriggerAzureFunctionAsync(
httpClient,
httpMethod,
endpoint,
azFuncHostKey);
Logger.Info($"{loggingContext} Delete Version response {packageIdentifier} {version} response StatusCode '{responseMessage.StatusCode}'");
if (!responseMessage.IsSuccessStatusCode)
{
throw new RestSourceCallException(endpoint, httpMethod, responseMessage.StatusCode);
}
}
}
}