Skip to content

Commit

Permalink
Adds the search guidance plugin. Closes #375 (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
waldekmastykarz authored Nov 7, 2023
1 parent 98ad667 commit b87a7cd
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
58 changes: 58 additions & 0 deletions m365-developer-proxy-plugins/Guidance/ODSPSearchGuidancePlugin.cs
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 / Publish binaries (linux-x64)

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.

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

View workflow job for this annotation

GitHub Actions / Publish binaries (win-x64)

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.

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

View workflow job for this annotation

GitHub Actions / Publish binaries (osx-x64)

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

0 comments on commit b87a7cd

Please sign in to comment.