1- // Copyright (c) Microsoft Corporation.
2- // Licensed under the MIT License.
3-
4- using System . Text . Json ;
5- using Microsoft . DevProxy . Abstractions ;
6- using Microsoft . DevProxy . Plugins . RequestLogs ;
7- using Microsoft . Extensions . Configuration ;
8- using Microsoft . Extensions . Logging ;
9-
10- namespace Microsoft . DevProxy . Plugins . Reporters ;
11-
12- public class JsonReporter ( IPluginEvents pluginEvents , IProxyContext context , ILogger logger , ISet < UrlToWatch > urlsToWatch , IConfigurationSection ? configSection = null ) : BaseReporter ( pluginEvents , context , logger , urlsToWatch , configSection )
13- {
14- public override string Name => nameof ( JsonReporter ) ;
15- public override string FileExtension => ".json" ;
16-
17- private readonly Dictionary < Type , Func < object , object > > _transformers = new ( )
18- {
19- { typeof ( ExecutionSummaryPluginReportByUrl ) , TransformExecutionSummary } ,
20- { typeof ( ExecutionSummaryPluginReportByMessageType ) , TransformExecutionSummary } ,
21- } ;
22-
23- protected override string GetReport ( KeyValuePair < string , object > report )
24- {
25- Logger . LogDebug ( "Serializing report {reportKey}..." , report . Key ) ;
26-
27- var reportData = report . Value ;
28- var reportType = reportData . GetType ( ) ;
29-
30- if ( _transformers . TryGetValue ( reportType , out var transform ) )
31- {
32- Logger . LogDebug ( "Transforming {reportType} using {transform}..." , reportType . Name , transform . Method . Name ) ;
33- reportData = transform ( reportData ) ;
34- }
35- else
36- {
37- Logger . LogDebug ( "No transformer found for {reportType}" , reportType . Name ) ;
38- }
39-
40- if ( reportData is string strVal )
41- {
42- Logger . LogDebug ( "{reportKey} is a string. Checking if it's JSON..." , report . Key ) ;
43-
44- try
45- {
46- JsonSerializer . Deserialize < object > ( strVal ) ;
47- Logger . LogDebug ( "{reportKey} is already JSON, ignore" , report . Key ) ;
48- // already JSON, ignore
49- return strVal ;
50- }
51- catch
52- {
53- Logger . LogDebug ( "{reportKey} is not JSON, serializing..." , report . Key ) ;
54- }
55- }
56-
57- return JsonSerializer . Serialize ( reportData , ProxyUtils . JsonSerializerOptions ) ;
58- }
59-
60- private static object TransformExecutionSummary ( object report )
61- {
62- var executionSummaryReport = ( ExecutionSummaryPluginReportBase ) report ;
63- return executionSummaryReport . Data ;
64- }
1+ // Copyright (c) Microsoft Corporation.
2+ // Licensed under the MIT License.
3+
4+ using System . Text ;
5+ using System . Text . Json ;
6+ using Microsoft . DevProxy . Abstractions ;
7+ using Microsoft . DevProxy . Plugins . RequestLogs ;
8+ using Microsoft . Extensions . Configuration ;
9+ using Microsoft . Extensions . Logging ;
10+
11+ namespace Microsoft . DevProxy . Plugins . Reporters ;
12+
13+ public class JsonReporter ( IPluginEvents pluginEvents , IProxyContext context , ILogger logger , ISet < UrlToWatch > urlsToWatch , IConfigurationSection ? configSection = null ) : BaseReporter ( pluginEvents , context , logger , urlsToWatch , configSection )
14+ {
15+ public override string Name => nameof ( JsonReporter ) ;
16+ private string _fileExtension = ".json" ;
17+ public override string FileExtension => _fileExtension ;
18+
19+ private readonly Dictionary < Type , Func < object , object > > _transformers = new ( )
20+ {
21+ { typeof ( ExecutionSummaryPluginReportByUrl ) , TransformExecutionSummary } ,
22+ { typeof ( ExecutionSummaryPluginReportByMessageType ) , TransformExecutionSummary } ,
23+ { typeof ( UrlDiscoveryPluginReport ) , TransformUrlDiscoveryReport }
24+ } ;
25+
26+ protected override string GetReport ( KeyValuePair < string , object > report )
27+ {
28+ Logger . LogDebug ( "Serializing report {reportKey}..." , report . Key ) ;
29+
30+ var reportData = report . Value ;
31+ var reportType = reportData . GetType ( ) ;
32+ _fileExtension = reportType . Name == nameof ( UrlDiscoveryPluginReport ) ? ".jsonc" : ".json" ;
33+
34+ if ( _transformers . TryGetValue ( reportType , out var transform ) )
35+ {
36+ Logger . LogDebug ( "Transforming {reportType} using {transform}..." , reportType . Name , transform . Method . Name ) ;
37+ reportData = transform ( reportData ) ;
38+ }
39+ else
40+ {
41+ Logger . LogDebug ( "No transformer found for {reportType}" , reportType . Name ) ;
42+ }
43+
44+ if ( reportData is string strVal )
45+ {
46+ Logger . LogDebug ( "{reportKey} is a string. Checking if it's JSON..." , report . Key ) ;
47+
48+ try
49+ {
50+ JsonSerializer . Deserialize < object > ( strVal , ProxyUtils . JsonSerializerOptions ) ;
51+ Logger . LogDebug ( "{reportKey} is already JSON, ignore" , report . Key ) ;
52+ // already JSON, ignore
53+ return strVal ;
54+ }
55+ catch
56+ {
57+ Logger . LogDebug ( "{reportKey} is not JSON, serializing..." , report . Key ) ;
58+ }
59+ }
60+
61+ return JsonSerializer . Serialize ( reportData , ProxyUtils . JsonSerializerOptions ) ;
62+ }
63+
64+ private static object TransformExecutionSummary ( object report )
65+ {
66+ var executionSummaryReport = ( ExecutionSummaryPluginReportBase ) report ;
67+ return executionSummaryReport . Data ;
68+ }
69+
70+ private static object TransformUrlDiscoveryReport ( object report )
71+ {
72+ var urlDiscoveryPluginReport = ( UrlDiscoveryPluginReport ) report ;
73+
74+ var sb = new StringBuilder ( ) ;
75+ sb . AppendLine ( "{" ) ;
76+ sb . AppendLine ( " // Wildcards" ) ;
77+ sb . AppendLine ( " // " ) ;
78+ sb . AppendLine ( " // You can use wildcards to catch multiple URLs with the same pattern." ) ;
79+ sb . AppendLine ( " // For example, you can use the following URL pattern to catch all API requests to" ) ;
80+ sb . AppendLine ( " // JSON Placeholder API:" ) ;
81+ sb . AppendLine ( " // " ) ;
82+ sb . AppendLine ( " // https://jsonplaceholder.typicode.com/*" ) ;
83+ sb . AppendLine ( " // " ) ;
84+ sb . AppendLine ( " // Excluding URLs" ) ;
85+ sb . AppendLine ( " // " ) ;
86+ sb . AppendLine ( " // You can exclude URLs with ! to prevent them from being intercepted." ) ;
87+ sb . AppendLine ( " // For example, you can exclude the URL https://jsonplaceholder.typicode.com/authors" ) ;
88+ sb . AppendLine ( " // by using the following URL pattern:" ) ;
89+ sb . AppendLine ( " // " ) ;
90+ sb . AppendLine ( " // !https://jsonplaceholder.typicode.com/authors" ) ;
91+ sb . AppendLine ( " // https://jsonplaceholder.typicode.com/*" ) ;
92+ sb . AppendLine ( " \" urlsToWatch\" : [" ) ;
93+ sb . AppendJoin ( $ ",{ Environment . NewLine } ", urlDiscoveryPluginReport . Data . Select ( u => $ " \" { u } \" ") ) ;
94+ sb . AppendLine ( "" ) ;
95+ sb . AppendLine ( " ]" ) ;
96+ sb . AppendLine ( "}" ) ;
97+
98+ return sb . ToString ( ) ;
99+ }
65100}
0 commit comments