Skip to content

Commit

Permalink
Merge pull request #6 from nickntg/GET-verb
Browse files Browse the repository at this point in the history
Get verb
  • Loading branch information
nickntg authored Aug 19, 2022
2 parents 4c22f3c + 38ca41f commit b787247
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ProjectGuid>{E2CEBBAF-6DF7-41E9-815D-9AD4CF90C845}</ProjectGuid>
<Version>1.0.7</Version>
<Version>1.0.8</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Projects/AWSRedrive.console/AWSRedrive.console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Version>1.0.7</Version>
<Version>1.0.8</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions Projects/AWSRedrive.console/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Region": "eu-west-1",
"Active": true,
"Timeout": 10000,
"UseGET": true,
"ServiceUrl": "https://www.google.com"
}
]
2 changes: 1 addition & 1 deletion Projects/AWSRedrive/AWSRedrive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ProjectGuid>{E2CEBBAF-6DF7-41E9-815D-9AD4CF90C844}</ProjectGuid>
<Version>1.0.7</Version>
<Version>1.0.8</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions Projects/AWSRedrive/ConfigurationEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
95 changes: 76 additions & 19 deletions Projects/AWSRedrive/HttpMessageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Net;
using AWSRedrive.Interfaces;
using Newtonsoft.Json.Linq;
using NLog;
using RestSharp;
using RestSharp.Authenticators;
Expand All @@ -15,9 +16,60 @@ public class HttpMessageProcessor : IMessageProcessor

public void ProcessMessage(string message, Dictionary<string, string> 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)
Expand All @@ -30,28 +82,19 @@ public void ProcessMessage(string message, Dictionary<string, string> 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) &&
Expand All @@ -60,12 +103,26 @@ public void ProcessMessage(string message, Dictionary<string, string> attributes
client.Authenticator = new HttpBasicAuthenticator(configurationEntry.BasicAuthUserName,
configurationEntry.BasicAuthPassword);
}
}

private void AddAttributes(RestRequest request, Dictionary<string, string> 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;
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Here are the elements of a configuration entry:
* **BasicAuthUserName**. If present, AWSRedrive will use this value and the one specified in BasicAuthPassword to perform basic authentication when posting messages to the configured service endpoint.
* **BasicAuthPassword**. See above.
* **Active**. Set to True to enable the configuration, False to disable it.
* **UseGET**. If set to True, AWSRedrive will use GET when sending messages to the configured service endpoint. When doing that, AWSRedrive will unwrap any JSON object contained in the SQS message and turn all JSON fields to query parameters.
* **UsePUT**. If set to True, AWSRedrive will use PUT instead of POST when sending messages to the configured service endpoint.
* **Timeout**. Service timeout in milliseconds to observe when sending messages to the configured service endpoint.
* **IgnoreCertificateErrors**. If set to True, AWSRedrive will ignore any certificate errors when connecting to the configured service endpoint.
Expand Down

0 comments on commit b787247

Please sign in to comment.