public async Task CreateAsync() { // Arrange var nickname = "My Sender ID"; var from = new MailAddress("*****@*****.**", "Example INC"); var replyTo = new MailAddress("*****@*****.**", "Example INC"); var address = "123 Elm St."; var address2 = "Apt. 456"; var city = "Denver"; var state = "Colorado"; var zip = "80202"; var country = "United States"; var mockHttp = new MockHttpMessageHandler(); mockHttp.Expect(HttpMethod.Post, Utils.GetSendGridApiUri(ENDPOINT)).Respond("application/json", SINGLE_SENDER_IDENTITY_JSON); var client = Utils.GetFluentClient(mockHttp); var senderIdentities = new SenderIdentities(client); // Act var result = await senderIdentities.CreateAsync(nickname, from, replyTo, address, address2, city, state, zip, country, null, CancellationToken.None).ConfigureAwait(false); // Assert mockHttp.VerifyNoOutstandingExpectation(); mockHttp.VerifyNoOutstandingRequest(); result.ShouldNotBeNull(); result.Id.ShouldBe(1); }
public void Create() { // Arrange var nickname = "My Sender ID"; var from = new MailAddress("*****@*****.**", "Example INC"); var replyTo = new MailAddress("*****@*****.**", "Example INC"); var address = "123 Elm St."; var address2 = "Apt. 456"; var city = "Denver"; var state = "Colorado"; var zip = "80202"; var country = "United States"; var mockRepository = new MockRepository(MockBehavior.Strict); var mockClient = mockRepository.Create <IClient>(); mockClient .Setup(c => c.PostAsync(ENDPOINT, It.IsAny <JObject>(), It.IsAny <CancellationToken>())) .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(SINGLE_SENDER_IDENTITY_JSON) }) .Verifiable(); var senderIdentities = new SenderIdentities(mockClient.Object, ENDPOINT); // Act var result = senderIdentities.CreateAsync(nickname, from, replyTo, address, address2, city, state, zip, country, CancellationToken.None).Result; // Assert result.ShouldNotBeNull(); result.Id.ShouldBe(1); }
/// <summary> /// Create a client that connects to the SendGrid Web API /// </summary> /// <param name="apiKey">Your SendGrid API Key</param> /// <param name="baseUri">Base SendGrid API Uri</param> public Client(string apiKey, string baseUri = "https://api.sendgrid.com", string apiVersion = "v3", HttpClient httpClient = null) { _baseUri = new Uri(string.Format("{0}/{1}", baseUri, apiVersion)); _apiKey = apiKey; Alerts = new Alerts(this); ApiKeys = new ApiKeys(this); Blocks = new Blocks(this); Campaigns = new Campaigns(this); Categories = new Categories(this); Contacts = new Contacts(this); CustomFields = new CustomFields(this); GlobalSuppressions = new GlobalSuppressions(this); Lists = new Lists(this); Mail = new Mail(this); Segments = new Segments(this); SenderIdentities = new SenderIdentities(this); Settings = new Settings(this); Statistics = new Statistics(this); Suppressions = new Suppressions(this); Templates = new Templates(this); UnsubscribeGroups = new UnsubscribeGroups(this); User = new User(this); Version = typeof(Client).GetTypeInfo().Assembly.GetName().Version.ToString(); _mustDisposeHttpClient = (httpClient == null); _httpClient = httpClient ?? new HttpClient(); _httpClient.BaseAddress = _baseUri; _httpClient.DefaultRequestHeaders.Accept.Clear(); _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(MEDIA_TYPE)); _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _apiKey); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", string.Format("StrongGrid/{0}", Version)); }
private Client(string apiKey, string username, string password, HttpClient httpClient, bool disposeClient, StrongGridClientOptions options) { _mustDisposeHttpClient = disposeClient; _httpClient = httpClient; _options = options ?? GetDefaultOptions(); _fluentClient = new FluentClient(new Uri(SENDGRID_V3_BASE_URI), httpClient) .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.LogLevelSuccessfulCalls, _options.LogLevelFailedCalls)); _fluentClient.Filters.Add(new SendGridErrorHandler()); if (!string.IsNullOrEmpty(apiKey)) { _fluentClient.SetBearerAuthentication(apiKey); } if (!string.IsNullOrEmpty(username)) { _fluentClient.SetBasicAuthentication(username, 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); Campaigns = new Campaigns(_fluentClient); Categories = new Categories(_fluentClient); Contacts = new Contacts(_fluentClient); CustomFields = new CustomFields(_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); Lists = new Lists(_fluentClient); Mail = new Mail(_fluentClient); Segments = new Segments(_fluentClient); SenderIdentities = new SenderIdentities(_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); }
private Client(string apiKey, string username, string password, string baseUri, string apiVersion, HttpClient httpClient) { _mustDisposeHttpClient = httpClient == null; _httpClient = httpClient; #if DEBUG Version = "DEBUG"; #else var assemblyVersion = typeof(Client).GetTypeInfo().Assembly.GetName().Version; Version = $"{assemblyVersion.Major}.{assemblyVersion.Minor}.{assemblyVersion.Build}"; #endif _fluentClient = new FluentClient(new Uri($"{baseUri.TrimEnd('/')}/{apiVersion.TrimStart('/')}"), httpClient) .SetUserAgent($"StrongGrid/{Version} (+https://github.com/Jericho/StrongGrid)") .SetRequestCoordinator(new SendGridRetryStrategy()); _fluentClient.Filters.Remove <DefaultErrorFilter>(); _fluentClient.Filters.Add(new DiagnosticHandler()); _fluentClient.Filters.Add(new SendGridErrorHandler()); if (!string.IsNullOrEmpty(apiKey)) { _fluentClient.SetBearerAuthentication(apiKey); } if (!string.IsNullOrEmpty(username)) { _fluentClient.SetBasicAuthentication(username, 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); Campaigns = new Campaigns(_fluentClient); Categories = new Categories(_fluentClient); Contacts = new Contacts(_fluentClient); CustomFields = new CustomFields(_fluentClient); GlobalSuppressions = new GlobalSuppressions(_fluentClient); InvalidEmails = new InvalidEmails(_fluentClient); IpAddresses = new IpAddresses(_fluentClient); IpPools = new IpPools(_fluentClient); Lists = new Lists(_fluentClient); Mail = new Mail(_fluentClient); Segments = new Segments(_fluentClient); SenderIdentities = new SenderIdentities(_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); Whitelabel = new Whitelabel(_fluentClient); }
public void Get() { // Arrange var identityId = 1; var mockRepository = new MockRepository(MockBehavior.Strict); var mockClient = mockRepository.Create <IClient>(); mockClient .Setup(c => c.GetAsync($"{ENDPOINT}/{identityId}", It.IsAny <CancellationToken>())) .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(SINGLE_SENDER_IDENTITY_JSON) }) .Verifiable(); var senderIdentities = new SenderIdentities(mockClient.Object, ENDPOINT); // Act var result = senderIdentities.GetAsync(identityId, CancellationToken.None).Result; // Assert result.ShouldNotBeNull(); result.Id.ShouldBe(identityId); }
private void Init() { Contacts = new Contacts(FluentClient); CustomFields = new CustomFields(FluentClient); Lists = new Lists(FluentClient); Segments = new Segments(FluentClient); SenderIdentities = new SenderIdentities(FluentClient); SingleSends = new SingleSends(FluentClient); }
public void Create() { // Arrange var nickname = "My Sender ID"; var from = new MailAddress("*****@*****.**", "Example INC"); var replyTo = new MailAddress("*****@*****.**", "Example INC"); var address = "123 Elm St."; var address2 = "Apt. 456"; var city = "Denver"; var state = "Colorado"; var zip = "80202"; var country = "United States"; var apiResponse = @"{ 'id': 1, 'nickname': 'My Sender ID', 'from': { 'email': '*****@*****.**', 'name': 'Example INC' }, 'reply_to': { 'email': '*****@*****.**', 'name': 'Example INC' }, 'address': '123 Elm St.', 'address_2': 'Apt. 456', 'city': 'Denver', 'state': 'Colorado', 'zip': '80202', 'country': 'United States', 'verified': { 'status': true, 'reason': '' }, 'updated_at': 1449872165, 'created_at': 1449872165, 'locked': false }" ; var mockClient = new Mock <IClient>(MockBehavior.Strict); mockClient.Setup(c => c.PostAsync(ENDPOINT, It.IsAny <JObject>(), It.IsAny <CancellationToken>())) .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(apiResponse) }); var senderIdentities = new SenderIdentities(mockClient.Object); // Act var result = senderIdentities.CreateAsync(nickname, from, replyTo, address, address2, city, state, zip, country, CancellationToken.None).Result; // Assert Assert.IsNotNull(result); Assert.AreEqual(1, result.Id); }
public void ResendVerification() { // Arrange var identityId = 1; var mockClient = new Mock <IClient>(MockBehavior.Strict); mockClient.Setup(c => c.PostAsync($"{ENDPOINT}/{identityId}/resend_verification", (JObject)null, It.IsAny <CancellationToken>())) .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.NoContent)); var senderIdentities = new SenderIdentities(mockClient.Object); // Act senderIdentities.ResendVerification(identityId, CancellationToken.None).Wait(CancellationToken.None); // Assert }
public void GetAll() { // Arrange var apiResponse = @"[ { 'id': 1, 'nickname': 'My Sender ID', 'from': { 'email': '*****@*****.**', 'name': 'Example INC' }, 'reply_to': { 'email': '*****@*****.**', 'name': 'Example INC' }, 'address': '123 Elm St.', 'address_2': 'Apt. 456', 'city': 'Denver', 'state': 'Colorado', 'zip': '80202', 'country': 'United States', 'verified': { 'status': true, 'reason': '' }, 'updated_at': 1449872165, 'created_at': 1449872165, 'locked': false } ]" ; var mockClient = new Mock <IClient>(MockBehavior.Strict); mockClient.Setup(c => c.GetAsync(ENDPOINT, It.IsAny <CancellationToken>())) .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(apiResponse) }); var senderIdentities = new SenderIdentities(mockClient.Object); // Act var result = senderIdentities.GetAllAsync(CancellationToken.None).Result; // Assert Assert.IsNotNull(result); Assert.AreEqual(1, result.Length); Assert.AreEqual(1, result[0].Id); }
private Client(string apiKey, string username, string password, string baseUri, string apiVersion, HttpClient httpClient, IRetryStrategy retryStrategy) { _baseUri = new Uri(string.Format("{0}/{1}", baseUri, apiVersion)); _retryStrategy = retryStrategy ?? new SendGridRetryStrategy(); Alerts = new Alerts(this); ApiKeys = new ApiKeys(this); Batches = new Batches(this); Blocks = new Blocks(this); Campaigns = new Campaigns(this); Categories = new Categories(this); Contacts = new Contacts(this); CustomFields = new CustomFields(this); GlobalSuppressions = new GlobalSuppressions(this); InvalidEmails = new InvalidEmails(this); Lists = new Lists(this); Mail = new Mail(this); Segments = new Segments(this); SenderIdentities = new SenderIdentities(this); Settings = new Settings(this); SpamReports = new SpamReports(this); Statistics = new Statistics(this); Suppressions = new Suppressions(this); Templates = new Templates(this); UnsubscribeGroups = new UnsubscribeGroups(this); User = new User(this); Version = typeof(Client).GetTypeInfo().Assembly.GetName().Version.ToString(); Whitelabel = new Whitelabel(this); _mustDisposeHttpClient = httpClient == null; _httpClient = httpClient ?? new HttpClient(); _httpClient.BaseAddress = _baseUri; _httpClient.DefaultRequestHeaders.Accept.Clear(); _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(MEDIA_TYPE)); if (!string.IsNullOrEmpty(apiKey)) { _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey); } if (!string.IsNullOrEmpty(username)) { _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Concat(username, ":", password)))); } _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", string.Format("StrongGrid/{0}", Version)); }
public void Update() { // Arrange var identityId = 1; var nickname = "New nickname"; var apiResponse = @"{ 'id': 1, 'nickname': 'New nickname', 'from': { 'email': '*****@*****.**', 'name': 'Example INC' }, 'reply_to': { 'email': '*****@*****.**', 'name': 'Example INC' }, 'address': '123 Elm St.', 'address_2': 'Apt. 456', 'city': 'Denver', 'state': 'Colorado', 'zip': '80202', 'country': 'United States', 'verified': { 'status': true, 'reason': '' }, 'updated_at': 1449872165, 'created_at': 1449872165, 'locked': false }" ; var mockClient = new Mock <IClient>(MockBehavior.Strict); mockClient.Setup(c => c.PatchAsync($"{ENDPOINT}/{identityId}", It.IsAny <JObject>(), It.IsAny <CancellationToken>())) .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(apiResponse) }); var senderIdentities = new SenderIdentities(mockClient.Object); // Act var result = senderIdentities.UpdateAsync(identityId, nickname, null, null, null, null, null, null, null, null, CancellationToken.None).Result; // Assert Assert.IsNotNull(result); }
public void Delete() { // Arrange var identityId = 1; var mockHttp = new MockHttpMessageHandler(); mockHttp.Expect(HttpMethod.Delete, Utils.GetSendGridApiUri(ENDPOINT, identityId)).Respond(HttpStatusCode.NoContent); var client = Utils.GetFluentClient(mockHttp); var senderIdentities = new SenderIdentities(client); // Act senderIdentities.DeleteAsync(identityId, CancellationToken.None).Wait(CancellationToken.None); // Assert mockHttp.VerifyNoOutstandingExpectation(); mockHttp.VerifyNoOutstandingRequest(); }
public async Task ResendVerificationAsync() { // Arrange var identityId = 1; var mockHttp = new MockHttpMessageHandler(); mockHttp.Expect(HttpMethod.Post, Utils.GetSendGridApiUri(ENDPOINT, identityId, "resend_verification")).Respond(HttpStatusCode.NoContent); var client = Utils.GetFluentClient(mockHttp); var senderIdentities = new SenderIdentities(client); // Act await senderIdentities.ResendVerification(identityId, null, CancellationToken.None).ConfigureAwait(false); // Assert mockHttp.VerifyNoOutstandingExpectation(); mockHttp.VerifyNoOutstandingRequest(); }
public void Delete() { // Arrange var identityId = 1; var mockRepository = new MockRepository(MockBehavior.Strict); var mockClient = mockRepository.Create <IClient>(); mockClient .Setup(c => c.DeleteAsync($"{ENDPOINT}/{identityId}", It.IsAny <CancellationToken>())) .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.NoContent)) .Verifiable(); var senderIdentities = new SenderIdentities(mockClient.Object, ENDPOINT); // Act senderIdentities.DeleteAsync(identityId, CancellationToken.None).Wait(CancellationToken.None); // Assert }
public async Task GetAllAsync() { // Arrange var mockHttp = new MockHttpMessageHandler(); mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT)).Respond("application/json", MULTIPLE_SENDER_IDENTITIES_JSON); var client = Utils.GetFluentClient(mockHttp); var senderIdentities = new SenderIdentities(client); // Act var result = await senderIdentities.GetAllAsync(null, CancellationToken.None).ConfigureAwait(false); // Assert mockHttp.VerifyNoOutstandingExpectation(); mockHttp.VerifyNoOutstandingRequest(); result.ShouldNotBeNull(); result.Length.ShouldBe(1); result[0].Id.ShouldBe(1); }
public async Task UpdateAsync() { // Arrange var identityId = 1; var nickname = "New nickname"; var mockHttp = new MockHttpMessageHandler(); mockHttp.Expect(new HttpMethod("PATCH"), Utils.GetSendGridApiUri(ENDPOINT, identityId)).Respond("application/json", SINGLE_SENDER_IDENTITY_JSON); var client = Utils.GetFluentClient(mockHttp); var senderIdentities = new SenderIdentities(client); // Act var result = await senderIdentities.UpdateAsync(identityId, nickname, null, null, null, null, null, null, null, null, null, CancellationToken.None).ConfigureAwait(false); // Assert mockHttp.VerifyNoOutstandingExpectation(); mockHttp.VerifyNoOutstandingRequest(); result.ShouldNotBeNull(); }
public void Get() { // Arrange var identityId = 1; var mockHttp = new MockHttpMessageHandler(); mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT, identityId)).Respond("application/json", SINGLE_SENDER_IDENTITY_JSON); var client = Utils.GetFluentClient(mockHttp); var senderIdentities = new SenderIdentities(client); // Act var result = senderIdentities.GetAsync(identityId, CancellationToken.None).Result; // Assert mockHttp.VerifyNoOutstandingExpectation(); mockHttp.VerifyNoOutstandingRequest(); result.ShouldNotBeNull(); result.Id.ShouldBe(identityId); }