From 739a85445054872d0e60bd1b4edcbf723a1d4e6c Mon Sep 17 00:00:00 2001 From: "n.bitounis" Date: Thu, 18 Aug 2022 18:32:01 +0300 Subject: [PATCH] GET request with JSON field unwrapping now supported. --- .../AWSRedrive.LinuxService.csproj | 2 +- .../AWSRedrive.console.csproj | 2 +- Projects/AWSRedrive.console/config.json | 1 + Projects/AWSRedrive/AWSRedrive.csproj | 2 +- Projects/AWSRedrive/ConfigurationEntry.cs | 1 + Projects/AWSRedrive/HttpMessageProcessor.cs | 95 +++++++++++++++---- 6 files changed, 81 insertions(+), 22 deletions(-) diff --git a/Projects/AWSRedrive.LinuxService/AWSRedrive.LinuxService.csproj b/Projects/AWSRedrive.LinuxService/AWSRedrive.LinuxService.csproj index e741e08..d852feb 100644 --- a/Projects/AWSRedrive.LinuxService/AWSRedrive.LinuxService.csproj +++ b/Projects/AWSRedrive.LinuxService/AWSRedrive.LinuxService.csproj @@ -3,7 +3,7 @@ net6.0 {E2CEBBAF-6DF7-41E9-815D-9AD4CF90C845} - 1.0.7 + 1.0.8 diff --git a/Projects/AWSRedrive.console/AWSRedrive.console.csproj b/Projects/AWSRedrive.console/AWSRedrive.console.csproj index 08b5170..8b62544 100644 --- a/Projects/AWSRedrive.console/AWSRedrive.console.csproj +++ b/Projects/AWSRedrive.console/AWSRedrive.console.csproj @@ -3,7 +3,7 @@ Exe net6.0 - 1.0.7 + 1.0.8 diff --git a/Projects/AWSRedrive.console/config.json b/Projects/AWSRedrive.console/config.json index 5c7ec19..d9ed00b 100644 --- a/Projects/AWSRedrive.console/config.json +++ b/Projects/AWSRedrive.console/config.json @@ -6,6 +6,7 @@ "Region": "eu-west-1", "Active": true, "Timeout": 10000, + "UseGET": true, "ServiceUrl": "https://www.google.com" } ] \ No newline at end of file diff --git a/Projects/AWSRedrive/AWSRedrive.csproj b/Projects/AWSRedrive/AWSRedrive.csproj index 65c372f..88a5510 100644 --- a/Projects/AWSRedrive/AWSRedrive.csproj +++ b/Projects/AWSRedrive/AWSRedrive.csproj @@ -3,7 +3,7 @@ net6.0 {E2CEBBAF-6DF7-41E9-815D-9AD4CF90C844} - 1.0.7 + 1.0.8 diff --git a/Projects/AWSRedrive/ConfigurationEntry.cs b/Projects/AWSRedrive/ConfigurationEntry.cs index 4f439f0..12bd652 100644 --- a/Projects/AWSRedrive/ConfigurationEntry.cs +++ b/Projects/AWSRedrive/ConfigurationEntry.cs @@ -19,6 +19,7 @@ public class ConfigurationEntry public string BasicAuthPassword { get; set; } public bool Active { get; set; } public bool UsePUT { get; set; } + public bool UseGET { get; set; } public int? Timeout { get; set; } public bool IgnoreCertificateErrors { get; set; } public string ServiceUrl { get; set; } diff --git a/Projects/AWSRedrive/HttpMessageProcessor.cs b/Projects/AWSRedrive/HttpMessageProcessor.cs index 2a9e52c..63008fd 100644 --- a/Projects/AWSRedrive/HttpMessageProcessor.cs +++ b/Projects/AWSRedrive/HttpMessageProcessor.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Net; using AWSRedrive.Interfaces; +using Newtonsoft.Json.Linq; using NLog; using RestSharp; using RestSharp.Authenticators; @@ -15,9 +16,60 @@ public class HttpMessageProcessor : IMessageProcessor public void ProcessMessage(string message, Dictionary attributes, ConfigurationEntry configurationEntry) { - Logger.Trace($"Preparing post to {configurationEntry.RedriveUrl}"); + Logger.Trace($"Preparing request to {configurationEntry.RedriveUrl}"); var uri = new Uri(configurationEntry.RedriveUrl); + var options = CreateOptions(uri, configurationEntry); + + var client = new RestClient(options); + + var request = CreateRequest(message, uri, configurationEntry); + + AddAuthentication(client, request, configurationEntry); + + AddAttributes(request, attributes); + + SendRequest(client, request, configurationEntry); + } + + private RestRequest CreateRequest(string message, Uri uri, ConfigurationEntry configurationEntry) + { + return !configurationEntry.UseGET + ? CreatePostOrPutRequest(message, uri, configurationEntry) + : CreateGetRequest(message, uri); + } + + private RestRequest CreateGetRequest(string message, Uri uri) + { + var request = new RestRequest(uri.PathAndQuery, Method.Get); + try + { + var data = JObject.Parse(message); + foreach (var p in data.Properties()) + { + request.AddQueryParameter(p.Name, p.Value.ToString()); + } + } + catch (Exception ex) + { + Logger.Warn(ex, $"Error parsing message and adding query parameters. GET request might be incorrect."); + Logger.Warn($"Message was [{message}]"); + } + + return request; + } + + private RestRequest CreatePostOrPutRequest(string message, Uri uri, ConfigurationEntry configurationEntry) + { + var request = new RestRequest(uri.PathAndQuery, configurationEntry.UsePUT ? Method.Put : Method.Post); + + request.AddStringBody(message, DataFormat.Json); + + return request; + } + + private RestClientOptions CreateOptions(Uri uri, ConfigurationEntry configurationEntry) + { var options = new RestClientOptions($"{uri.Scheme}://{uri.Host}:{uri.Port}"); if (configurationEntry.IgnoreCertificateErrors) @@ -30,28 +82,19 @@ public void ProcessMessage(string message, Dictionary attributes options.Timeout = configurationEntry.Timeout.Value; } - var client = new RestClient(options); - - var post = new RestRequest(uri.PathAndQuery, configurationEntry.UsePUT ? Method.Put : Method.Post); - - post.AddStringBody(message, DataFormat.Json); + return options; + } + private void AddAuthentication(RestClient client, RestRequest request, ConfigurationEntry configurationEntry) + { if (!string.IsNullOrEmpty(configurationEntry.AwsGatewayToken)) { - post.AddHeader("x-api-key", configurationEntry.AwsGatewayToken); + request.AddHeader("x-api-key", configurationEntry.AwsGatewayToken); } if (!string.IsNullOrEmpty(configurationEntry.AuthToken)) { - post.AddHeader("Authorization", configurationEntry.AuthToken); - } - - if (attributes != null) - { - foreach (var key in attributes.Keys.Where(key => !string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(attributes[key]))) - { - post.AddHeader(key, attributes[key]); - } + request.AddHeader("Authorization", configurationEntry.AuthToken); } if (!string.IsNullOrEmpty(configurationEntry.BasicAuthPassword) && @@ -60,12 +103,26 @@ public void ProcessMessage(string message, Dictionary attributes client.Authenticator = new HttpBasicAuthenticator(configurationEntry.BasicAuthUserName, configurationEntry.BasicAuthPassword); } + } + + private void AddAttributes(RestRequest request, Dictionary attributes) + { + if (attributes != null) + { + foreach (var key in attributes.Keys.Where(key => !string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(attributes[key]))) + { + request.AddHeader(key, attributes[key]); + } + } + } + private void SendRequest(RestClient client, RestRequest request, ConfigurationEntry configurationEntry) + { Logger.Trace($"Posting to {configurationEntry.RedriveUrl}"); - var response = client.ExecuteAsync(post).Result; + var response = client.ExecuteAsync(request).Result; - if (response.IsSuccessful && - (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created)) + if (response.IsSuccessful && + (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created)) { Logger.Trace($"Post to {configurationEntry.RedriveUrl} successful"); return;