Skip to content

Commit 6f2c980

Browse files
Adds the beta support guidance. Closes #86 (#120)
* Adds the beta support guidance. Closes #86
1 parent 425dc63 commit 6f2c980

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

msgraph-developer-proxy-abstractions/ProxyUtils.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public static bool IsGraphRequest(Request request) =>
1313

1414
public static bool IsSdkRequest(Request request) => request.Headers.HeaderExists("SdkVersion");
1515

16+
public static bool IsGraphBetaRequest(Request request) =>
17+
IsGraphRequest(request) &&
18+
request.RequestUri.AbsolutePath.Contains("/beta/", StringComparison.OrdinalIgnoreCase);
19+
1620
/// <summary>
1721
/// Utiliy to build HTTP respopnse headers consistent with Microsoft Graph
1822
/// </summary>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Graph.DeveloperProxy.Abstractions;
6+
using System.Text.RegularExpressions;
7+
using Titanium.Web.Proxy.Http;
8+
9+
namespace Microsoft.Graph.DeveloperProxy.Plugins.Guidance;
10+
11+
public class GraphBetaSupportGuidancePlugin : IProxyPlugin {
12+
private ISet<Regex>? _urlsToWatch;
13+
private ILogger? _logger;
14+
public string Name => nameof(GraphBetaSupportGuidancePlugin);
15+
16+
public void Register(IPluginEvents pluginEvents,
17+
IProxyContext context,
18+
ISet<Regex> urlsToWatch,
19+
IConfigurationSection? configSection = null) {
20+
if (pluginEvents is null) {
21+
throw new ArgumentNullException(nameof(pluginEvents));
22+
}
23+
24+
if (context is null || context.Logger is null) {
25+
throw new ArgumentException($"{nameof(context)} must not be null and must supply a non-null Logger", nameof(context));
26+
}
27+
28+
if (urlsToWatch is null || urlsToWatch.Count == 0) {
29+
throw new ArgumentException($"{nameof(urlsToWatch)} cannot be null or empty", nameof(urlsToWatch));
30+
}
31+
32+
_urlsToWatch = urlsToWatch;
33+
_logger = context.Logger;
34+
35+
pluginEvents.AfterResponse += AfterResponse;
36+
}
37+
38+
private void AfterResponse(object? sender, ProxyResponseArgs e) {
39+
Request request = e.Session.HttpClient.Request;
40+
if (_urlsToWatch is not null &&
41+
e.HasRequestUrlMatch(_urlsToWatch) &&
42+
ProxyUtils.IsGraphBetaRequest(request))
43+
_logger?.LogRequest(BuildBetaSupportMessage(request), MessageType.Warning, new LoggingContext(e.Session));
44+
}
45+
46+
private static string GetBetaSupportGuidanceUrl() => "https://learn.microsoft.com/graph/versioning-and-support#beta-version";
47+
private static string[] BuildBetaSupportMessage(Request r) => new[] { $"Don't use beta APIs in production because they can change or be deprecated.", $"More info at {GetBetaSupportGuidanceUrl()}" };
48+
}

msgraph-developer-proxy/ProxyCommandHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation.
1+
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

44
using Microsoft.Extensions.Configuration;
@@ -16,6 +16,7 @@ public class ProxyCommandHandler : ICommandHandler {
1616
private readonly PluginEvents _pluginEvents;
1717
private readonly ISet<Regex> _urlsToWatch;
1818
private readonly ILogger _logger;
19+
1920
public ProxyCommandHandler(Option<int?> port,
2021
Option<LogLevel?> logLevel,
2122
PluginEvents pluginEvents,

msgraph-developer-proxy/ProxyHost.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation.
1+
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

44
using Microsoft.Graph.DeveloperProxy.Abstractions;
@@ -15,7 +15,7 @@ public ProxyHost() {
1515
_portOption = new Option<int?>("--port", "The port for the proxy server to listen on");
1616
_portOption.AddAlias("-p");
1717
_portOption.ArgumentHelpName = "port";
18-
18+
1919
_logLevelOption = new Option<LogLevel?>("--logLevel", $"Level of messages to log. Allowed values: {string.Join(", ", Enum.GetNames(typeof(LogLevel)))}");
2020
_logLevelOption.ArgumentHelpName = "logLevel";
2121
_logLevelOption.AddValidator(input => {

msgraph-developer-proxy/appsettings.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
"https://microsoftgraph.chinacloudapi.cn/beta/*"
1515
]
1616
},
17+
{
18+
"pluginPath": "GraphProxyPlugins\\msgraph-developer-proxy-plugins.dll",
19+
"name": "GraphBetaSupportGuidancePlugin",
20+
"urlsToWatch": [
21+
"https://graph.microsoft.com/v1.0/*",
22+
"https://graph.microsoft.com/beta/*",
23+
"https://graph.microsoft.us/v1.0/*",
24+
"https://graph.microsoft.us/beta/*",
25+
"https://dod-graph.microsoft.us/v1.0/*",
26+
"https://dod-graph.microsoft.us/beta/*",
27+
"https://microsoftgraph.chinacloudapi.cn/v1.0/*",
28+
"https://microsoftgraph.chinacloudapi.cn/beta/*"
29+
]
30+
},
1731
{
1832
"pluginPath": "GraphProxyPlugins\\msgraph-developer-proxy-plugins.dll",
1933
"name": "GraphSdkGuidancePlugin",

0 commit comments

Comments
 (0)