-
Notifications
You must be signed in to change notification settings - Fork 2
/
ServiceForwarder.cs
46 lines (44 loc) · 1.9 KB
/
ServiceForwarder.cs
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
44
45
46
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using net.vieapps.Components.Utility;
namespace net.vieapps.Services
{
/// <summary>
/// Forwards a request to a remote HTTP service in the VIEApps NGX API Gateway
/// </summary>
public class ServiceForwarder
{
/// <summary>
/// Prepares the request before sending
/// </summary>
/// <param name="requestInfo"></param>
/// <param name="endpointURL"></param>
/// <param name="dataSource"></param>
/// <param name="cancellationToken"></param>
/// <returns>The string that presents the well-formed URL of the remote end-point</returns>
public virtual Task<string> PrepareAsync(RequestInfo requestInfo, string endpointURL, string dataSource, CancellationToken cancellationToken)
{
var url = endpointURL ?? "/";
var objectName = requestInfo.Query["object-name"];
if (!string.IsNullOrWhiteSpace(objectName))
{
var objectIdentity = requestInfo.GetObjectIdentity();
url += $"{(url.EndsWith("/") ? "" : "/")}{objectName.ToLower()}{(string.IsNullOrWhiteSpace(objectIdentity) ? "" : $"/{objectIdentity.ToLower()}")}";
}
var query = requestInfo.Query.Where(kvp => !kvp.Key.IsEquals("service-name") && !kvp.Key.IsEquals("object-name") && !kvp.Key.IsEquals("object-identity")).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
url += query.Any() ? $"{(url.IndexOf("?") > 0 ? "&" : "?")}{query.ToString("&", kvp => $"{kvp.Key}={kvp.Value?.UrlEncode()}")}" : "";
return Task.FromResult(url);
}
/// <summary>
/// Normalizes the body before response to client
/// </summary>
/// <param name="requestInfo"></param>
/// <param name="body"></param>
/// <param name="cancellationToken"></param>
/// <returns>The normalized JSON</returns>
public virtual Task<JToken> NormalizeAsync(RequestInfo requestInfo, JToken body, CancellationToken cancellationToken)
=> Task.FromResult(body);
}
}