Skip to content

Commit

Permalink
Merge pull request #33 from mausworks/bug/di-issues
Browse files Browse the repository at this point in the history
Resolve dependency resolving issues
  • Loading branch information
mausworks authored Feb 28, 2018
2 parents 19b8d55 + 5675db9 commit 429ac3e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/Pidget.AspNet/Setup/SetupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static IServiceCollection AddPidgetMiddleware(
this IServiceCollection services,
Action<SentryOptions> setup)
=> services.Configure<SentryOptions>(setup)
.AddScoped<SentryClient>(ClientFactory.CreateClient)
.AddSingleton<SentryClient>(ClientFactory.CreateClient)
.AddSingleton<RateLimit>();

public static IApplicationBuilder UsePidgetMiddleware(
Expand Down
4 changes: 4 additions & 0 deletions src/Pidget.Client/ErrorLevel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Pidget.Client
{
[JsonConverter(typeof(StringEnumConverter), true)]
public enum ErrorLevel : byte
{
Error,
Expand Down
41 changes: 15 additions & 26 deletions src/Pidget.Client/Http/SentryHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@

namespace Pidget.Client.Http
{
public class SentryHttpClient : SentryClient, IDisposable
public class SentryHttpClient : SentryClient
{
public static TimeSpan Timeout { get; }
= TimeSpan.FromSeconds(3);

public static JsonSerializer JsonSerializer { get; }
= GetJsonSerializer();

public static string UserAgent { get; }
= string.Join("/", Name, Version);

private static readonly JsonStreamSerializer _streamSerializer
private static readonly JsonStreamSerializer _serializer
= new JsonStreamSerializer(
encoding: Sentry.ApiEncoding,
jsonSerializer: JsonSerializer);
jsonSerializer: JsonSerializer.CreateDefault());

private readonly HttpMessageInvoker _sender;

Expand All @@ -38,16 +35,6 @@ public SentryHttpClient(Dsn dsn, HttpMessageInvoker sender)
_sender = sender;
}

private static JsonSerializer GetJsonSerializer()
{
var settings = new JsonSerializerSettings();

settings.Converters.Add(
new StringEnumConverter(camelCaseText: true));

return JsonSerializer.Create(settings);
}

public override async Task<SentryResponse> SendEventAsync(
SentryEventData eventData)
{
Expand All @@ -56,21 +43,23 @@ public override async Task<SentryResponse> SendEventAsync(
return SentryResponse.Empty;
}

using (var stream = _streamSerializer.Serialize(eventData))
using (var stream = _serializer.Serialize(eventData))
{
var httpResponse = await _sender
.SendAsync(ComposeMessage(stream), None)
.ConfigureAwait(false);

var responseProvider = new SentryResponseProvider(_streamSerializer);
using (var res = await SendMessage(stream))
{
var responseProvider = new SentryResponseProvider(_serializer);

return await responseProvider.GetResponseAsync(httpResponse);
return await responseProvider.GetResponseAsync(res);
}
}
}

public void Dispose() => _sender.Dispose();
private async Task<HttpResponseMessage> SendMessage(Stream stream)
=> await _sender
.SendAsync(ComposeMessage(stream), None)
.ConfigureAwait(false);

public static HttpClient CreateHttpClient()
public static HttpClient CreateDefaultHttpClient()
{
var client = new HttpClient { Timeout = Timeout };

Expand All @@ -80,7 +69,7 @@ public static HttpClient CreateHttpClient()
}

public static SentryHttpClient Default(Dsn dsn)
=> new SentryHttpClient(dsn, CreateHttpClient());
=> new SentryHttpClient(dsn, CreateDefaultHttpClient());

private HttpRequestMessage ComposeMessage(Stream stream)
{
Expand Down
4 changes: 2 additions & 2 deletions test/Pidget.AspNet.Test/Setup/SetupExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ namespace Pidget.AspNet.Setup
public class SetupExtensionsTests
{
[Fact]
public void AddPidgetMiddleware_AddsScopedClient()
public void AddPidgetMiddleware_AddsSingletonClient()
{
var servicesMock = new Mock<IServiceCollection>();

servicesMock.Setup(m => m
.Add(It.Is<ServiceDescriptor>(s
=> s.ServiceType == typeof(SentryClient)
&& s.Lifetime == ServiceLifetime.Scoped)))
&& s.Lifetime == ServiceLifetime.Singleton)))
.Verifiable();

servicesMock.Object.AddPidgetMiddleware(_ => {});
Expand Down
17 changes: 2 additions & 15 deletions test/Pidget.Client.Test/Http/SentryHttpClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void RequiresHttpClient()
[Fact]
public void Sender_HasExpectedUserAgent()
{
var httpClient = SentryHttpClient.CreateHttpClient();
var httpClient = SentryHttpClient.CreateDefaultHttpClient();

Assert.Equal(SentryHttpClient.UserAgent,
httpClient.DefaultRequestHeaders.UserAgent.ToString());
Expand All @@ -49,7 +49,7 @@ public async Task SendMessageEvent(string message)
senderMock.Object);

senderMock.Setup(m => m.SendAsync(It.IsAny<HttpRequestMessage>(), None))
.ReturnsAsync(CreateOkHttpResponse(SentryHttpClient.JsonSerializer))
.ReturnsAsync(CreateOkHttpResponse(JsonSerializer.CreateDefault()))
.Verifiable();

var response = await client.SendEventAsync(new SentryEventData
Expand Down Expand Up @@ -82,19 +82,6 @@ public async Task DisabledClient_ReturnsEmptyResponse()
Assert.Null(response.EventId);
}

[Fact]
public void DisposesSender()
{
var sender = new TestSender(new HttpClientHandler());

var client = new SentryHttpClient(DsnTests.SentryDsn,
sender);

client.Dispose();

Assert.True(sender.IsDisposed);
}

[Fact]
public void CreateDefault()
=> Assert.NotNull(
Expand Down

0 comments on commit 429ac3e

Please sign in to comment.