Skip to content

Commit

Permalink
Merge pull request #219 from thinktecture/feature/additional-properties
Browse files Browse the repository at this point in the history
Add additional properties in JSON to request root
  • Loading branch information
gingters authored May 7, 2020
2 parents 2b41899 + 743a2dc commit 6f75fd0
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
11 changes: 11 additions & 0 deletions Thinktecture.Relay.CustomCodeDemo/DemoRequestInterceptor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -46,6 +47,16 @@ public HttpResponseMessage OnRequestReceived(IInterceptedRequest request)
_logger?.Debug("This request comes from localhost.");
}

// Example: Move query parameters into own JSON property
if (request.Url.Contains("?"))
{
var parts = request.Url.Split('?');
request.Url = parts[0];
request.Properties = new Dictionary<string, object>() {
{ "Parameter", parts[1] }
};
}

// Example: Modify content
if (request.HttpHeaders.TryGetValue("Content-Type", out var contentType) && contentType == "application/json")
{
Expand Down
3 changes: 2 additions & 1 deletion Thinktecture.Relay.Server/Interceptor/InterceptedRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Stream Content

public InterceptedRequest(ILogger logger, IOnPremiseConnectorRequest other)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));;
_logger = logger ?? throw new ArgumentNullException(nameof(logger));

RequestId = other.RequestId;
OriginId = other.OriginId;
Expand All @@ -49,6 +49,7 @@ public InterceptedRequest(ILogger logger, IOnPremiseConnectorRequest other)
AlwaysSendToOnPremiseConnector = other.AlwaysSendToOnPremiseConnector;
Expiration = other.Expiration;
AcknowledgeOriginId = other.AcknowledgeOriginId;
Properties = other.Properties;
}

public Dictionary<string, string> CloneHttpHeaders()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ internal class OnPremiseConnectorRequest : IOnPremiseConnectorRequest
public TimeSpan Expiration { get; set; }

public Guid AcknowledgeOriginId { get; set; }

public IReadOnlyDictionary<string, object> Properties { get; set; }
}
}
15 changes: 14 additions & 1 deletion Thinktecture.Relay.Server/SignalR/OnPremisesConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Threading.Tasks;
using Autofac;
using Microsoft.AspNet.SignalR;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Serilog;
using Thinktecture.Relay.Server.Communication;
using Thinktecture.Relay.Server.Config;
Expand Down Expand Up @@ -74,7 +76,18 @@ private async Task ForwardClientRequestAsync(string connectionId, IOnPremiseConn
_logger?.Verbose("Forwarding client request to connection. connection-id={ConnectionId}, request-id={RequestId}, http-method={RequestMethod}, url={RequestUrl}, origin-id={OriginId}, body-length={RequestContentLength}",
connectionId, request.RequestId, request.HttpMethod, _configuration.LogSensitiveData ? uri.PathAndQuery : uri.AbsolutePath, request.OriginId, request.ContentLength);

await Connection.Send(connectionId, request).ConfigureAwait(false);
var json = JObject.FromObject(request);
if (request.Properties != null)
{
json.Remove(nameof(IOnPremiseConnectorRequest.Properties));

foreach (var kvp in request.Properties)
{
json[kvp.Key] = JToken.FromObject(kvp.Value);
}
}

await Connection.Send(connectionId, json).ConfigureAwait(false);
}

private static OnPremiseClaims GetOnPremiseClaims(IRequest request)
Expand Down
5 changes: 5 additions & 0 deletions Thinktecture.Relay/Server/Interceptor/IInterceptedRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,10 @@ public interface IInterceptedRequest : IReadOnlyInterceptedRequest
/// </summary>
/// <returns>A changeable dictionary containing all HttpHeaders of the intercepted request.</returns>
Dictionary<string, string> CloneHttpHeaders();

/// <summary>
/// Gets the additional properties which will be serialized onto the root object
/// </summary>
IReadOnlyDictionary<string, object> Properties { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,10 @@ public interface IOnPremiseConnectorRequest
/// Gets the id of the RelayServer this request may acknowledged to
/// </summary>
Guid AcknowledgeOriginId { get; }

/// <summary>
/// Gets the additional properties which will be serialized onto the root object
/// </summary>
IReadOnlyDictionary<string, object> Properties { get; }
}
}

0 comments on commit 6f75fd0

Please sign in to comment.