Skip to content

Commit 1905de1

Browse files
committed
Move common GetRequestsFromBatch and GetTokenizedUrl methods to GraphUtils
1 parent ea0c49b commit 1905de1

File tree

3 files changed

+48
-87
lines changed

3 files changed

+48
-87
lines changed

DevProxy.Plugins/Reporting/GraphMinimalPermissionsGuidancePlugin.cs

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ public override async Task AfterRecordingStopAsync(RecordingArgs e, Cancellation
116116
if (ProxyUtils.IsGraphBatchUrl(uri))
117117
{
118118
var graphVersion = ProxyUtils.IsGraphBetaUrl(uri) ? "beta" : "v1.0";
119-
requestsFromBatch = GetRequestsFromBatch(request.Context?.Session.HttpClient.Request.BodyString!, graphVersion, uri.Host);
119+
requestsFromBatch = GraphUtils.GetRequestsFromBatch(request.Context?.Session.HttpClient.Request.BodyString!, graphVersion, uri.Host);
120120
}
121121
else
122122
{
123-
methodAndUrl = new(methodAndUrl.Method, GetTokenizedUrl(methodAndUrl.Url));
123+
methodAndUrl = new(methodAndUrl.Method, GraphUtils.GetTokenizedUrl(methodAndUrl.Url));
124124
}
125125

126126
var (type, permissions) = GetPermissionsAndType(request);
@@ -288,41 +288,6 @@ private async Task EvaluateMinimalScopesAsync(
288288
}
289289
}
290290

291-
private static MethodAndUrl[] GetRequestsFromBatch(string batchBody, string graphVersion, string graphHostName)
292-
{
293-
var requests = new List<MethodAndUrl>();
294-
295-
if (string.IsNullOrEmpty(batchBody))
296-
{
297-
return [.. requests];
298-
}
299-
300-
try
301-
{
302-
var batch = JsonSerializer.Deserialize<GraphBatchRequestPayload>(batchBody, ProxyUtils.JsonSerializerOptions);
303-
if (batch == null)
304-
{
305-
return [.. requests];
306-
}
307-
308-
foreach (var request in batch.Requests)
309-
{
310-
try
311-
{
312-
var method = request.Method;
313-
var url = request.Url;
314-
var absoluteUrl = $"https://{graphHostName}/{graphVersion}{url}";
315-
MethodAndUrl methodAndUrl = new(Method: method, Url: GetTokenizedUrl(absoluteUrl));
316-
requests.Add(methodAndUrl);
317-
}
318-
catch { }
319-
}
320-
}
321-
catch { }
322-
323-
return [.. requests];
324-
}
325-
326291
/// <summary>
327292
/// Returns permissions and type (delegated or application) from the access token
328293
/// used on the request.
@@ -376,10 +341,4 @@ private static (GraphPermissionsType type, IEnumerable<string> permissions) GetP
376341
return (GraphPermissionsType.Application, []);
377342
}
378343
}
379-
380-
private static string GetTokenizedUrl(string absoluteUrl)
381-
{
382-
var sanitizedUrl = ProxyUtils.SanitizeUrl(absoluteUrl);
383-
return "/" + string.Concat(new Uri(sanitizedUrl).Segments.Skip(2).Select(Uri.UnescapeDataString));
384-
}
385344
}

DevProxy.Plugins/Reporting/GraphMinimalPermissionsPlugin.cs

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ public override async Task AfterRecordingStopAsync(RecordingArgs e, Cancellation
9191
if (ProxyUtils.IsGraphBatchUrl(uri))
9292
{
9393
var graphVersion = ProxyUtils.IsGraphBetaUrl(uri) ? "beta" : "v1.0";
94-
var requestsFromBatch = GetRequestsFromBatch(request.Context?.Session.HttpClient.Request.BodyString!, graphVersion, uri.Host);
94+
var requestsFromBatch = GraphUtils.GetRequestsFromBatch(request.Context?.Session.HttpClient.Request.BodyString!, graphVersion, uri.Host);
9595
endpoints.AddRange(requestsFromBatch);
9696
}
9797
else
9898
{
99-
methodAndUrl = new(methodAndUrl.Method, GetTokenizedUrl(methodAndUrl.Url));
99+
methodAndUrl = new(methodAndUrl.Method, GraphUtils.GetTokenizedUrl(methodAndUrl.Url));
100100
endpoints.Add(methodAndUrl);
101101
}
102102
}
@@ -177,45 +177,4 @@ public override async Task AfterRecordingStopAsync(RecordingArgs e, Cancellation
177177
return null;
178178
}
179179
}
180-
181-
private static MethodAndUrl[] GetRequestsFromBatch(string batchBody, string graphVersion, string graphHostName)
182-
{
183-
var requests = new List<MethodAndUrl>();
184-
185-
if (string.IsNullOrEmpty(batchBody))
186-
{
187-
return [.. requests];
188-
}
189-
190-
try
191-
{
192-
var batch = JsonSerializer.Deserialize<GraphBatchRequestPayload>(batchBody, ProxyUtils.JsonSerializerOptions);
193-
if (batch == null)
194-
{
195-
return [.. requests];
196-
}
197-
198-
foreach (var request in batch.Requests)
199-
{
200-
try
201-
{
202-
var method = request.Method;
203-
var url = request.Url;
204-
var absoluteUrl = $"https://{graphHostName}/{graphVersion}{url}";
205-
MethodAndUrl methodAndUrl = new(Method: method, Url: GetTokenizedUrl(absoluteUrl));
206-
requests.Add(methodAndUrl);
207-
}
208-
catch { }
209-
}
210-
}
211-
catch { }
212-
213-
return [.. requests];
214-
}
215-
216-
private static string GetTokenizedUrl(string absoluteUrl)
217-
{
218-
var sanitizedUrl = ProxyUtils.SanitizeUrl(absoluteUrl);
219-
return "/" + string.Concat(new Uri(sanitizedUrl).Segments.Skip(2).Select(Uri.UnescapeDataString));
220-
}
221180
}

DevProxy.Plugins/Utils/GraphUtils.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using DevProxy.Abstractions.Models;
6+
using DevProxy.Abstractions.Utils;
57
using DevProxy.Plugins.Models;
68
using Microsoft.Extensions.Logging;
79
using System.Net.Http.Json;
10+
using System.Text.Json;
811
using Titanium.Web.Proxy.Http;
912

1013
namespace DevProxy.Plugins.Utils;
@@ -101,7 +104,7 @@ internal async Task<IEnumerable<string>> UpdateUserScopesAsync(IEnumerable<strin
101104

102105
public static MethodAndUrl GetMethodAndUrl(string methodAndUrlString)
103106
{
104-
ArgumentException.ThrowIfNullOrWhiteSpace(methodAndUrlString, nameof(methodAndUrlString));
107+
ArgumentException.ThrowIfNullOrWhiteSpace(methodAndUrlString);
105108

106109
var info = methodAndUrlString.Split(" ");
107110
if (info.Length > 2)
@@ -111,4 +114,44 @@ public static MethodAndUrl GetMethodAndUrl(string methodAndUrlString)
111114
return new(Method: info[0], Url: info[1]);
112115
}
113116

117+
public static string GetTokenizedUrl(string absoluteUrl)
118+
{
119+
var sanitizedUrl = ProxyUtils.SanitizeUrl(absoluteUrl);
120+
return "/" + string.Concat(new Uri(sanitizedUrl).Segments.Skip(2).Select(Uri.UnescapeDataString));
121+
}
122+
123+
public static MethodAndUrl[] GetRequestsFromBatch(string batchBody, string graphVersion, string graphHostName)
124+
{
125+
var requests = new List<MethodAndUrl>();
126+
127+
if (string.IsNullOrEmpty(batchBody))
128+
{
129+
return [.. requests];
130+
}
131+
132+
try
133+
{
134+
var batch = JsonSerializer.Deserialize<GraphBatchRequestPayload>(batchBody, ProxyUtils.JsonSerializerOptions);
135+
if (batch == null)
136+
{
137+
return [.. requests];
138+
}
139+
140+
foreach (var request in batch.Requests)
141+
{
142+
try
143+
{
144+
var method = request.Method;
145+
var url = request.Url;
146+
var absoluteUrl = $"https://{graphHostName}/{graphVersion}{url}";
147+
MethodAndUrl methodAndUrl = new(Method: method, Url: GetTokenizedUrl(absoluteUrl));
148+
requests.Add(methodAndUrl);
149+
}
150+
catch { }
151+
}
152+
}
153+
catch { }
154+
155+
return [.. requests];
156+
}
114157
}

0 commit comments

Comments
 (0)