-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenAPIOverrides.cs
More file actions
91 lines (81 loc) · 3.91 KB
/
OpenAPIOverrides.cs
File metadata and controls
91 lines (81 loc) · 3.91 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.
// Licensed under the MIT license.
using System.Text.Json;
namespace CheckCloudSupport.OpenAPI;
/// <summary>
/// Contains helper methods to lookup override paths.
/// </summary>
public static class OpenAPIOverrides
{
private static List<Override>? overrides = null;
private static List<CloudExclusion>? cloudExclusions = null;
/// <summary>
/// Loads overrides and exclusions from JSON files.
/// </summary>
/// <param name="overridesFile">The path to a JSON file containing API overrides.</param>
/// <param name="excludesFile">The path to a JSON file containing cloud exclusions.</param>
public static void Initialize(string? overridesFile, string? excludesFile)
{
LoadOverridesFromJson(overridesFile);
LoadCloudExclusionsFromJson(excludesFile);
}
/// <summary>
/// Returns override API path for a given API path, if one exists.
/// </summary>
/// <param name="originalPath">The original API path to check for an override for.</param>
/// <param name="method">The HTTP method to check for an override for.</param>
/// <returns>The override path.</returns>
public static string CheckForOverride(string originalPath, HttpMethod? method)
{
var apiOverride = overrides?.SingleOrDefault(o => string.Compare(o.ApiPath?.ToLower(), originalPath.ToLower()) == 0);
if (!string.IsNullOrEmpty(apiOverride?.Operation) &&
method != null &&
string.Compare(apiOverride.Operation, method.Method, StringComparison.OrdinalIgnoreCase) != 0)
{
return originalPath;
}
return apiOverride == null ? originalPath : apiOverride.OverridePath ?? originalPath;
}
/// <summary>
/// Checks if a cloud is excluded for a given API path and method.
/// </summary>
/// <param name="apiPath">The API path to check for an exclusion for.</param>
/// <param name="method">The HTTP method to check for an exclusion for.</param>
/// <param name="cloud">The cloud to check.</param>
/// <returns>True if the given cloud is excluded.</returns>
public static bool CheckIfCloudExcluded(string apiPath, HttpMethod? method, string cloud)
{
apiPath = apiPath.Replace("\\", "/");
return cloudExclusions?.Any(e => string.Compare(e.ApiPath, apiPath, StringComparison.InvariantCultureIgnoreCase) == 0 &&
string.Compare(e.Operation, method?.Method, StringComparison.InvariantCultureIgnoreCase) == 0 &&
string.Compare(e.Cloud, cloud, StringComparison.InvariantCultureIgnoreCase) == 0) ?? false;
}
/// <summary>
/// Checks if a cloud is excluded for a given API doc file. This is used to exclude entire
/// files of APIs that don't function in certain clouds, even if the individual API paths aren't known.
/// </summary>
/// <param name="fileName">The name of the API doc file to check for an exclusion for.</param>
/// <param name="cloud">The cloud to check.</param>
/// <returns>True if the given cloud is excluded for the specified file.</returns>
public static bool CheckIfCloudExcludedForFile(string fileName, string cloud)
{
return cloudExclusions?.Any(e => string.Compare(e.FileName, fileName, StringComparison.InvariantCultureIgnoreCase) == 0 &&
string.Compare(e.Cloud, cloud, StringComparison.InvariantCultureIgnoreCase) == 0) ?? false;
}
private static void LoadOverridesFromJson(string? jsonFile)
{
if (!string.IsNullOrEmpty(jsonFile))
{
var json = File.ReadAllText(jsonFile);
overrides = JsonSerializer.Deserialize<List<Override>>(json);
}
}
private static void LoadCloudExclusionsFromJson(string? jsonFile)
{
if (!string.IsNullOrEmpty(jsonFile))
{
var json = File.ReadAllText(jsonFile);
cloudExclusions = JsonSerializer.Deserialize<List<CloudExclusion>>(json);
}
}
}