diff --git a/src/dnsimple-test/FixtureLoader.cs b/src/dnsimple-test/FixtureLoader.cs index b891acc..3037d21 100644 --- a/src/dnsimple-test/FixtureLoader.cs +++ b/src/dnsimple-test/FixtureLoader.cs @@ -4,7 +4,6 @@ using System.Diagnostics; using System.IO; using System.Linq; -using Castle.Core.Internal; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using RestSharp; @@ -65,9 +64,9 @@ private string LoadFixture() return File.ReadAllText(path); } - public List ExtractHeaders() + public List ExtractHeaders() { - var headers = new List(); + var headers = new List(); foreach (var line in GetLines()) { @@ -76,7 +75,7 @@ public List ExtractHeaders() if (line.Contains(':')) { var header = line.Split(':'); - headers.Add(new Parameter(header[0], header[1], ParameterType.HttpHeader)); + headers.Add(new HeaderParameter(header[0], header[1])); } } diff --git a/src/dnsimple-test/MockDnsimpleClient.cs b/src/dnsimple-test/MockDnsimpleClient.cs index a047f63..54bc002 100644 --- a/src/dnsimple-test/MockDnsimpleClient.cs +++ b/src/dnsimple-test/MockDnsimpleClient.cs @@ -1,11 +1,11 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Net; using dnsimple; using dnsimple.Services; using Newtonsoft.Json.Linq; using RestSharp; -using RestSharp.Extensions; using ICredentials = dnsimple.ICredentials; namespace dnsimple_test @@ -82,7 +82,7 @@ public void SetUserAgent(string customUserAgent) public string RequestSentTo() { - return ((MockHttpService)Http).RequestUrlSent.UrlDecode(); + return Uri.UnescapeDataString(((MockHttpService)Http).RequestUrlSent); } public Method HttpMethodUsed() @@ -118,14 +118,16 @@ public override RequestBuilder RequestBuilder(string path) return new RequestBuilder(path); } - public override IRestResponse Execute(IRestRequest request) + public override RestResponse Execute(RestRequest request) { - RequestUrlSent = new RestClient($"{_baseUrl}/v2/").BuildUri(request).ToString(); + using var client = new RestClient(new Uri($"{_baseUrl}/v2/")); + RequestUrlSent = client.BuildUri(request).ToString(); MethodSent = request.Method; try { - PayloadSent = (string)request.Parameters.Find(x => - x.ContentType.Equals("application/json")).Value; + var bodyParameter = request.Parameters + .FirstOrDefault(p => p.Type == ParameterType.RequestBody); + PayloadSent = bodyParameter?.Value?.ToString(); } catch (Exception) { @@ -156,14 +158,16 @@ public override IRestResponse Execute(IRestRequest request) public class MockResponse : RestResponse { - public MockResponse(FixtureLoader loader) + public MockResponse(FixtureLoader loader) : base(new RestRequest()) { StatusCode = loader.ExtractStatusCode(); Content = loader.ExtractJsonPayload(); Headers = loader.ExtractHeaders(); + IsSuccessStatusCode = (int)StatusCode >= 200 && (int)StatusCode < 300; + ResponseStatus = ResponseStatus.Completed; } - public void SetHeaders(List headers) + public void SetHeaders(List headers) { Headers = headers; } diff --git a/src/dnsimple-test/Services/CertificatesTest.cs b/src/dnsimple-test/Services/CertificatesTest.cs index 2187de8..78dd260 100644 --- a/src/dnsimple-test/Services/CertificatesTest.cs +++ b/src/dnsimple-test/Services/CertificatesTest.cs @@ -346,7 +346,7 @@ public void PurchaseLetsEncryptCertificate(long accountId, Assert.That(certificateOrdered.CreatedAt, Is.EqualTo(Convert.ToDateTime("2020-06-18T18:54:17Z"))); Assert.That(certificateOrdered.UpdatedAt, Is.EqualTo(Convert.ToDateTime("2020-06-18T18:54:17Z"))); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } @@ -375,7 +375,7 @@ public void IssueLetsEncryptCertificate(long accountId, Assert.That(certificate.AlternateNames, Is.Empty); Assert.That(certificate.AuthorityIdentifier, Is.EqualTo("letsencrypt")); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } @@ -407,7 +407,7 @@ public void PurchaseLetsEncryptCertificateRenewal(long accountId, Assert.That(renewalPurchased.CreatedAt, Is.EqualTo(Convert.ToDateTime("2020-06-18T19:56:20Z"))); Assert.That(renewalPurchased.UpdatedAt, Is.EqualTo(Convert.ToDateTime("2020-06-18T19:56:20Z"))); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } @@ -440,7 +440,7 @@ public void IssueLetsEncryptCertificateRenewal(long accountId, Assert.That(renewalIssued.AlternateNames, Is.Empty); Assert.That(renewalIssued.AuthorityIdentifier, Is.EqualTo("letsencrypt")); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } diff --git a/src/dnsimple-test/Services/ContactsTest.cs b/src/dnsimple-test/Services/ContactsTest.cs index b3cc9ef..89238b8 100644 --- a/src/dnsimple-test/Services/ContactsTest.cs +++ b/src/dnsimple-test/Services/ContactsTest.cs @@ -142,7 +142,7 @@ public void CreateContact(long accountId, string expectedUrl) Assert.That(created.Email, Is.EqualTo(contact.Email)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post)); }); } @@ -215,7 +215,7 @@ public void UpdateContact(long accountId, long contactId, string expectedUrl) Assert.That(updated.AccountId, Is.EqualTo(accountId)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.PATCH)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Patch)); }); } @@ -233,7 +233,7 @@ public void DeleteContact(long accountId, long contactId, string expectedUrl) }); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete)); }); } diff --git a/src/dnsimple-test/Services/HttpTest.cs b/src/dnsimple-test/Services/HttpTest.cs index b09f17c..c31e7c1 100644 --- a/src/dnsimple-test/Services/HttpTest.cs +++ b/src/dnsimple-test/Services/HttpTest.cs @@ -5,7 +5,6 @@ using dnsimple; using dnsimple.Services; using dnsimple.Services.ListOptions; -using Moq; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using NUnit.Framework; @@ -19,31 +18,23 @@ public class HttpTest [Test] public void ReturnsARequestBuilder() { - var http = new HttpService(new RestClient(), new RequestBuilder()); + var http = new HttpService(new RestClientWrapper(), new RequestBuilder()); Assert.That(http.RequestBuilder(""), Is.InstanceOf()); - } [Test] public void InvalidRequest() { - var client = new Mock(); - var response = new Mock(); - var request = new Mock(); - var http = new HttpService(client.Object, new RequestBuilder()); - - response.SetupProperty(mock => mock.StatusCode, - HttpStatusCode.Unauthorized); - response.Setup(mock => mock.IsSuccessful).Returns(false); - response.Setup(mock => mock.Content) - .Returns("{\"message\": \"Authentication failed\"}"); - - client.Setup(mock => mock.Execute(request.Object)) - .Returns(response.Object); + var response = new RestResponse(new RestRequest()) + { + StatusCode = HttpStatusCode.Unauthorized, + Content = "{\"message\": \"Authentication failed\"}", + ResponseStatus = ResponseStatus.Completed, + }; Assert.Throws(Is.TypeOf() .And.Message.EqualTo("Authentication failed"), - delegate { http.Execute(request.Object); }); + delegate { HttpService.HandleExceptions(response); }); } [Test] @@ -144,7 +135,7 @@ public void BuildsRequestWithPath() [Test] public void Resets() { - _builder.Method(Method.HEAD); + _builder.Method(Method.Head); Assert.That(_builder.Reset().Request, Is.Null); } @@ -152,10 +143,10 @@ public void Resets() [Test] public void SetsTheMethod() { - _builder.Method(Method.POST); + _builder.Method(Method.Post); var request = _builder.Request; - Assert.That(request.Method, Is.EqualTo(Method.POST)); + Assert.That(request.Method, Is.EqualTo(Method.Post)); } [Test] diff --git a/src/dnsimple-test/Services/OAuth2Test.cs b/src/dnsimple-test/Services/OAuth2Test.cs index f343b1e..408f408 100644 --- a/src/dnsimple-test/Services/OAuth2Test.cs +++ b/src/dnsimple-test/Services/OAuth2Test.cs @@ -15,8 +15,10 @@ public class OAuth2Test private static void SetupMockHttpService(Mock http, IMock mockRestResponse) { - var response = new RestResponse(); - response.Content = mockRestResponse.Object.Content; + var response = new RestResponse(new RestRequest()) + { + Content = mockRestResponse.Object.Content, + }; http.Setup(mock => mock.RequestBuilder(It.IsAny())) .Returns(new RequestBuilder("")); @@ -31,8 +33,8 @@ private static void SetupMockHttpService(Mock http, [Test] public void ExchangeAuthorizationForToken() { - var http = new Mock(new RestClient(), new RequestBuilder()); - var mockRestResponse = new Mock(); + var http = new Mock(new RestClientWrapper(), new RequestBuilder()); + var mockRestResponse = new Mock(new RestRequest()); var oauthService = new OAuth2Service(http.Object); mockRestResponse.Object.Content = diff --git a/src/dnsimple-test/Services/RegistrarAutoRenewalTest.cs b/src/dnsimple-test/Services/RegistrarAutoRenewalTest.cs index 4e74136..13be648 100644 --- a/src/dnsimple-test/Services/RegistrarAutoRenewalTest.cs +++ b/src/dnsimple-test/Services/RegistrarAutoRenewalTest.cs @@ -27,7 +27,7 @@ public void EnableDomainAutoRenewal(long accountId, string domain, string expect client.Registrar.EnableDomainAutoRenewal(accountId, domain); }); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.PUT)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Put)); }); } @@ -45,7 +45,7 @@ public void DisableDomainAutoRenewal(long accountId, string domain, string expec client.Registrar.DisableDomainAutoRenewal(accountId, domain); }); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete)); }); } } diff --git a/src/dnsimple-test/Services/RegistrarDelegationTest.cs b/src/dnsimple-test/Services/RegistrarDelegationTest.cs index e5754fd..d54a093 100644 --- a/src/dnsimple-test/Services/RegistrarDelegationTest.cs +++ b/src/dnsimple-test/Services/RegistrarDelegationTest.cs @@ -99,7 +99,7 @@ public void ChangeDomainDelegation(long accountId, string domain, Assert.That(newDelegation.Data, Is.EqualTo(delegation)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.PUT)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Put)); }); } @@ -130,7 +130,7 @@ public void ChangeDomainDelegationToVanity(long accountId, string domain, string Assert.That(vanityDelegation.First().UpdatedAt, Is.EqualTo(UpdatedAt)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.PUT)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Put)); }); } @@ -150,7 +150,7 @@ public void ChangeDomainDelegationFromVanity(long accountId, string domain, stri }); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete)); }); } } diff --git a/src/dnsimple-test/Services/RegistrarTransferLockTest.cs b/src/dnsimple-test/Services/RegistrarTransferLockTest.cs index 59cdada..b0ae8cd 100644 --- a/src/dnsimple-test/Services/RegistrarTransferLockTest.cs +++ b/src/dnsimple-test/Services/RegistrarTransferLockTest.cs @@ -29,7 +29,7 @@ public void EnableDomainTransferLock(long accountId, string domain, string expec Assert.That(transferLockStatus.Enabled, Is.True); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post)); }); } @@ -46,7 +46,7 @@ public void DisableDomainTransferLock(long accountId, string domain, string expe Assert.That(transferLockStatus.Enabled, Is.False); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete)); }); } @@ -62,7 +62,7 @@ public void GetDomainTransferLock(long accountId, string domain, string expected Assert.That(transferLockStatus.Enabled, Is.True); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.GET)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Get)); }); } } diff --git a/src/dnsimple-test/Services/RegistrarWhoisPrivacyTest.cs b/src/dnsimple-test/Services/RegistrarWhoisPrivacyTest.cs index 9a8d67d..61b09c2 100644 --- a/src/dnsimple-test/Services/RegistrarWhoisPrivacyTest.cs +++ b/src/dnsimple-test/Services/RegistrarWhoisPrivacyTest.cs @@ -32,7 +32,7 @@ public void EnableWhoisPrivacy(long accountId, string domain, string expectedUrl Assert.That(privacy.Enabled, Is.True); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.PUT)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Put)); }); } @@ -51,7 +51,7 @@ public void PurchaseAndEnableWhoisPrivacy(long accountId, string domain, string Assert.That(privacy.Enabled, Is.Null); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.PUT)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Put)); }); } @@ -70,7 +70,7 @@ public void DisableWhoisPrivacy(long accountId, string domain, string expectedUr Assert.That(privacy.Enabled, Is.False); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete)); }); } } diff --git a/src/dnsimple-test/Services/ServicesDomainsTest.cs b/src/dnsimple-test/Services/ServicesDomainsTest.cs index 229b226..ae1c2ed 100644 --- a/src/dnsimple-test/Services/ServicesDomainsTest.cs +++ b/src/dnsimple-test/Services/ServicesDomainsTest.cs @@ -45,7 +45,7 @@ public void ApplyService(long accountId, string domain, string service, Assert.Multiple(() => { - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } @@ -62,7 +62,7 @@ public void UnapplyService(long accountId, string domain, string service, string Assert.Multiple(() => { - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } diff --git a/src/dnsimple-test/Services/TemplateDomainsTest.cs b/src/dnsimple-test/Services/TemplateDomainsTest.cs index d50cdc6..5bcdc79 100644 --- a/src/dnsimple-test/Services/TemplateDomainsTest.cs +++ b/src/dnsimple-test/Services/TemplateDomainsTest.cs @@ -19,7 +19,7 @@ public void ApplyTemplate(long accountId, string domain, string template, string Assert.Multiple(() => { - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } diff --git a/src/dnsimple-test/Services/TemplateRecordsTest.cs b/src/dnsimple-test/Services/TemplateRecordsTest.cs index 7660f88..55e3250 100644 --- a/src/dnsimple-test/Services/TemplateRecordsTest.cs +++ b/src/dnsimple-test/Services/TemplateRecordsTest.cs @@ -132,7 +132,7 @@ public void CreateTemplateRecord(long accountId, string template, string expecte Assert.That(record.Ttl, Is.EqualTo(templateRecord.Ttl)); Assert.That(record.Priority, Is.EqualTo(templateRecord.Priority)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } @@ -168,7 +168,7 @@ public void DeleteTemplateRecord(long accountId, string template, long recordId, Assert.Multiple(() => { - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } diff --git a/src/dnsimple-test/Services/TemplatesTest.cs b/src/dnsimple-test/Services/TemplatesTest.cs index bf0618e..4d19d12 100644 --- a/src/dnsimple-test/Services/TemplatesTest.cs +++ b/src/dnsimple-test/Services/TemplatesTest.cs @@ -122,7 +122,7 @@ public void CreateTemplate(long accountId, string expectedUrl) Assert.That(template.Sid, Is.EqualTo(templateData.Sid)); Assert.That(template.Description, Is.EqualTo(templateData.Description)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } @@ -162,7 +162,7 @@ public void UpdateTemplate(long accountId, string template, string expectedUrl) Assert.Multiple(() => { - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.PATCH)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Patch)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } @@ -178,7 +178,7 @@ public void DeleteTemplate(long accountId, string template, string expectedUrl) Assert.Multiple(() => { - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } diff --git a/src/dnsimple-test/Services/VanityNameServersTest.cs b/src/dnsimple-test/Services/VanityNameServersTest.cs index e2c6b2a..3f80174 100644 --- a/src/dnsimple-test/Services/VanityNameServersTest.cs +++ b/src/dnsimple-test/Services/VanityNameServersTest.cs @@ -66,7 +66,7 @@ public void EnableVanityNameServers(long accountId, string domain, { Assert.That(vanityNameServers.Count, Is.EqualTo(4)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.PUT)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Put)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } @@ -82,7 +82,7 @@ public void DisableVanityNameServers(long accountId, string domain, string expec Assert.Multiple(() => { - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } diff --git a/src/dnsimple-test/Services/WebhooksTest.cs b/src/dnsimple-test/Services/WebhooksTest.cs index b0c41f9..8085635 100644 --- a/src/dnsimple-test/Services/WebhooksTest.cs +++ b/src/dnsimple-test/Services/WebhooksTest.cs @@ -88,7 +88,7 @@ public void CreateWebhook(long accountId, string expectedUrl) { Assert.That(created.Url, Is.EqualTo(hook.Url)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } @@ -118,7 +118,7 @@ public void DeleteWebhook(long accountId, long webhookId, string expectedUrl) Assert.Multiple(() => { - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } diff --git a/src/dnsimple-test/Services/ZoneRecordsTest.cs b/src/dnsimple-test/Services/ZoneRecordsTest.cs index 5e508a2..273ceee 100644 --- a/src/dnsimple-test/Services/ZoneRecordsTest.cs +++ b/src/dnsimple-test/Services/ZoneRecordsTest.cs @@ -167,7 +167,7 @@ public void CreateZoneRecord(string fixture, long accountId, Assert.That(created.Type, Is.EqualTo("A")); Assert.That(created.Regions, Contains.Item("global")); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } @@ -196,7 +196,7 @@ public void GetZoneRecord(long accountId, string zoneId, long recordId, Assert.That(record.Regions, Contains.Item("IAD")); Assert.That(record.SystemRecord, Is.False); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.GET)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Get)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } @@ -234,7 +234,7 @@ public void UpdateZoneRecord(long accountId, string zoneId, Assert.That(record.SystemRecord, Is.False); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.PATCH)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Patch)); }); } @@ -253,7 +253,7 @@ public void DeleteRecord(long accountId, string zoneId, long recordId, client.Zones.DeleteZoneRecord(accountId, zoneId, recordId); }); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } diff --git a/src/dnsimple-test/Services/ZonesTest.cs b/src/dnsimple-test/Services/ZonesTest.cs index eec996d..ab4b557 100644 --- a/src/dnsimple-test/Services/ZonesTest.cs +++ b/src/dnsimple-test/Services/ZonesTest.cs @@ -269,7 +269,7 @@ public void ActivateDns(long accountId, string zoneName) Assert.That(zone.CreatedAt, Is.EqualTo(Convert.ToDateTime("2022-09-28T04:45:24Z"))); Assert.That(zone.UpdatedAt, Is.EqualTo(Convert.ToDateTime("2023-07-06T11:19:48Z"))); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.PUT)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Put)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } @@ -291,7 +291,7 @@ public void DeactivateDns(long accountId, string zoneName) Assert.That(zone.CreatedAt, Is.EqualTo(Convert.ToDateTime("2022-09-28T04:45:24Z"))); Assert.That(zone.UpdatedAt, Is.EqualTo(Convert.ToDateTime("2023-08-08T04:19:52Z"))); - Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE)); + Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete)); Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl)); }); } diff --git a/src/dnsimple-test/dnsimple-test.csproj b/src/dnsimple-test/dnsimple-test.csproj index fe3f705..e255045 100644 --- a/src/dnsimple-test/dnsimple-test.csproj +++ b/src/dnsimple-test/dnsimple-test.csproj @@ -569,7 +569,7 @@ - + diff --git a/src/dnsimple/Credentials.cs b/src/dnsimple/Credentials.cs index a26ddea..c5b59bd 100644 --- a/src/dnsimple/Credentials.cs +++ b/src/dnsimple/Credentials.cs @@ -1,3 +1,4 @@ +using System.Text; using dnsimple.Services; using RestSharp.Authenticators; @@ -12,7 +13,7 @@ namespace dnsimple /// public interface ICredentials { - AuthenticatorBase Authenticator { get; } + IAuthenticator Authenticator { get; } } /// @@ -23,10 +24,10 @@ public interface ICredentials /// public readonly struct BasicHttpCredentials : ICredentials { - public AuthenticatorBase Authenticator { get; } + public IAuthenticator Authenticator { get; } public BasicHttpCredentials(string name, string password) => - Authenticator = new HttpBasicAuthenticator(name, password); + Authenticator = new HttpBasicAuthenticator(name, password, Encoding.UTF8); } /// @@ -38,9 +39,9 @@ public BasicHttpCredentials(string name, string password) => /// public readonly struct OAuth2Credentials : ICredentials { - public AuthenticatorBase Authenticator { get; } + public IAuthenticator Authenticator { get; } public OAuth2Credentials(string token) => - Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(token, "Bearer"); + Authenticator = new RestSharp.Authenticators.OAuth2.OAuth2AuthorizationRequestHeaderAuthenticator(token, "Bearer"); } -} \ No newline at end of file +} diff --git a/src/dnsimple/DNSimple.cs b/src/dnsimple/DNSimple.cs index d01b103..772eea0 100644 --- a/src/dnsimple/DNSimple.cs +++ b/src/dnsimple/DNSimple.cs @@ -342,7 +342,7 @@ public Client(RestClientWrapper restClientWrapper) public void ChangeBaseUrlTo(string baseUrl) { BaseUrl = baseUrl; - RestClientWrapper.RestClient.BaseUrl = new Uri(VersionedBaseUrl()); + RestClientWrapper.SetBaseUrl(new Uri(VersionedBaseUrl())); } /// @@ -374,7 +374,7 @@ public string VersionedBaseUrl() public void SetUserAgent(string customUserAgent) { UserAgent = $"{customUserAgent} {DefaultUserAgent}"; - RestClientWrapper.RestClient.UserAgent = UserAgent; + RestClientWrapper.SetUserAgent(UserAgent); } /// @@ -385,8 +385,7 @@ public void SetUserAgent(string customUserAgent) /// private void InitializeRestClient() { - RestClientWrapper.RestClient.BaseUrl = new Uri(VersionedBaseUrl()); - RestClientWrapper.RestClient.UseJson(); + RestClientWrapper.SetBaseUrl(new Uri(VersionedBaseUrl())); } /// @@ -398,7 +397,7 @@ private void InitializeRestClient() /// private void InitializeServices() { - Http = new HttpService(RestClientWrapper.RestClient, new RequestBuilder()); + Http = new HttpService(RestClientWrapper, new RequestBuilder()); Accounts = new AccountsService(this); Certificates = new CertificatesService(this); Contacts = new ContactsService(this); diff --git a/src/dnsimple/RequestBuilder.cs b/src/dnsimple/RequestBuilder.cs index d63c2e2..269f420 100644 --- a/src/dnsimple/RequestBuilder.cs +++ b/src/dnsimple/RequestBuilder.cs @@ -12,7 +12,7 @@ namespace dnsimple public class RequestBuilder { /// - /// Represents the RestRequest we will be issuing. + /// Represents the RestRequest we will be issuing. /// public RestRequest Request { get; private set; } @@ -32,14 +32,14 @@ public RequestBuilder() /// var builder = new RequestBuilder("/whoami"); /// /// - public RequestBuilder(string path) => + public RequestBuilder(string path) => AddPath(path); - + /// /// Adds headers to the request. /// /// The headers we want to add to the request. - public void AddHeaders(Collection> headers) => + public void AddHeaders(Collection> headers) => Request.AddHeaders(headers); /// @@ -54,7 +54,7 @@ public void AddParameters( AddParameter(parameter); } } - + /// /// Adds a parameter to the request. /// @@ -66,28 +66,28 @@ public void AddParameter(KeyValuePair parameter) Request.AddParameter(parameter.Key, parameter.Value); } } - + /// /// Adds a JSON payload to the body of the request. /// /// The object to be serialized and send in the /// body of the request. public void AddJsonPayload(object payload) - => Request.AddJsonBody(JsonConvert.SerializeObject(payload)); - + => Request.AddStringBody(JsonConvert.SerializeObject(payload), DataFormat.Json); + /// /// Adds a JSON payload to the body of the request. /// /// The object to be send in the body of the request. public void AddJsonPayloadRaw(object payload) - => Request.AddJsonBody(payload); + => Request.AddJsonBody(payload); /// /// Sets the HTTP method to be used. /// /// /// - public void Method(Method method) => + public void Method(Method method) => Request.Method = method; /// @@ -101,8 +101,10 @@ public void Method(Method method) => /// builder.AddPath("/whoami"); /// /// - public void AddPath(string path) => - Request = new RestRequest(path, DataFormat.Json); + public void AddPath(string path) + { + Request = new RestRequest(path) { RequestFormat = DataFormat.Json }; + } /// /// Resets the RequestBuilder emptying the Request contained. diff --git a/src/dnsimple/RestClientWrapper.cs b/src/dnsimple/RestClientWrapper.cs index 5e07f35..f2fea8e 100644 --- a/src/dnsimple/RestClientWrapper.cs +++ b/src/dnsimple/RestClientWrapper.cs @@ -1,4 +1,6 @@ +using System; using RestSharp; +using RestSharp.Authenticators; namespace dnsimple { @@ -8,21 +10,26 @@ namespace dnsimple /// public class RestClientWrapper { + private RestClient _restClient; + private IAuthenticator _authenticator; + private Uri _baseUrl; + private string _userAgent; + /// /// The instance of the RestSharp.RestClient. /// /// - public RestClient RestClient { get; } + public virtual RestClient RestClient => _restClient ??= Build(); /// /// Constructs a new RestClientWrapper /// - public RestClientWrapper() : this(new RestClient()) + public RestClientWrapper() { } /// - /// Adds an Authenticator to the RestSharp.RestClient. + /// Adds an Authenticator to the underlying RestClient. /// /// /// Authenticators are used to interact with the DNSimple API. They can @@ -31,10 +38,33 @@ public RestClientWrapper() : this(new RestClient()) /// The credentials containing the authenticator to be used /// /// - public virtual void AddAuthenticator(ICredentials credentials) => - RestClient.Authenticator = credentials.Authenticator; + public virtual void AddAuthenticator(ICredentials credentials) + { + _authenticator = credentials.Authenticator; + _restClient = null; + } + + internal void SetBaseUrl(Uri baseUrl) + { + _baseUrl = baseUrl; + _restClient = null; + } - private RestClientWrapper(RestClient restClient) => - RestClient = restClient; + internal void SetUserAgent(string userAgent) + { + _userAgent = userAgent; + _restClient = null; + } + + private RestClient Build() + { + var options = new RestClientOptions + { + Authenticator = _authenticator, + UserAgent = _userAgent, + }; + if (_baseUrl != null) options.BaseUrl = _baseUrl; + return new RestClient(options); + } } -} \ No newline at end of file +} diff --git a/src/dnsimple/Services/Certificates.cs b/src/dnsimple/Services/Certificates.cs index d0b53d5..9eccb66 100644 --- a/src/dnsimple/Services/Certificates.cs +++ b/src/dnsimple/Services/Certificates.cs @@ -92,7 +92,7 @@ public SimpleResponse GetCertificatePrivateKey(long accountId public SimpleResponse PurchaseLetsEncryptCertificate(long accountId, string domainIdentifier, LetsencryptCertificateAttributes attributes) { var builder = BuildRequestForPath(PurchaseLetsEncryptCertificatePath(accountId, domainIdentifier)); - builder.Method(Method.POST); + builder.Method(Method.Post); builder.AddJsonPayload(attributes); return new SimpleResponse(Execute(builder.Request)); @@ -110,7 +110,7 @@ public SimpleResponse PurchaseLetsEncryptCertificate(long a public SimpleResponse IssueLetsEncryptCertificate(long accountId, string domainIdentifier, long certificateId) { var builder = BuildRequestForPath(IssueLetsEncryptCertificatePath(accountId, domainIdentifier, certificateId)); - builder.Method(Method.POST); + builder.Method(Method.Post); return new SimpleResponse(Execute(builder.Request)); } @@ -128,7 +128,7 @@ public SimpleResponse IssueLetsEncryptCertificate(long accountId, s public SimpleResponse PurchaseLetsEncryptCertificateRenewal(long accountId, string domainIdentifier, long certificateId, LetsencryptCertificateAttributes attributes) { var builder = BuildRequestForPath(LetsEncryptCertificateRenewalPath(accountId, domainIdentifier, certificateId)); - builder.Method(Method.POST); + builder.Method(Method.Post); builder.AddJsonPayload(attributes); return new SimpleResponse(Execute(builder.Request)); @@ -145,7 +145,7 @@ public SimpleResponse PurchaseLetsEncryptCertificateRenewal( public SimpleResponse IssueLetsEncryptCertificateRenewal(long accountId, string domainIdentifier, long certificateId, long certificateRenewalId) { var builder = BuildRequestForPath(IssueLetsEncryptCertificateRenewalPath(accountId, domainIdentifier, certificateId, certificateRenewalId)); - builder.Method(Method.POST); + builder.Method(Method.Post); return new SimpleResponse(Execute(builder.Request)); } diff --git a/src/dnsimple/Services/Contacts.cs b/src/dnsimple/Services/Contacts.cs index cecb23e..ea93fcc 100644 --- a/src/dnsimple/Services/Contacts.cs +++ b/src/dnsimple/Services/Contacts.cs @@ -49,7 +49,7 @@ public PaginatedResponse ListContacts(long accountId, public SimpleResponse CreateContact(long accountId, Contact contact) { var builder = BuildRequestForPath(ContactsPath(accountId)); - builder.Method(Method.POST); + builder.Method(Method.Post); builder.AddJsonPayload(contact); return new SimpleResponse(Execute(builder.Request)); @@ -82,7 +82,7 @@ public SimpleResponse GetContact(long accountId, long contactId) public SimpleResponse UpdateContact(long accountId, long contactId, Contact contact) { var builder = BuildRequestForPath(ContactPath(accountId, contactId)); - builder.Method(Method.PATCH); + builder.Method(Method.Patch); builder.AddJsonPayload(new UpdateContact(contact)); return new SimpleResponse(Execute(builder.Request)); @@ -97,7 +97,7 @@ public SimpleResponse UpdateContact(long accountId, long contactId, Con public EmptyResponse DeleteContact(long accountId, long contactId) { var builder = BuildRequestForPath(ContactPath(accountId, contactId)); - builder.Method(Method.DELETE); + builder.Method(Method.Delete); return new EmptyResponse(Execute(builder.Request)); } diff --git a/src/dnsimple/Services/Domains.cs b/src/dnsimple/Services/Domains.cs index 1eb8c38..024c732 100644 --- a/src/dnsimple/Services/Domains.cs +++ b/src/dnsimple/Services/Domains.cs @@ -58,7 +58,7 @@ public PaginatedResponse ListDomains(long accountId, ListOptionsWithFilt public SimpleResponse CreateDomain(long accountId, Domain domain) { var builder = BuildRequestForPath(DomainsPath(accountId)); - builder.Method(Method.POST); + builder.Method(Method.Post); builder.AddJsonPayload(domain); return new SimpleResponse(Execute(builder.Request)); @@ -74,7 +74,7 @@ public SimpleResponse CreateDomain(long accountId, Domain domain) public EmptyResponse DeleteDomain(long accountId, string domainIdentifier) { var builder = BuildRequestForPath(DeleteDomainPath(accountId, domainIdentifier)); - builder.Method(Method.DELETE); + builder.Method(Method.Delete); return new EmptyResponse(Execute(builder.Request)); } diff --git a/src/dnsimple/Services/DomainsDelegationSignerRecords.cs b/src/dnsimple/Services/DomainsDelegationSignerRecords.cs index 98feb84..ab130ac 100644 --- a/src/dnsimple/Services/DomainsDelegationSignerRecords.cs +++ b/src/dnsimple/Services/DomainsDelegationSignerRecords.cs @@ -39,7 +39,7 @@ public PaginatedResponse ListDelegationSignerRecords(lon public SimpleResponse CreateDelegationSignerRecord(long accountId, string domainIdentifier, DelegationSignerRecord record) { var builder = BuildRequestForPath(DsRecordsPath(accountId, domainIdentifier)); - builder.Method(Method.POST); + builder.Method(Method.Post); builder.AddJsonPayload(record); if (record.Algorithm.Trim().Equals("")) @@ -73,7 +73,7 @@ public SimpleResponse GetDelegationSignerRecord(long acc public EmptyResponse DeleteDelegationSignerRecord(long accountId, string domainIdentifier, int recordId) { var builder = BuildRequestForPath(DsRecordPath(accountId, domainIdentifier, recordId)); - builder.Method(Method.DELETE); + builder.Method(Method.Delete); return new EmptyResponse(Execute(builder.Request)); } diff --git a/src/dnsimple/Services/DomainsDnssec.cs b/src/dnsimple/Services/DomainsDnssec.cs index d544d33..d9fa507 100644 --- a/src/dnsimple/Services/DomainsDnssec.cs +++ b/src/dnsimple/Services/DomainsDnssec.cs @@ -38,7 +38,7 @@ public SimpleResponse GetDnssec(long accountId, string domainIdent public SimpleResponse EnableDnssec(long accountId, string domainIdentifier) { var builder = BuildRequestForPath(DnssecPath(accountId, domainIdentifier)); - builder.Method(Method.POST); + builder.Method(Method.Post); return new SimpleResponse(Execute(builder.Request)); } @@ -58,7 +58,7 @@ public SimpleResponse EnableDnssec(long accountId, string domainId public EmptyResponse DisableDnssec(long accountId, string domainIdentifier) { var builder = BuildRequestForPath(DnssecPath(accountId,domainIdentifier)); - builder.Method(Method.DELETE); + builder.Method(Method.Delete); return new EmptyResponse(Execute(builder.Request)); } diff --git a/src/dnsimple/Services/DomainsEmailForwards.cs b/src/dnsimple/Services/DomainsEmailForwards.cs index baec5e4..3ee9096 100644 --- a/src/dnsimple/Services/DomainsEmailForwards.cs +++ b/src/dnsimple/Services/DomainsEmailForwards.cs @@ -42,7 +42,7 @@ public PaginatedResponse ListEmailForwards(long accountId, string public SimpleResponse CreateEmailForward(long accountId, string domainIdentifier, EmailForward record) { var builder = BuildRequestForPath(EmailForwardsPath(accountId, domainIdentifier)); - builder.Method(Method.POST); + builder.Method(Method.Post); builder.AddJsonPayload(record); if (record.DestinationEmail.Trim().Equals("") || record.AliasName.Trim().Equals("")) @@ -76,7 +76,7 @@ public SimpleResponse GetEmailForward(long accountId, string domai public EmptyResponse DeleteEmailForward(long accountId, string domainIdentifier, int emailForwardId) { var builder = BuildRequestForPath(EmailForwardPath(accountId, domainIdentifier, emailForwardId)); - builder.Method(Method.DELETE); + builder.Method(Method.Delete); return new EmptyResponse(Execute(builder.Request)); } diff --git a/src/dnsimple/Services/DomainsPushes.cs b/src/dnsimple/Services/DomainsPushes.cs index 1ee20c1..d7e419d 100644 --- a/src/dnsimple/Services/DomainsPushes.cs +++ b/src/dnsimple/Services/DomainsPushes.cs @@ -25,7 +25,7 @@ public SimpleResponse InitiatePushWithIdentifier(long accountId, string do throw new ArgumentException("Account identifier cannot be null or empty"); var builder = BuildRequestForPath(InitiatePushPath(accountId, domainIdentifier)); - builder.Method(Method.POST); + builder.Method(Method.Post); builder.AddJsonPayload(PushPayload("new_account_identifier", newAccountIdentifier)); return new SimpleResponse(Execute(builder.Request)); @@ -46,7 +46,7 @@ public SimpleResponse InitiatePush(long accountId, string domainIdentifier throw new ArgumentException("Email cannot be null or empty"); var builder = BuildRequestForPath(InitiatePushPath(accountId, domainIdentifier)); - builder.Method(Method.POST); + builder.Method(Method.Post); builder.AddJsonPayload(PushPayload("new_account_email", email)); return new SimpleResponse(Execute(builder.Request)); @@ -77,7 +77,7 @@ public PaginatedResponse ListPushes(long accountId) public EmptyResponse AcceptPush(long accountId, long pushId, long contactId) { var builder = BuildRequestForPath(PushPath(accountId, pushId)); - builder.Method(Method.POST); + builder.Method(Method.Post); builder.AddJsonPayload(PushPayload("contact_id", contactId.ToString())); return new EmptyResponse(Execute(builder.Request)); @@ -92,18 +92,14 @@ public EmptyResponse AcceptPush(long accountId, long pushId, long contactId) public EmptyResponse RejectPush(int accountId, int pushId) { var builder = BuildRequestForPath(PushPath(accountId, pushId)); - builder.Method(Method.DELETE); + builder.Method(Method.Delete); return new EmptyResponse(Execute(builder.Request)); } - private static JsonObject PushPayload(string key, string value) + private static Dictionary PushPayload(string key, string value) { - var payload = new JsonObject - { - new KeyValuePair(key, value) - }; - return payload; + return new Dictionary { { key, value } }; } } diff --git a/src/dnsimple/Services/Http.cs b/src/dnsimple/Services/Http.cs index 45e63cf..7695306 100644 --- a/src/dnsimple/Services/Http.cs +++ b/src/dnsimple/Services/Http.cs @@ -10,22 +10,19 @@ namespace dnsimple.Services public class HttpService { private readonly RequestBuilder _builder; - private IRestClient RestClient { get; } + private RestClientWrapper ClientWrapper { get; } protected HttpService() {} /// /// Constructs the HTTP service by passing an instance of a - /// RestClient and RequestBuilder objects. + /// RestClientWrapper and RequestBuilder objects. /// - /// RestClient instance to be used. + /// RestClientWrapper instance to be used. /// RequestBuilder instance to be used. - /// - /// - /// - public HttpService(IRestClient restClient, RequestBuilder builder) + public HttpService(RestClientWrapper clientWrapper, RequestBuilder builder) { - RestClient = restClient; + ClientWrapper = clientWrapper; _builder = builder; } @@ -37,7 +34,7 @@ public HttpService(IRestClient restClient, RequestBuilder builder) /// var builder = new RequestBuilder("/whoami"); /// /// - /// + /// /// Path to the resource /// RequestBuilder instance. public virtual RequestBuilder RequestBuilder(string path) @@ -51,31 +48,29 @@ public virtual RequestBuilder RequestBuilder(string path) /// Executes the request passed (GET, POST, etc.) /// /// - /// The RestRequest instance will have been build with a + /// The RestRequest instance will have been built with a /// RequestBuilder and contains all the information /// (headers and parameters) needed to successfully issue the /// request to the server. /// /// RestRequest /// - /// A JToken object representing the JSON payload returned - /// by the API call. + /// The RestResponse returned by the API call. /// - /// - public virtual IRestResponse Execute(IRestRequest request) + public virtual RestResponse Execute(RestRequest request) { - var response = RestClient.Execute(request); + var response = ClientWrapper.RestClient.Execute(request); if (!response.IsSuccessful) HandleExceptions(response); return response; } - private static void HandleExceptions(IRestResponse restResponse) + internal static void HandleExceptions(RestResponse restResponse) { var error = JObject.Parse(restResponse.Content); var message = error["message"]?.ToString(); - + switch (restResponse.StatusCode) { case HttpStatusCode.BadRequest: diff --git a/src/dnsimple/Services/OAuth2.cs b/src/dnsimple/Services/OAuth2.cs index f2f19cd..39ca6c6 100644 --- a/src/dnsimple/Services/OAuth2.cs +++ b/src/dnsimple/Services/OAuth2.cs @@ -59,7 +59,7 @@ public AccessToken ExchangeAuthorizationForToken(Dictionary private RestRequest BuildRequest(string path, IReadOnlyDictionary arguments) { var builder = Http.RequestBuilder(path); - builder.Method(Method.POST); + builder.Method(Method.Post); builder.AddHeaders(Headers()); builder.AddParameters(PostParameters(arguments)); return builder.Request; diff --git a/src/dnsimple/Services/Registrar.cs b/src/dnsimple/Services/Registrar.cs index 0620239..4a34675 100644 --- a/src/dnsimple/Services/Registrar.cs +++ b/src/dnsimple/Services/Registrar.cs @@ -58,7 +58,7 @@ public SimpleResponse GetDomainPrices(long accountId, string domai public SimpleResponse RegisterDomain(long accountId, string domainName, DomainRegistrationInput domain) { var builder = BuildRequestForPath(RegisterDomainPath(accountId, domainName)); - builder.Method(Method.POST); + builder.Method(Method.Post); builder.AddJsonPayload(domain); return new SimpleResponse(Execute(builder.Request)); @@ -96,7 +96,7 @@ public SimpleResponse TransferDomain(long accountId, string doma throw new DnsimpleException("Please provide an AuthCode"); } var builder = BuildRequestForPath(TransferDomainPath(accountId, domainName)); - builder.Method(Method.POST); + builder.Method(Method.Post); builder.AddJsonPayload(transferInput); return new SimpleResponse(Execute(builder.Request)); @@ -127,7 +127,7 @@ public SimpleResponse GetDomainTransfer(long accountId, string d public SimpleResponse CheckRegistrantChange(long accountId, CheckRegistrantChangeInput input) { var builder = BuildRequestForPath(CheckRegistrantChangePath(accountId)); - builder.Method(Method.POST); + builder.Method(Method.Post); builder.AddJsonPayload(input); return new SimpleResponse(Execute(builder.Request)); @@ -155,7 +155,7 @@ public SimpleResponse GetRegistrantChange(long accountId, long public SimpleResponse CreateRegistrantChange(long accountId, CreateRegistrantChangeInput input) { var builder = BuildRequestForPath(RegistrantChangesPath(accountId)); - builder.Method(Method.POST); + builder.Method(Method.Post); builder.AddJsonPayload(input); return new SimpleResponse(Execute(builder.Request)); @@ -187,7 +187,7 @@ public PaginatedResponse ListRegistrantChanges(long accountId, public SimpleResponseOrEmpty DeleteRegistrantChange(long accountId, long registrantChangeId) { var builder = BuildRequestForPath(RegistrantChangePath(accountId, registrantChangeId)); - builder.Method(Method.DELETE); + builder.Method(Method.Delete); return new SimpleResponseOrEmpty(Execute(builder.Request)); } @@ -204,7 +204,7 @@ public SimpleResponseOrEmpty DeleteRegistrantChange(long accou public SimpleResponse CancelDomainTransfer(long accountId, string domainName, long domainTransferId) { var builder = BuildRequestForPath(DomainTransferPath(accountId, domainName, domainTransferId)); - builder.Method(Method.DELETE); + builder.Method(Method.Delete); return new SimpleResponse(Execute(builder.Request)); } @@ -220,7 +220,7 @@ public SimpleResponse CancelDomainTransfer(long accountId, strin public SimpleResponse RenewDomain(long accountId, string domainName, DomainRenewalInput input) { var builder = BuildRequestForPath(RenewDomainPath(accountId, domainName)); - builder.Method(Method.POST); + builder.Method(Method.Post); builder.AddJsonPayload(input); return new SimpleResponse(Execute(builder.Request)); @@ -251,7 +251,7 @@ public SimpleResponse GetDomainRenewal(long accountId, string dom public EmptyResponse TransferDomainOut(long accountId, string domainName) { var builder = BuildRequestForPath(DomainTransferOutPath(accountId, domainName)); - builder.Method(Method.POST); + builder.Method(Method.Post); return new EmptyResponse(Execute(builder.Request)); } diff --git a/src/dnsimple/Services/RegistrarAutoRenewal.cs b/src/dnsimple/Services/RegistrarAutoRenewal.cs index 02f6bbb..fdf967a 100644 --- a/src/dnsimple/Services/RegistrarAutoRenewal.cs +++ b/src/dnsimple/Services/RegistrarAutoRenewal.cs @@ -15,7 +15,7 @@ public partial class RegistrarService /// https://developer.dnsimple.com/v2/registrar/auto-renewal/#enableDomainAutoRenewal public EmptyResponse EnableDomainAutoRenewal(long accountId, string domain) { - return DomainAutoRenewal(accountId, domain, Method.PUT); + return DomainAutoRenewal(accountId, domain, Method.Put); } /// @@ -26,7 +26,7 @@ public EmptyResponse EnableDomainAutoRenewal(long accountId, string domain) /// https://developer.dnsimple.com/v2/registrar/auto-renewal/#disableDomainAutoRenewal public EmptyResponse DisableDomainAutoRenewal(long accountId, string domain) { - return DomainAutoRenewal(accountId, domain, Method.DELETE); + return DomainAutoRenewal(accountId, domain, Method.Delete); } private EmptyResponse DomainAutoRenewal(long accountId, string domain, Method method) diff --git a/src/dnsimple/Services/RegistrarDelegation.cs b/src/dnsimple/Services/RegistrarDelegation.cs index 8ba91ed..915e6ec 100644 --- a/src/dnsimple/Services/RegistrarDelegation.cs +++ b/src/dnsimple/Services/RegistrarDelegation.cs @@ -37,7 +37,7 @@ public DelegationResponse GetDomainDelegation(long accountId, string domain) public DelegationResponse ChangeDomainDelegation(long accountId, string domain, IList delegation) { var builder = BuildRequestForPath(DelegationPath(accountId, domain)); - builder.Method(Method.PUT); + builder.Method(Method.Put); builder.AddJsonPayloadRaw(delegation); return new DelegationResponse(Execute(builder.Request)); @@ -54,7 +54,7 @@ public DelegationResponse ChangeDomainDelegation(long accountId, string domain, public ListResponse ChangeDomainDelegationToVanity(long accountId, string domain, List delegation) { var builder = BuildRequestForPath(VanityDelegationPath(accountId, domain)); - builder.Method(Method.PUT); + builder.Method(Method.Put); builder.AddJsonPayloadRaw(delegation); return new ListResponse(Execute(builder.Request)); @@ -69,7 +69,7 @@ public ListResponse ChangeDomainDelegationToVanity(long accoun public EmptyResponse ChangeDomainDelegationFromVanity(long accountId, string domain) { var builder = BuildRequestForPath(VanityDelegationPath(accountId, domain)); - builder.Method(Method.DELETE); + builder.Method(Method.Delete); return new EmptyResponse(Execute(builder.Request)); } @@ -84,7 +84,7 @@ public class DelegationResponse public DelegationResponse(JToken json) => Data = JsonTools.DeserializeList(json); - public DelegationResponse(IRestResponse response) => Data = JsonTools.DeserializeList( + public DelegationResponse(RestResponse response) => Data = JsonTools.DeserializeList( JObject.Parse(response.Content) ); } diff --git a/src/dnsimple/Services/RegistrarTransferLock.cs b/src/dnsimple/Services/RegistrarTransferLock.cs index 3ae4800..cc02789 100644 --- a/src/dnsimple/Services/RegistrarTransferLock.cs +++ b/src/dnsimple/Services/RegistrarTransferLock.cs @@ -20,7 +20,7 @@ public partial class RegistrarService public SimpleResponse EnableDomainTransferLock(long accountId, string domain) { var builder = BuildRequestForPath(TransferLockPath(accountId, domain)); - builder.Method(Method.POST); + builder.Method(Method.Post); return new SimpleResponse(Execute(builder.Request)); } @@ -35,7 +35,7 @@ public SimpleResponse EnableDomainTransferLock(long accountI public SimpleResponse DisableDomainTransferLock(long accountId, string domain) { var builder = BuildRequestForPath(TransferLockPath(accountId, domain)); - builder.Method(Method.DELETE); + builder.Method(Method.Delete); return new SimpleResponse(Execute(builder.Request)); } diff --git a/src/dnsimple/Services/RegistrarWhoisPrivacy.cs b/src/dnsimple/Services/RegistrarWhoisPrivacy.cs index 60aeb53..962216f 100644 --- a/src/dnsimple/Services/RegistrarWhoisPrivacy.cs +++ b/src/dnsimple/Services/RegistrarWhoisPrivacy.cs @@ -31,7 +31,7 @@ public partial class RegistrarService public SimpleResponse EnableWhoisPrivacy(long accountId, string domain) { var builder = BuildRequestForPath(WhoisPrivacyPath(accountId, domain)); - builder.Method(Method.PUT); + builder.Method(Method.Put); return new SimpleResponse(Execute(builder.Request)); } @@ -46,7 +46,7 @@ public SimpleResponse EnableWhoisPrivacy(long accountId, string do public SimpleResponse DisableWhoisPrivacy(long accountId, string domain) { var builder = BuildRequestForPath(WhoisPrivacyPath(accountId, domain)); - builder.Method(Method.DELETE); + builder.Method(Method.Delete); return new SimpleResponse(Execute(builder.Request)); } diff --git a/src/dnsimple/Services/ServiceBase.cs b/src/dnsimple/Services/ServiceBase.cs index b880124..7268d32 100644 --- a/src/dnsimple/Services/ServiceBase.cs +++ b/src/dnsimple/Services/ServiceBase.cs @@ -24,7 +24,7 @@ protected RequestBuilder BuildRequestForPath(string path) return Client.Http.RequestBuilder(path); } - protected IRestResponse Execute(RestRequest request) + protected RestResponse Execute(RestRequest request) { return Client.Http.Execute(request); } @@ -45,12 +45,12 @@ protected static void AddListOptionsToRequest( public abstract class Response { - public readonly IList Headers; + public readonly IReadOnlyCollection Headers; public readonly int RateLimit; public readonly int RateLimitRemaining; public readonly int RateLimitReset; - public Response(IRestResponse response) + public Response(RestResponse response) { Headers = response.Headers; RateLimit = int.Parse(ExtractValueFromHeader("X-RateLimit-Limit")); @@ -60,9 +60,9 @@ public Response(IRestResponse response) private string ExtractValueFromHeader(string headerName) { - return (string)Headers.Where(header => + return Headers.First(header => header.Name != null && header.Name.Equals(headerName, System.StringComparison.OrdinalIgnoreCase)) - .First().Value; + .Value?.ToString(); } } @@ -72,7 +72,7 @@ private string ExtractValueFromHeader(string headerName) /// public class EmptyResponse : Response { - public EmptyResponse(IRestResponse response) : base(response) + public EmptyResponse(RestResponse response) : base(response) { } } @@ -88,7 +88,7 @@ public class SimpleResponse : Response /// public T Data { get; protected set; } - public SimpleResponse(IRestResponse response) : base(response) + public SimpleResponse(RestResponse response) : base(response) { Data = JsonTools.DeserializeObject("data", JObject.Parse(response.Content)); } @@ -108,7 +108,7 @@ public class SimpleResponseOrEmpty : Response public bool IsEmpty { get; protected set; } - public SimpleResponseOrEmpty(IRestResponse response) : base(response) + public SimpleResponseOrEmpty(RestResponse response) : base(response) { if (response.StatusCode == System.Net.HttpStatusCode.NoContent) { @@ -136,7 +136,7 @@ public SimpleResponseOrEmpty(IRestResponse response) : base(response) /// public List Data { get; } - public ListResponse(IRestResponse response) : base(response) + public ListResponse(RestResponse response) : base(response) { Data = JsonTools.DeserializeList(JObject.Parse(response.Content)); } @@ -161,7 +161,7 @@ public ListResponse(IRestResponse response) : base(response) /// public Pagination Pagination { get; } - public PaginatedResponse(IRestResponse response) : base(response) + public PaginatedResponse(RestResponse response) : base(response) { var json = JObject.Parse(response.Content); diff --git a/src/dnsimple/Services/ServicesDomains.cs b/src/dnsimple/Services/ServicesDomains.cs index 4eb7579..bfa72f6 100644 --- a/src/dnsimple/Services/ServicesDomains.cs +++ b/src/dnsimple/Services/ServicesDomains.cs @@ -30,7 +30,7 @@ public PaginatedResponse AppliedServices(long accountId, string domainI public EmptyResponse ApplyService(long accountId, string domainIdentifier, string serviceIdentifier) { var builder = BuildRequestForPath(ApplyServicePath(accountId, domainIdentifier, serviceIdentifier)); - builder.Method(Method.POST); + builder.Method(Method.Post); return new EmptyResponse(Execute(builder.Request)); } @@ -45,7 +45,7 @@ public EmptyResponse ApplyService(long accountId, string domainIdentifier, strin public EmptyResponse UnapplyService(long accountId, string domainIdentifier, string serviceIdentifier) { var builder = BuildRequestForPath(ApplyServicePath(accountId, domainIdentifier, serviceIdentifier)); - builder.Method(Method.DELETE); + builder.Method(Method.Delete); return new EmptyResponse(Execute(builder.Request)); } diff --git a/src/dnsimple/Services/TemplateDomains.cs b/src/dnsimple/Services/TemplateDomains.cs index 3acb26e..7aaab41 100644 --- a/src/dnsimple/Services/TemplateDomains.cs +++ b/src/dnsimple/Services/TemplateDomains.cs @@ -15,7 +15,7 @@ public partial class TemplatesService public EmptyResponse ApplyTemplate(long accountId, string domainIdentifier, string templateIdentifier) { var builder = BuildRequestForPath(TemplateDomainPath(accountId, domainIdentifier, templateIdentifier)); - builder.Method(Method.POST); + builder.Method(Method.Post); return new EmptyResponse(Execute(builder.Request)); } diff --git a/src/dnsimple/Services/TemplateRecords.cs b/src/dnsimple/Services/TemplateRecords.cs index 2c12205..b7141ff 100644 --- a/src/dnsimple/Services/TemplateRecords.cs +++ b/src/dnsimple/Services/TemplateRecords.cs @@ -39,7 +39,7 @@ public PaginatedResponse ListTemplateRecords(long accountId, str public SimpleResponse CreateTemplateRecord(long accountId, string template, TemplateRecord payload) { var builder = BuildRequestForPath(TemplateRecordsPath(accountId, template)); - builder.Method(Method.POST); + builder.Method(Method.Post); builder.AddJsonPayload(payload); return new SimpleResponse(Execute(builder.Request)); @@ -70,7 +70,7 @@ public SimpleResponse GetTemplateRecord(long accountId, string t public EmptyResponse DeleteTemplateRecord(long accountId, string template, long recordId) { var builder = BuildRequestForPath(TemplateRecordPath(accountId, template, recordId)); - builder.Method(Method.DELETE); + builder.Method(Method.Delete); return new EmptyResponse(Execute(builder.Request)); } diff --git a/src/dnsimple/Services/Templates.cs b/src/dnsimple/Services/Templates.cs index 1b3e1c5..81e11c0 100644 --- a/src/dnsimple/Services/Templates.cs +++ b/src/dnsimple/Services/Templates.cs @@ -46,7 +46,7 @@ public PaginatedResponse