Skip to content

Commit

Permalink
Merge branch 'release/0.57.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jericho committed Jun 3, 2019
2 parents 853ca7d + d32b167 commit 916fa54
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 329 deletions.
119 changes: 0 additions & 119 deletions Source/StrongGrid.IntegrationTests/ColoredConsoleLogProvider.cs

This file was deleted.

40 changes: 31 additions & 9 deletions Source/StrongGrid.IntegrationTests/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using StrongGrid.Logging;
using Logzio.DotNet.NLog;
using NLog;
using NLog.Config;
using NLog.Targets;
using StrongGrid.Models;
using StrongGrid.Models.Search;
using StrongGrid.Utilities;
Expand Down Expand Up @@ -30,23 +33,42 @@ static async Task<int> Main()
// Do you want to proxy requests through Fiddler? Can be useful for debugging.
var useFiddler = false;

// As an alternative to Fiddler, you can display debug information in the console.
var logToConsole = true;

// Decide which calls should be logged. You can choose to log only successful calls, only failed calls, both or neither.
var logBehavior = LogBehavior.LogFailedCalls;
// Logging options.
var options = new StrongGridClientOptions()
{
LogLevelFailedCalls = StrongGrid.Logging.LogLevel.Error,
LogLevelSuccessfulCalls = StrongGrid.Logging.LogLevel.Debug
};
// -----------------------------------------------------------------------------

if (logToConsole)
// Configure logging
var nLogConfig = new LoggingConfiguration();

// Send logs to logz.io
var logzioToken = Environment.GetEnvironmentVariable("LOGZIO_TOKEN");
if (!string.IsNullOrEmpty(logzioToken))
{
LogProvider.SetCurrentLogProvider(new ColoredConsoleLogProvider());
var logzioTarget = new LogzioTarget { Token = logzioToken };
logzioTarget.ContextProperties.Add(new TargetPropertyWithContext("source", "StrongGrid_integration_tests"));
logzioTarget.ContextProperties.Add(new TargetPropertyWithContext("StrongGrid-Version", StrongGrid.Client.Version));

nLogConfig.AddTarget("Logzio", logzioTarget);
nLogConfig.AddRule(NLog.LogLevel.Debug, NLog.LogLevel.Fatal, "Logzio", "*");
}

// Send logs to console
var consoleTarget = new ColoredConsoleTarget();
nLogConfig.AddTarget("ColoredConsole", consoleTarget);
nLogConfig.AddRule(NLog.LogLevel.Warn, NLog.LogLevel.Fatal, "ColoredConsole", "*");

LogManager.Configuration = nLogConfig;

// Configure StrongGrid client
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
var proxy = useFiddler ? new WebProxy("http://localhost:8888") : null;
var options = new StrongGridClientOptions() { LogBehavior = logBehavior };
var client = new Client(apiKey, proxy, options);

// Configure Console
var source = new CancellationTokenSource();
Console.CancelKeyPress += (s, e) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Logzio.DotNet.NLog" Version="1.0.6" />
<PackageReference Include="System.Collections.NonGeneric" Version="4.3.0" />
<PackageReference Include="System.Net.NetworkInformation" Version="4.3.0" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Source/StrongGrid.UnitTests/StrongGrid.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.0" />
<PackageReference Include="Moq" Version="4.10.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" />
<PackageReference Include="Moq" Version="4.11.0" />
<PackageReference Include="RichardSzalay.MockHttp" Version="5.0.0" />
<PackageReference Include="Shouldly" Version="3.0.2" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down
3 changes: 2 additions & 1 deletion Source/StrongGrid.UnitTests/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Pathoschild.Http.Client;
using Pathoschild.Http.Client.Extensibility;
using RichardSzalay.MockHttp;
using StrongGrid.Logging;
using StrongGrid.Utilities;
using System.Linq;

Expand All @@ -16,7 +17,7 @@ public static Pathoschild.Http.Client.IClient GetFluentClient(MockHttpMessageHan
var client = new FluentClient(SENDGRID_API_BASE_URI, httpClient);
client.SetRequestCoordinator(new SendGridRetryStrategy());
client.Filters.Remove<DefaultErrorFilter>();
client.Filters.Add(new DiagnosticHandler(LogBehavior.LogNothing));
client.Filters.Add(new DiagnosticHandler(LogLevel.Debug, LogLevel.Error));
client.Filters.Add(new SendGridErrorHandler());
return client;
}
Expand Down
51 changes: 34 additions & 17 deletions Source/StrongGrid/Client.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Pathoschild.Http.Client;
using Pathoschild.Http.Client.Extensibility;
using StrongGrid.Logging;
using StrongGrid.Resources;
using StrongGrid.Utilities;
using System;
Expand Down Expand Up @@ -28,6 +29,19 @@ public class Client : IClient, IDisposable

#region PROPERTIES

/// <summary>
/// Gets the Version.
/// </summary>
/// <value>
/// The version.
/// </value>
public static string Version { get; private set; }

/// <summary>
/// Gets the user agent.
/// </summary>
public static string UserAgent { get; private set; }

/// <summary>
/// Gets the Access Management resource which allows you to control IP whitelisting.
/// </summary>
Expand Down Expand Up @@ -252,14 +266,6 @@ public class Client : IClient, IDisposable
/// </value>
public IUser User { get; private set; }

/// <summary>
/// Gets the Version.
/// </summary>
/// <value>
/// The version.
/// </value>
public string Version { get; private set; }

/// <summary>
/// Gets the SenderAuthentication resource.
/// </summary>
Expand Down Expand Up @@ -288,13 +294,25 @@ public class Client : IClient, IDisposable

#region CTOR

/// <summary>
/// Initializes static members of the <see cref="Client"/> class.
/// </summary>
static Client()
{
Version = typeof(Client).GetTypeInfo().Assembly.GetName().Version.ToString(3);
#if DEBUG
Version = "DEBUG";
#endif
UserAgent = $"StrongGrid/{Version} (+https://github.com/Jericho/StrongGrid)";
}

/// <summary>
/// Initializes a new instance of the <see cref="Client"/> class.
/// </summary>
/// <param name="apiKey">Your SendGrid API Key.</param>
/// <param name="options">Options for the SendGrid client.</param>
public Client(string apiKey, StrongGridClientOptions options = null)
: this(apiKey, null, null, null, false, options)
: this(apiKey, null, null, null, false, options)
{
}

Expand Down Expand Up @@ -384,20 +402,15 @@ private Client(string apiKey, string username, string password, HttpClient httpC
_httpClient = httpClient;
_options = options ?? GetDefaultOptions();

Version = typeof(Client).GetTypeInfo().Assembly.GetName().Version.ToString(3);
#if DEBUG
Version = "DEBUG";
#endif

_fluentClient = new FluentClient(new Uri(SENDGRID_V3_BASE_URI), httpClient)
.SetUserAgent($"StrongGrid/{Version} (+https://github.com/Jericho/StrongGrid)")
.SetUserAgent(Client.UserAgent)
.SetRequestCoordinator(new SendGridRetryStrategy());

_fluentClient.Filters.Remove<DefaultErrorFilter>();

// Order is important: DiagnosticHandler must be first.
// Also, the list of filters must be kept in sync with the filters in Utils.GetFluentClient in the unit testing project.
_fluentClient.Filters.Add(new DiagnosticHandler(_options.LogBehavior));
_fluentClient.Filters.Add(new DiagnosticHandler(_options.LogLevelSuccessfulCalls, _options.LogLevelFailedCalls));
_fluentClient.Filters.Add(new SendGridErrorHandler());

if (!string.IsNullOrEmpty(apiKey)) _fluentClient.SetBearerAuthentication(apiKey);
Expand Down Expand Up @@ -510,7 +523,11 @@ private StrongGridClientOptions GetDefaultOptions()
{
return new StrongGridClientOptions()
{
LogBehavior = LogBehavior.LogEverything
// Setting to 'Debug' to mimic previous behavior. I think this is a sensible default setting.
LogLevelSuccessfulCalls = LogLevel.Debug,

// Setting to 'Debug' to mimic previous behavior. I think 'Error' would make more sense.
LogLevelFailedCalls = LogLevel.Debug
};
}

Expand Down
10 changes: 1 addition & 9 deletions Source/StrongGrid/IClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using StrongGrid.Resources;
using StrongGrid.Resources;

namespace StrongGrid
{
Expand Down Expand Up @@ -231,14 +231,6 @@ public interface IClient
/// </value>
IUser User { get; }

/// <summary>
/// Gets the Version.
/// </summary>
/// <value>
/// The version.
/// </value>
string Version { get; }

/// <summary>
/// Gets the WebhookSettings resource.
/// </summary>
Expand Down
Loading

0 comments on commit 916fa54

Please sign in to comment.