-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonToXmlTransformProvider.cs
More file actions
43 lines (37 loc) · 1.73 KB
/
Copy pathJsonToXmlTransformProvider.cs
File metadata and controls
43 lines (37 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using Yarp.ReverseProxy.Transforms;
using Yarp.ReverseProxy.Transforms.Builder;
namespace XmlDataSourceProxy;
/// <summary>
/// Registers the JSON-to-XML response transform on every route, picking up
/// an optional <c>RowPath</c> from the route's <c>Metadata</c> dictionary.
/// All authentication (API keys, bearer tokens, basic auth) is configured
/// declaratively in <c>appsettings.json</c> using YARP's built-in
/// <c>RequestHeader</c> and <c>QueryValueParameter</c> transforms.
/// </summary>
public class JsonToXmlTransformProvider : ITransformProvider
{
public const string RowPathMetadataKey = "RowPath";
public void ValidateRoute(TransformRouteValidationContext context) { }
public void ValidateCluster(TransformClusterValidationContext context) { }
public void Apply(TransformBuilderContext context)
{
string? rowPath = null;
if (context.Route.Metadata != null &&
context.Route.Metadata.TryGetValue(RowPathMetadataKey, out var value))
{
rowPath = value;
}
// Ask upstream for uncompressed JSON. SSRS doesn't send Accept-Encoding
// itself, but other test clients (browsers, curl, Invoke-WebRequest) do,
// and YARP forwards it; clearing it keeps the response body uncompressed
// so the JSON->XML transform can parse it directly.
context.AddRequestTransform(t =>
{
t.ProxyRequest.Headers.Accept.Clear();
t.ProxyRequest.Headers.TryAddWithoutValidation("Accept", "application/json");
t.ProxyRequest.Headers.AcceptEncoding.Clear();
return ValueTask.CompletedTask;
});
context.AddResponseTransform(ctx => JsonToXmlResponseTransform.TransformAsync(ctx, rowPath));
}
}