/// <summary> /// Initializes a new instance of the <see cref="BaseClient" /> class. /// </summary> /// <param name="apiKey">Your api key.</param> /// <param name="httpClient">Allows you to inject your own HttpClient. This is useful, for example, to setup the HtppClient with a proxy.</param> /// <param name="disposeClient">Indicates if the http client should be dispose when this instance of BaseClient is disposed.</param> /// <param name="options">Options for the SendGrid client.</param> /// <param name="logger">Logger.</param> public BaseClient(string apiKey, HttpClient httpClient, bool disposeClient, StrongGridClientOptions options, ILogger logger = null) { _mustDisposeHttpClient = disposeClient; _httpClient = httpClient; _options = options; _logger = logger ?? NullLogger.Instance; _fluentClient = new FluentClient(new Uri(SENDGRID_V3_BASE_URI), httpClient) .SetUserAgent($"StrongGrid/{Version} (+https://github.com/Jericho/StrongGrid)") .SetRequestCoordinator(new SendGridRetryStrategy()); _fluentClient.Filters.Remove <DefaultErrorFilter>(); // Remove all the built-in formatters and replace them with our custom JSON formatter _fluentClient.Formatters.Clear(); _fluentClient.Formatters.Add(new JsonFormatter()); // 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.LogLevelSuccessfulCalls, _options.LogLevelFailedCalls, _logger)); _fluentClient.Filters.Add(new SendGridErrorHandler()); if (string.IsNullOrEmpty(apiKey)) { throw new ArgumentNullException(apiKey); } _fluentClient.SetBearerAuthentication(apiKey); AccessManagement = new AccessManagement(FluentClient); Alerts = new Alerts(FluentClient); ApiKeys = new ApiKeys(FluentClient); Batches = new Batches(FluentClient); Blocks = new Blocks(FluentClient); Bounces = new Bounces(FluentClient); Designs = new Designs(FluentClient); EmailActivities = new EmailActivities(FluentClient); EmailValidation = new EmailValidation(FluentClient); GlobalSuppressions = new GlobalSuppressions(FluentClient); InvalidEmails = new InvalidEmails(FluentClient); IpAddresses = new IpAddresses(FluentClient); IpPools = new IpPools(FluentClient); Mail = new Mail(FluentClient); Settings = new Settings(FluentClient); SpamReports = new SpamReports(FluentClient); Statistics = new Statistics(FluentClient); Subusers = new Subusers(FluentClient); Suppressions = new Suppressions(FluentClient); Teammates = new Teammates(FluentClient); Templates = new Templates(FluentClient); UnsubscribeGroups = new UnsubscribeGroups(FluentClient); User = new User(FluentClient); WebhookSettings = new WebhookSettings(FluentClient); WebhookStats = new WebhookStats(FluentClient); SenderAuthentication = new SenderAuthentication(FluentClient); }
public async Task GetGlobalStatsAsync() { // Arrange var startDate = new DateTime(2015, 1, 1); var endDate = new DateTime(2015, 1, 2); var apiResponse = @"[ { 'date': '2015-01-01', 'stats': [ { 'metrics': { 'received': 1 } } ] }, { 'date': '2015-01-02', 'stats': [ { 'metrics': { 'received': 3 } } ] } ]" ; var mockHttp = new MockHttpMessageHandler(); mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri($"user/webhooks/parse/stats?start_date={startDate.ToString("yyyy-MM-dd")}&end_date={ endDate.ToString("yyyy-MM-dd")}")).Respond("application/json", apiResponse); var client = Utils.GetFluentClient(mockHttp); var webhookStats = new WebhookStats(client); // Act var result = await webhookStats.GetInboundParseUsageAsync(startDate, endDate, AggregateBy.None, CancellationToken.None).ConfigureAwait(false); // Assert mockHttp.VerifyNoOutstandingExpectation(); mockHttp.VerifyNoOutstandingRequest(); result.ShouldNotBeNull(); result.Length.ShouldBe(2); result[0].Stats.Length.ShouldBe(1); result[0].Stats[0].Metrics.Length.ShouldBe(1); result[0].Stats[0].Metrics.Single(m => m.Key == "received").Value.ShouldBe(1); result[1].Stats[0].Metrics.Length.ShouldBe(1); result[1].Stats[0].Metrics.Single(m => m.Key == "received").Value.ShouldBe(3); }
/// <summary> /// Initializes a new instance of the <see cref="BaseClient" /> class. /// </summary> /// <param name="apiKey">Your api key.</param> /// <param name="username">Your username. Ignored if the api key is specified.</param> /// <param name="password">Your password. Ignored if the api key is specified.</param> /// <param name="httpClient">Allows you to inject your own HttpClient. This is useful, for example, to setup the HtppClient with a proxy.</param> /// <param name="disposeClient">Indicates if the http client should be dispose when this instance of BaseClient is disposed.</param> /// <param name="options">Options for the SendGrid client.</param> /// <param name="logger">Logger.</param> public BaseClient(Parameter <string> apiKey, Parameter <string> username, Parameter <string> password, HttpClient httpClient, bool disposeClient, StrongGridClientOptions options, ILogger logger = null) { _mustDisposeHttpClient = disposeClient; _httpClient = httpClient; _options = options ?? GetDefaultOptions(); _logger = logger ?? NullLogger.Instance; _fluentClient = new FluentClient(new Uri(SENDGRID_V3_BASE_URI), httpClient) .SetUserAgent($"StrongGrid/{Version} (+https://github.com/Jericho/StrongGrid)") .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.LogLevelSuccessfulCalls, _options.LogLevelFailedCalls, _logger)); _fluentClient.Filters.Add(new SendGridErrorHandler()); if (apiKey.HasValue) { if (string.IsNullOrEmpty(apiKey)) { throw new ArgumentNullException(apiKey); } else { _fluentClient.SetBearerAuthentication(apiKey); } } else if (username.HasValue) { if (string.IsNullOrEmpty(username)) { throw new ArgumentNullException(username); } else { _fluentClient.SetBasicAuthentication(username, password); } } else { throw new ArgumentException("You must provide either an API key or a username and a password."); } AccessManagement = new AccessManagement(FluentClient); Alerts = new Alerts(FluentClient); ApiKeys = new ApiKeys(FluentClient); Batches = new Batches(FluentClient); Blocks = new Blocks(FluentClient); Bounces = new Bounces(FluentClient); Designs = new Designs(FluentClient); EmailActivities = new EmailActivities(FluentClient); EmailValidation = new EmailValidation(FluentClient); GlobalSuppressions = new GlobalSuppressions(FluentClient); InvalidEmails = new InvalidEmails(FluentClient); IpAddresses = new IpAddresses(FluentClient); IpPools = new IpPools(FluentClient); Mail = new Mail(FluentClient); Settings = new Settings(FluentClient); SpamReports = new SpamReports(FluentClient); Statistics = new Statistics(FluentClient); Subusers = new Subusers(FluentClient); Suppressions = new Suppressions(FluentClient); Teammates = new Teammates(FluentClient); Templates = new Templates(FluentClient); UnsubscribeGroups = new UnsubscribeGroups(FluentClient); User = new User(FluentClient); WebhookSettings = new WebhookSettings(FluentClient); WebhookStats = new WebhookStats(FluentClient); SenderAuthentication = new SenderAuthentication(FluentClient); }