Skip to content

Commit 777282b

Browse files
committed
Fixed an issue where submission clients would make requests when the configuration was invalid.
1 parent 52e85e9 commit 777282b

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/Exceptionless/Extensions/ExceptionlessClientExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public static class ExceptionlessClientExtensions {
1616
/// <param name="client">The ExceptionlessClient.</param>
1717
/// <param name="apiKey">The API key that will be used when sending events to the server.</param>
1818
public static void Startup(this ExceptionlessClient client, string apiKey = null) {
19-
client.Configuration.ApiKey = apiKey;
19+
if (!String.IsNullOrEmpty(apiKey))
20+
client.Configuration.ApiKey = apiKey;
21+
2022
client.Configuration.ReadAllConfig();
2123

2224
#if !PORTABLE && !NETSTANDARD1_2
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Net.Http.Headers;
4+
using Exceptionless.Submission.Net;
5+
6+
namespace Exceptionless.Extensions {
7+
internal static class HttpClientExtensions {
8+
public static void AddAuthorizationHeader(this HttpClient client, string apiKey) {
9+
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(ExceptionlessHeaders.Bearer, apiKey);
10+
}
11+
}
12+
}

src/Exceptionless/Submission/DefaultSubmissionClient.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ public class DefaultSubmissionClient : ISubmissionClient, IDisposable {
1818
private readonly HttpClient _client;
1919

2020
public DefaultSubmissionClient(ExceptionlessConfiguration config) {
21-
_client = CreateHttpClient(config);
21+
_client = CreateHttpClient(config.UserAgent);
2222
}
2323

2424
public SubmissionResponse PostEvents(IEnumerable<Event> events, ExceptionlessConfiguration config, IJsonSerializer serializer) {
25+
if (!config.IsValid)
26+
return new SubmissionResponse(500, message: "Invalid client configuration settings");
27+
2528
string data = serializer.Serialize(events);
2629
string url = String.Format("{0}/events", GetServiceEndPoint(config));
2730

@@ -33,6 +36,7 @@ public SubmissionResponse PostEvents(IEnumerable<Event> events, ExceptionlessCon
3336
if (data.Length > 1024 * 4)
3437
content = new GzipContent(content);
3538

39+
_client.AddAuthorizationHeader(config.ApiKey);
3640
response = _client.PostAsync(url, content).ConfigureAwait(false).GetAwaiter().GetResult();
3741
} catch (Exception ex) {
3842
return new SubmissionResponse(500, message: ex.Message);
@@ -46,6 +50,9 @@ public SubmissionResponse PostEvents(IEnumerable<Event> events, ExceptionlessCon
4650
}
4751

4852
public SubmissionResponse PostUserDescription(string referenceId, UserDescription description, ExceptionlessConfiguration config, IJsonSerializer serializer) {
53+
if (!config.IsValid)
54+
return new SubmissionResponse(500, message: "Invalid client configuration settings.");
55+
4956
string data = serializer.Serialize(description);
5057
string url = String.Format("{0}/events/by-ref/{1}/user-description", GetServiceEndPoint(config), referenceId);
5158

@@ -57,6 +64,7 @@ public SubmissionResponse PostUserDescription(string referenceId, UserDescriptio
5764
if (data.Length > 1024 * 4)
5865
content = new GzipContent(content);
5966

67+
_client.AddAuthorizationHeader(config.ApiKey);
6068
response = _client.PostAsync(url, content).ConfigureAwait(false).GetAwaiter().GetResult();
6169
} catch (Exception ex) {
6270
return new SubmissionResponse(500, message: ex.Message);
@@ -69,11 +77,15 @@ public SubmissionResponse PostUserDescription(string referenceId, UserDescriptio
6977
return new SubmissionResponse((int)response.StatusCode, GetResponseMessage(response));
7078
}
7179

72-
public SettingsResponse GetSettings(ExceptionlessConfiguration config, int version, IJsonSerializer serializer) {
80+
public SettingsResponse GetSettings(ExceptionlessConfiguration config, int version, IJsonSerializer serializer) {
81+
if (!config.IsValid)
82+
return new SettingsResponse(false, message: "Invalid client configuration settings.");
83+
7384
string url = String.Format("{0}/projects/config?v={1}", GetServiceEndPoint(config), version);
7485

7586
HttpResponseMessage response;
7687
try {
88+
_client.AddAuthorizationHeader(config.ApiKey);
7789
response = _client.GetAsync(url).ConfigureAwait(false).GetAwaiter().GetResult();
7890
} catch (Exception ex) {
7991
var message = String.Concat("Unable to retrieve configuration settings. Exception: ", ex.GetMessage());
@@ -95,16 +107,20 @@ public SettingsResponse GetSettings(ExceptionlessConfiguration config, int versi
95107
}
96108

97109
public void SendHeartbeat(string sessionIdOrUserId, bool closeSession, ExceptionlessConfiguration config) {
110+
if (!config.IsValid)
111+
return;
112+
98113
string url = String.Format("{0}/events/session/heartbeat?id={1}&close={2}", GetHeartbeatServiceEndPoint(config), sessionIdOrUserId, closeSession);
99114
try {
115+
_client.AddAuthorizationHeader(config.ApiKey);
100116
_client.GetAsync(url).ConfigureAwait(false).GetAwaiter().GetResult();
101117
} catch (Exception ex) {
102118
var log = config.Resolver.GetLog();
103119
log.Error(String.Concat("Error submitting heartbeat: ", ex.GetMessage()));
104120
}
105121
}
106122

107-
private HttpClient CreateHttpClient(ExceptionlessConfiguration config) {
123+
private HttpClient CreateHttpClient(string userAgent) {
108124
#if !NET45
109125
var handler = new HttpClientHandler { UseDefaultCredentials = true };
110126

@@ -123,9 +139,8 @@ private HttpClient CreateHttpClient(ExceptionlessConfiguration config) {
123139

124140
var client = new HttpClient(handler, true);
125141
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
126-
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(ExceptionlessHeaders.Bearer, config.ApiKey);
127142
client.DefaultRequestHeaders.ExpectContinue = false;
128-
client.DefaultRequestHeaders.UserAgent.ParseAdd(config.UserAgent);
143+
client.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent);
129144

130145
return client;
131146
}

0 commit comments

Comments
 (0)