Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the search guidance plugin. Closes #375 #376

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.Extensions.Configuration;
using Microsoft365.DeveloperProxy.Abstractions;
using Titanium.Web.Proxy.Http;

namespace Microsoft365.DeveloperProxy.Plugins.Guidance;

public class ODSPSearchGuidancePlugin : BaseProxyPlugin
{
public override string Name => nameof(ODSPSearchGuidancePlugin);

public override void Register(IPluginEvents pluginEvents,
IProxyContext context,
ISet<UrlToWatch> urlsToWatch,
IConfigurationSection? configSection = null)
{
base.Register(pluginEvents, context, urlsToWatch, configSection);

pluginEvents.BeforeRequest += BeforeRequest;
}

private async Task BeforeRequest(object sender, ProxyRequestArgs e)

Check warning on line 24 in m365-developer-proxy-plugins/Guidance/ODSPSearchGuidancePlugin.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
Request request = e.Session.HttpClient.Request;
if (_urlsToWatch is not null && e.HasRequestUrlMatch(_urlsToWatch) && WarnDeprecatedSearch(request))
_logger?.LogRequest(BuildUseGraphSearchMessage(), MessageType.Warning, new LoggingContext(e.Session));
}

private bool WarnDeprecatedSearch(Request request)
{
if (!ProxyUtils.IsGraphRequest(request) ||
request.Method != "GET")
{
return false;
}

// graph.microsoft.com/{version}/drives/{drive-id}/root/search(q='{search-text}')
// graph.microsoft.com/{version}/groups/{group-id}/drive/root/search(q='{search-text}')
// graph.microsoft.com/{version}/me/drive/root/search(q='{search-text}')
// graph.microsoft.com/{version}/sites/{site-id}/drive/root/search(q='{search-text}')
// graph.microsoft.com/{version}/users/{user-id}/drive/root/search(q='{search-text}')
// graph.microsoft.com/{version}/sites?search={query}
if (request.RequestUri.AbsolutePath.Contains("/search(q=", StringComparison.OrdinalIgnoreCase) ||
(request.RequestUri.AbsolutePath.EndsWith("/sites", StringComparison.OrdinalIgnoreCase) &&
request.RequestUri.Query.Contains("search=", StringComparison.OrdinalIgnoreCase)))
{
return true;
}
else
{
return false;
}
}

private static string[] BuildUseGraphSearchMessage() => new[] { $"To get the best search experience, use the Microsoft Search APIs in Microsoft Graph.", $"More info at https://aka.ms/m365/proxy/guidance/odspsearch" };
}
15 changes: 15 additions & 0 deletions m365-developer-proxy/m365proxyrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@
"https://microsoftgraph.chinacloudapi.cn/beta/*"
]
},
{
"name": "ODSPSearchGuidancePlugin",
"enabled": true,
"pluginPath": "plugins\\m365-developer-proxy-plugins.dll",
"urlsToWatch": [
"https://graph.microsoft.com/v1.0/*",
"https://graph.microsoft.com/beta/*",
"https://graph.microsoft.us/v1.0/*",
"https://graph.microsoft.us/beta/*",
"https://dod-graph.microsoft.us/v1.0/*",
"https://dod-graph.microsoft.us/beta/*",
"https://microsoftgraph.chinacloudapi.cn/v1.0/*",
"https://microsoftgraph.chinacloudapi.cn/beta/*"
]
},
{
"name": "GraphBetaSupportGuidancePlugin",
"enabled": true,
Expand Down
Loading