• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Jericho / StrongGrid / 1278

07 May 2025 01:31PM UTC coverage: 73.904% (+0.03%) from 73.876%
1278

push

appveyor

Jericho
Refresh build script

2614 of 3537 relevant lines covered (73.9%)

87.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.77
/Source/StrongGrid/BaseClient.cs
1
using Microsoft.Extensions.Logging;
2
using Microsoft.Extensions.Logging.Abstractions;
3
using Pathoschild.Http.Client;
4
using Pathoschild.Http.Client.Extensibility;
5
using StrongGrid.Json;
6
using StrongGrid.Resources;
7
using StrongGrid.Utilities;
8
using System;
9
using System.Net.Http;
10
using System.Reflection;
11

12
namespace StrongGrid
13
{
14
        /// <summary>
15
        /// Base class for StrongGrid's REST clients.
16
        /// </summary>
17
        public abstract class BaseClient : IBaseClient, IDisposable
18
        {
19
                #region FIELDS
20

21
                private const string SENDGRID_V3_BASE_URI = "https://api.sendgrid.com/v3";
22

23
                private static string _version;
24

25
                private readonly bool _mustDisposeHttpClient;
26
                private readonly StrongGridClientOptions _options;
27
                private readonly ILogger _logger;
28

29
                private HttpClient _httpClient;
30
                private Pathoschild.Http.Client.IClient _fluentClient;
31

32
                #endregion
33

34
                #region PROPERTIES
35

36
                /// <summary>
37
                /// Gets the Version.
38
                /// </summary>
39
                /// <value>
40
                /// The version.
41
                /// </value>
42
                public static string Version
43
                {
44
                        get
45
                        {
46
                                if (string.IsNullOrEmpty(_version))
4✔
47
                                {
48
                                        _version = typeof(Client).GetTypeInfo().Assembly.GetName().Version.ToString(3);
1✔
49
#if DEBUG
50
                                        _version = "DEBUG";
51
#endif
52
                                }
53

54
                                return _version;
4✔
55
                        }
56
                }
57

58
                /// <summary>
59
                /// Gets the Access Management resource which allows you to control IP whitelisting.
60
                /// </summary>
61
                /// <value>
62
                /// The access management.
63
                /// </value>
64
                public IAccessManagement AccessManagement { get; private set; }
65

66
                /// <summary>
67
                /// Gets the Alerts resource which allows you to receive notifications regarding your email usage or statistics.
68
                /// </summary>
69
                /// <value>
70
                /// The alerts.
71
                /// </value>
72
                public IAlerts Alerts { get; private set; }
73

74
                /// <summary>
75
                /// Gets the API Keys resource which allows you to manage your API Keys.
76
                /// </summary>
77
                /// <value>
78
                /// The API keys.
79
                /// </value>
80
                public IApiKeys ApiKeys { get; private set; }
81

82
                /// <summary>
83
                /// Gets the Batches resource.
84
                /// </summary>
85
                /// <value>
86
                /// The batches.
87
                /// </value>
88
                public IBatches Batches { get; private set; }
89

90
                /// <summary>
91
                /// Gets the Blocks resource which allows you to manage blacked email addresses.
92
                /// </summary>
93
                /// <value>
94
                /// The blocks.
95
                /// </value>
96
                public IBlocks Blocks { get; private set; }
97

98
                /// <summary>
99
                /// Gets the Bounces resource which allows you to manage bounces.
100
                /// </summary>
101
                /// <value>
102
                /// The bounces.
103
                /// </value>
104
                public IBounces Bounces { get; private set; }
105

106
                /// <summary>
107
                /// Gets the Designs resource which allows you to manage designs.
108
                /// </summary>
109
                public IDesigns Designs { get; private set; }
110

111
                /// <summary>
112
                /// Gets the EmailActivities resource which allows you to search and download a CSV of your recent email event activity.
113
                /// </summary>
114
                /// <value>
115
                /// The email activities.
116
                /// </value>
117
                public IEmailActivities EmailActivities { get; private set; }
118

119
                /// <summary>
120
                /// Gets the validation resource.
121
                /// </summary>
122
                public IEmailValidation EmailValidation { get; private set; }
123

124
                /// <summary>
125
                /// Gets the GlobalSuppressions resource.
126
                /// </summary>
127
                /// <value>
128
                /// The global suppressions.
129
                /// </value>
130
                public IGlobalSuppressions GlobalSuppressions { get; private set; }
131

132
                /// <summary>
133
                /// Gets the InvalidEmails resource.
134
                /// </summary>
135
                /// <value>
136
                /// The invalid emails.
137
                /// </value>
138
                public IInvalidEmails InvalidEmails { get; private set; }
139

140
                /// <summary>
141
                /// Gets the IpAddresses resource.
142
                /// </summary>
143
                /// <value>
144
                /// The IP addresses.
145
                /// </value>
146
                public IIpAddresses IpAddresses { get; private set; }
147

148
                /// <summary>
149
                /// Gets the IpPools resource.
150
                /// </summary>
151
                /// <value>
152
                /// The IP pools..
153
                /// </value>
154
                public IIpPools IpPools { get; private set; }
155

156
                /// <summary>
157
                /// Gets the Mail resource.
158
                /// </summary>
159
                /// <value>
160
                /// The mail.
161
                /// </value>
162
                public IMail Mail { get; private set; }
163

164
                /// <summary>
165
                /// Gets the SenderAuthentication resource.
166
                /// </summary>
167
                /// <value>
168
                /// The <see cref="ISenderAuthentication"/>.
169
                /// </value>
170
                public ISenderAuthentication SenderAuthentication { get; private set; }
171

172
                /// <summary>
173
                /// Gets the Settings resource.
174
                /// </summary>
175
                /// <value>
176
                /// The settings.
177
                /// </value>
178
                public ISettings Settings { get; private set; }
179

180
                /// <summary>
181
                /// Gets the SpamReports resource.
182
                /// </summary>
183
                /// <value>
184
                /// The spam reports.
185
                /// </value>
186
                public ISpamReports SpamReports { get; private set; }
187

188
                /// <summary>
189
                /// Gets the Statistics resource.
190
                /// </summary>
191
                /// <value>
192
                /// The statistics.
193
                /// </value>
194
                public IStatistics Statistics { get; private set; }
195

196
                /// <summary>
197
                /// Gets the Subusers resource.
198
                /// </summary>
199
                /// <value>
200
                /// The subusers.
201
                /// </value>
202
                public ISubusers Subusers { get; private set; }
203

204
                /// <summary>
205
                /// Gets the Suppressions resource.
206
                /// </summary>
207
                /// <value>
208
                /// The suppressions.
209
                /// </value>
210
                public ISuppressions Suppressions { get; private set; }
211

212
                /// <summary>
213
                /// Gets the Teammates resource.
214
                /// </summary>
215
                /// <value>
216
                /// The Teammates.
217
                /// </value>
218
                public ITeammates Teammates { get; private set; }
219

220
                /// <summary>
221
                /// Gets the Templates resource.
222
                /// </summary>
223
                /// <value>
224
                /// The templates.
225
                /// </value>
226
                public ITemplates Templates { get; private set; }
227

228
                /// <summary>
229
                /// Gets the UnsubscribeGroups resource.
230
                /// </summary>
231
                /// <value>
232
                /// The unsubscribe groups.
233
                /// </value>
234
                public IUnsubscribeGroups UnsubscribeGroups { get; private set; }
235

236
                /// <summary>
237
                /// Gets the User resource.
238
                /// </summary>
239
                /// <value>
240
                /// The user.
241
                /// </value>
242
                public IUser User { get; private set; }
243

244
                /// <summary>
245
                /// Gets the webhook settings resource.
246
                /// </summary>
247
                /// <value>
248
                /// The webhook settings.
249
                /// </value>
250
                public IWebhookSettings WebhookSettings { get; private set; }
251

252
                /// <summary>
253
                /// Gets the WebhookStats resource.
254
                /// </summary>
255
                /// <value>
256
                /// The webhook stats.
257
                /// </value>
258
                public IWebhookStats WebhookStats { get; private set; }
259

260
                internal Pathoschild.Http.Client.IClient FluentClient
261
                {
262
                        get { return _fluentClient; }
33✔
263
                }
264

265
                #endregion
266

267
                #region CTOR
268

269
                /// <summary>
270
                /// Initializes a new instance of the <see cref="BaseClient" /> class.
271
                /// </summary>
272
                /// <param name="apiKey">Your api key.</param>
273
                /// <param name="httpClient">Allows you to inject your own HttpClient. This is useful, for example, to setup the HtppClient with a proxy.</param>
274
                /// <param name="disposeClient">Indicates if the http client should be dispose when this instance of BaseClient is disposed.</param>
275
                /// <param name="options">Options for the SendGrid client.</param>
276
                /// <param name="logger">Logger.</param>
277
                public BaseClient(string apiKey, HttpClient httpClient, bool disposeClient, StrongGridClientOptions options, ILogger logger = null)
278
                {
279
                        _mustDisposeHttpClient = disposeClient;
3✔
280
                        _httpClient = httpClient;
3✔
281
                        _options = options;
3✔
282
                        _logger = logger ?? NullLogger.Instance;
3✔
283

284
                        _fluentClient = new FluentClient(new Uri(SENDGRID_V3_BASE_URI), httpClient)
3✔
285
                                .SetUserAgent($"StrongGrid/{Version} (+https://github.com/Jericho/StrongGrid)")
3✔
286
                                .SetRequestCoordinator(new SendGridRetryStrategy());
3✔
287

288
                        _fluentClient.Filters.Remove<DefaultErrorFilter>();
3✔
289

290
                        // Remove all the built-in formatters and replace them with our custom JSON formatter
291
                        _fluentClient.Formatters.Clear();
3✔
292
                        _fluentClient.Formatters.Add(new JsonFormatter());
3✔
293

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

299
                        if (string.IsNullOrEmpty(apiKey)) throw new ArgumentNullException(apiKey);
5✔
300
                        _fluentClient.SetBearerAuthentication(apiKey);
1✔
301

302
                        AccessManagement = new AccessManagement(FluentClient);
1✔
303
                        Alerts = new Alerts(FluentClient);
1✔
304
                        ApiKeys = new ApiKeys(FluentClient);
1✔
305
                        Batches = new Batches(FluentClient);
1✔
306
                        Blocks = new Blocks(FluentClient);
1✔
307
                        Bounces = new Bounces(FluentClient);
1✔
308
                        Designs = new Designs(FluentClient);
1✔
309
                        EmailActivities = new EmailActivities(FluentClient);
1✔
310
                        EmailValidation = new EmailValidation(FluentClient);
1✔
311
                        GlobalSuppressions = new GlobalSuppressions(FluentClient);
1✔
312
                        InvalidEmails = new InvalidEmails(FluentClient);
1✔
313
                        IpAddresses = new IpAddresses(FluentClient);
1✔
314
                        IpPools = new IpPools(FluentClient);
1✔
315
                        Mail = new Mail(FluentClient);
1✔
316
                        Settings = new Settings(FluentClient);
1✔
317
                        SpamReports = new SpamReports(FluentClient);
1✔
318
                        Statistics = new Statistics(FluentClient);
1✔
319
                        Subusers = new Subusers(FluentClient);
1✔
320
                        Suppressions = new Suppressions(FluentClient);
1✔
321
                        Teammates = new Teammates(FluentClient);
1✔
322
                        Templates = new Templates(FluentClient);
1✔
323
                        UnsubscribeGroups = new UnsubscribeGroups(FluentClient);
1✔
324
                        User = new User(FluentClient);
1✔
325
                        WebhookSettings = new WebhookSettings(FluentClient);
1✔
326
                        WebhookStats = new WebhookStats(FluentClient);
1✔
327
                        SenderAuthentication = new SenderAuthentication(FluentClient);
1✔
328
                }
1✔
329

330
                /// <summary>
331
                /// Finalizes an instance of the <see cref="BaseClient"/> class.
332
                /// </summary>
333
                ~BaseClient()
334
                {
335
                        // The object went out of scope and finalized is called.
336
                        // Call 'Dispose' to release unmanaged resources
337
                        // Managed resources will be released when GC runs the next time.
338
                        Dispose(false);
×
339
                }
×
340

341
                #endregion
342

343
                #region PUBLIC METHODS
344

345
                /// <summary>
346
                /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
347
                /// </summary>
348
                public void Dispose()
349
                {
350
                        // Call 'Dispose' to release resources
351
                        Dispose(true);
1✔
352

353
                        // Tell the GC that we have done the cleanup and there is nothing left for the Finalizer to do
354
                        GC.SuppressFinalize(this);
1✔
355
                }
1✔
356

357
                /// <summary>
358
                /// Releases unmanaged and - optionally - managed resources.
359
                /// </summary>
360
                /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
361
                protected virtual void Dispose(bool disposing)
362
                {
363
                        if (disposing)
1✔
364
                        {
365
                                ReleaseManagedResources();
1✔
366
                        }
367
                        else
368
                        {
369
                                // The object went out of scope and the Finalizer has been called.
370
                                // The GC will take care of releasing managed resources, therefore there is nothing to do here.
371
                        }
372

373
                        ReleaseUnmanagedResources();
1✔
374
                }
1✔
375

376
                #endregion
377

378
                #region PRIVATE METHODS
379

380
                private void ReleaseManagedResources()
381
                {
382
                        if (_fluentClient != null)
1✔
383
                        {
384
                                _fluentClient.Dispose();
1✔
385
                                _fluentClient = null;
1✔
386
                        }
387

388
                        if (_httpClient != null && _mustDisposeHttpClient)
1✔
389
                        {
390
                                _httpClient.Dispose();
1✔
391
                                _httpClient = null;
1✔
392
                        }
393
                }
1✔
394

395
                private void ReleaseUnmanagedResources()
396
                {
397
                        // We do not hold references to unmanaged resources
398
                }
1✔
399

400
                #endregion
401
        }
402
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc