diff --git a/README.md b/README.md index a311382..f0162d0 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,16 @@ If you are building a third party application you will need to implement OAuth b ## Usage +### For all client types + +It is now possible to create all types of client by either supplying the authentication object instance or by providing an instance of the new RestClientFactory. The latter is the new preferred method to construct instances of the various clients. The older constructor methods have been marked as obsolete and will be removed in later versions. + +```cs +Authentication auth = new Authentication("MyPersonalAccessToken"); +RestClientFactory factory = new RestClientFactory(auth); +UsersClient usersClient = new UsersClient(factory); +``` + ### Users **Create UsersClient instance** diff --git a/src/Intercom.Tests.Integration/Intercom.Tests.Integration.csproj b/src/Intercom.Tests.Integration/Intercom.Tests.Integration.csproj index e602a30..99ce0a3 100644 --- a/src/Intercom.Tests.Integration/Intercom.Tests.Integration.csproj +++ b/src/Intercom.Tests.Integration/Intercom.Tests.Integration.csproj @@ -2,7 +2,7 @@ netcoreapp2.0 - 2.0.0 + 3.0.0 diff --git a/src/Intercom.Tests/Clients/AdminClientTest.cs b/src/Intercom.Tests/Clients/AdminClientTest.cs index 6ab397e..a4553f0 100755 --- a/src/Intercom.Tests/Clients/AdminClientTest.cs +++ b/src/Intercom.Tests/Clients/AdminClientTest.cs @@ -4,6 +4,7 @@ using Intercom.Data; using Intercom.Clients; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; using RestSharp.Authenticators; using System.Collections.Generic; @@ -20,7 +21,9 @@ public class AdminClientTest : TestBase public AdminClientTest() : base() { - this.adminsClient = new AdminsClient(new Authentication(AppId, AppKey)); + var auth = new Authentication(AppId, AppKey); + var restClientFactory = new RestClientFactory(auth); + adminsClient = new AdminsClient(restClientFactory); } [Test()] diff --git a/src/Intercom.Tests/Clients/AdminConversationsClientTest.cs b/src/Intercom.Tests/Clients/AdminConversationsClientTest.cs index cdd3ebb..babd02b 100755 --- a/src/Intercom.Tests/Clients/AdminConversationsClientTest.cs +++ b/src/Intercom.Tests/Clients/AdminConversationsClientTest.cs @@ -4,6 +4,7 @@ using Intercom.Data; using Intercom.Clients; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; using RestSharp.Authenticators; using System.Collections.Generic; @@ -20,7 +21,9 @@ public class AdminConversationsClientTest : TestBase public AdminConversationsClientTest() { - this.adminConversationsClient = new AdminConversationsClient(new Authentication(AppId, AppKey)); + var auth = new Authentication(AppId, AppKey); + var restClientFactory = new RestClientFactory(auth); + adminConversationsClient = new AdminConversationsClient(restClientFactory); } [Test()] diff --git a/src/Intercom.Tests/Clients/CompanyClientTest.cs b/src/Intercom.Tests/Clients/CompanyClientTest.cs index 24d88a3..629a2f4 100755 --- a/src/Intercom.Tests/Clients/CompanyClientTest.cs +++ b/src/Intercom.Tests/Clients/CompanyClientTest.cs @@ -4,6 +4,7 @@ using Intercom.Data; using Intercom.Clients; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; using RestSharp.Authenticators; using System.Collections.Generic; @@ -19,7 +20,9 @@ public class CompanyClientTest : TestBase public CompanyClientTest() { - this.companyClient = new CompanyClient(new Authentication(AppId, AppKey)); + var auth = new Authentication(AppId, AppKey); + var restClientFactory = new RestClientFactory(auth); + companyClient = new CompanyClient(restClientFactory); } [Test()] diff --git a/src/Intercom.Tests/Clients/ContactClientTest.cs b/src/Intercom.Tests/Clients/ContactClientTest.cs index f8ca856..e1502aa 100755 --- a/src/Intercom.Tests/Clients/ContactClientTest.cs +++ b/src/Intercom.Tests/Clients/ContactClientTest.cs @@ -4,6 +4,7 @@ using Intercom.Data; using Intercom.Clients; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; using RestSharp.Authenticators; using System.Collections.Generic; @@ -20,7 +21,9 @@ public class ContactClientTest : TestBase public ContactClientTest() : base() { - this.contactsClient = new ContactsClient(new Authentication(AppId, AppKey)); + var auth = new Authentication(AppId, AppKey); + var restClientFactory = new RestClientFactory(auth); + contactsClient = new ContactsClient(restClientFactory); } [Test()] diff --git a/src/Intercom.Tests/Clients/ConversationsClientTest.cs b/src/Intercom.Tests/Clients/ConversationsClientTest.cs index 3e24230..fea4ef5 100755 --- a/src/Intercom.Tests/Clients/ConversationsClientTest.cs +++ b/src/Intercom.Tests/Clients/ConversationsClientTest.cs @@ -4,6 +4,7 @@ using Intercom.Data; using Intercom.Clients; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; using RestSharp.Authenticators; using System.Collections.Generic; @@ -19,7 +20,9 @@ public class ConversationsClientTest : TestBase public ConversationsClientTest() { - this.conversationsClient = new ConversationsClient(new Authentication(AppId, AppKey)); + var auth = new Authentication(AppId, AppKey); + var restClientFactory = new RestClientFactory(auth); + conversationsClient = new ConversationsClient(restClientFactory); } [Test()] diff --git a/src/Intercom.Tests/Clients/EventsClientTest.cs b/src/Intercom.Tests/Clients/EventsClientTest.cs index 23c8f2b..a833237 100755 --- a/src/Intercom.Tests/Clients/EventsClientTest.cs +++ b/src/Intercom.Tests/Clients/EventsClientTest.cs @@ -4,6 +4,7 @@ using Intercom.Data; using Intercom.Clients; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; using RestSharp.Authenticators; using System.Collections.Generic; @@ -19,7 +20,9 @@ public class EventsClientTest : TestBase public EventsClientTest() { - this.eventsClient = new EventsClient(new Authentication(AppId, AppKey)); + var auth = new Authentication(AppId, AppKey); + var restClientFactory = new RestClientFactory(auth); + eventsClient = new EventsClient(restClientFactory); } [Test()] diff --git a/src/Intercom.Tests/Clients/NotesClientTest.cs b/src/Intercom.Tests/Clients/NotesClientTest.cs index 689486c..eb5845c 100755 --- a/src/Intercom.Tests/Clients/NotesClientTest.cs +++ b/src/Intercom.Tests/Clients/NotesClientTest.cs @@ -4,6 +4,7 @@ using Intercom.Data; using Intercom.Clients; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; using RestSharp.Authenticators; using System.Collections.Generic; @@ -19,7 +20,9 @@ public class NotesClientTest : TestBase public NotesClientTest() { - this.notesClient = new NotesClient(new Authentication(AppId, AppKey)); + var auth = new Authentication(AppId, AppKey); + var restClientFactory = new RestClientFactory(auth); + notesClient = new NotesClient(restClientFactory); } [Test()] diff --git a/src/Intercom.Tests/Clients/TagsClientTest.cs b/src/Intercom.Tests/Clients/TagsClientTest.cs index ca930b4..3c9ce0d 100755 --- a/src/Intercom.Tests/Clients/TagsClientTest.cs +++ b/src/Intercom.Tests/Clients/TagsClientTest.cs @@ -4,6 +4,7 @@ using Intercom.Core; using Intercom.Data; using Intercom.Exceptions; +using Intercom.Factories; using Moq; using Newtonsoft.Json; using NUnit.Framework; @@ -19,7 +20,9 @@ public class TagsClientTest : TestBase public TagsClientTest() { - this.tagsClient = new TagsClient(new Authentication(AppId, AppKey)); + var auth = new Authentication(AppId, AppKey); + var restClientFactory = new RestClientFactory(auth); + tagsClient = new TagsClient(restClientFactory); } [Test()] diff --git a/src/Intercom.Tests/Clients/UserClientTest.cs b/src/Intercom.Tests/Clients/UserClientTest.cs index a0e2083..f99ac7c 100755 --- a/src/Intercom.Tests/Clients/UserClientTest.cs +++ b/src/Intercom.Tests/Clients/UserClientTest.cs @@ -1,9 +1,11 @@ using NUnit.Framework; using System; +using System.Net; using Intercom.Core; using Intercom.Data; using Intercom.Clients; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; using RestSharp.Authenticators; using System.Collections.Generic; @@ -17,39 +19,71 @@ public class UserClientTest : TestBase { private UsersClient usersClient; - public UserClientTest() + public UserClientTest() { } + + private void SetupMock(RestClient restClient = null) { - this.usersClient = new UsersClient(new Authentication(AppId, AppKey)); + var auth = new Authentication(AppId, AppKey); + if (restClient == null) + { + var restClientMock = new Mock(); + restClient = restClientMock.Object; + } + var restClientFactoryMock = new Mock(new object[] { auth }); + restClientFactoryMock.Setup(x => x.RestClient).Returns(restClient); + var restClientFactory = restClientFactoryMock.Object; + usersClient = new UsersClient(restClientFactory); } [Test()] public void Create_WithNull_ThrowException() { + SetupMock(); Assert.Throws(() => usersClient.Create(null)); } [Test()] public void Create_NoUserIdOrEmail_ThrowException() { + SetupMock(); Assert.Throws(() => usersClient.Create(new User())); } [Test()] public void Archive_NoIdOrUserIdOrEmail_ThrowException() { + SetupMock(); Assert.Throws(() => usersClient.Archive(new User())); } [Test()] public void PermanentlyDeleteUser_NoId_ThrowException() { + SetupMock(); Assert.Throws(() => usersClient.PermanentlyDeleteUser(null)); } [Test()] public void Update_NoIdOrUserIdOrEmail_ThrowException() { + SetupMock(); Assert.Throws(() => usersClient.Update(new User())); } + + [Test()] + public void View_ByStringId_ReturnsObjectAsExpected() + { + var userId = "id"; + var restClientMock = new Mock(); + var restResponse = new RestResponse() + { + StatusCode = HttpStatusCode.OK, + Content = $"{{ \"type\": \"user\", \"id\": \"530370b477ad7120001d\", \"user_id\": \"{userId}\", \"email\": \"wash@serenity.io\" }}", + }; + restClientMock.Setup(x => x.Execute(It.IsAny())).Returns(restResponse); + var restClient = restClientMock.Object; + SetupMock(restClient); + Assert.AreEqual(userId, usersClient.View(userId).user_id); + } } } diff --git a/src/Intercom.Tests/Clients/UserConversationsClientTest.cs b/src/Intercom.Tests/Clients/UserConversationsClientTest.cs index 44650c3..cd350ed 100755 --- a/src/Intercom.Tests/Clients/UserConversationsClientTest.cs +++ b/src/Intercom.Tests/Clients/UserConversationsClientTest.cs @@ -4,6 +4,7 @@ using Intercom.Data; using Intercom.Clients; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; using RestSharp.Authenticators; using System.Collections.Generic; @@ -19,7 +20,9 @@ public class UserConversationsClientTest : TestBase public UserConversationsClientTest() { - this.userConversationsClient = new UserConversationsClient(new Authentication(AppId, AppKey)); + var auth = new Authentication(AppId, AppKey); + var restClientFactory = new RestClientFactory(auth); + userConversationsClient = new UserConversationsClient(restClientFactory); } [Test()] diff --git a/src/Intercom.Tests/Intercom.Tests.csproj b/src/Intercom.Tests/Intercom.Tests.csproj index 1db607a..1643889 100644 --- a/src/Intercom.Tests/Intercom.Tests.csproj +++ b/src/Intercom.Tests/Intercom.Tests.csproj @@ -2,7 +2,7 @@ netcoreapp2.0 - 2.0.0 + 3.0.0 diff --git a/src/Intercom/Clients/AdminConversationsClient.cs b/src/Intercom/Clients/AdminConversationsClient.cs index cb612d6..5252fd3 100644 --- a/src/Intercom/Clients/AdminConversationsClient.cs +++ b/src/Intercom/Clients/AdminConversationsClient.cs @@ -6,6 +6,7 @@ using Intercom.Core; using Intercom.Data; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; using RestSharp.Authenticators; @@ -17,11 +18,18 @@ public class AdminConversationsClient: Client private const String MESSAGES_RESOURCE = "messages"; private const String REPLY_RESOURCE = "reply"; + public AdminConversationsClient(RestClientFactory restClientFactory) + : base(CONVERSATIONS_RESOURCE, restClientFactory) + { + } + + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use AdminConversationsClient(RestClientFactory restClientFactory)")] public AdminConversationsClient(Authentication authentication) : base(INTERCOM_API_BASE_URL, CONVERSATIONS_RESOURCE, authentication) { } + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use AdminConversationsClient(RestClientFactory restClientFactory)")] public AdminConversationsClient(String intercomApiUrl, Authentication authentication) : base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, CONVERSATIONS_RESOURCE, authentication) { diff --git a/src/Intercom/Clients/AdminsClient.cs b/src/Intercom/Clients/AdminsClient.cs index c31e820..cc82fb2 100644 --- a/src/Intercom/Clients/AdminsClient.cs +++ b/src/Intercom/Clients/AdminsClient.cs @@ -6,6 +6,7 @@ using Intercom.Core; using Intercom.Data; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; using RestSharp.Authenticators; @@ -15,11 +16,18 @@ public class AdminsClient : Client { private const String ADMINS_RESOURCE = "admins"; - public AdminsClient (Authentication authentication) - : base (INTERCOM_API_BASE_URL, ADMINS_RESOURCE, authentication) + public AdminsClient (RestClientFactory restClientFactory) + : base (ADMINS_RESOURCE, restClientFactory) { } + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use AdminsClient(RestClientFactory restClientFactory)")] + public AdminsClient(Authentication authentication) + : base(INTERCOM_API_BASE_URL, ADMINS_RESOURCE, authentication) + { + } + + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use AdminsClient(RestClientFactory restClientFactory)")] public AdminsClient(String intercomApiUrl, Authentication authentication) : base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, ADMINS_RESOURCE, authentication) { diff --git a/src/Intercom/Clients/CompanyClient.cs b/src/Intercom/Clients/CompanyClient.cs index e5c0266..b004fdb 100644 --- a/src/Intercom/Clients/CompanyClient.cs +++ b/src/Intercom/Clients/CompanyClient.cs @@ -4,6 +4,7 @@ using System.Linq; using Intercom.Core; using Intercom.Data; +using Intercom.Factories; using Newtonsoft.Json; namespace Intercom.Clients @@ -13,11 +14,18 @@ public class CompanyClient : Client { private const String COMPANIES_RESOURCE = "companies"; + public CompanyClient(RestClientFactory restClientFactory) + : base(COMPANIES_RESOURCE, restClientFactory) + { + } + + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use CompanyClient(RestClientFactory restClientFactory)")] public CompanyClient(Authentication authentication) : base(INTERCOM_API_BASE_URL, COMPANIES_RESOURCE, authentication) { } + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use CompanyClient(RestClientFactory restClientFactory)")] public CompanyClient(String intercomApiUrl, Authentication authentication) : base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, COMPANIES_RESOURCE, authentication) { diff --git a/src/Intercom/Clients/ContactsClient.cs b/src/Intercom/Clients/ContactsClient.cs index d2c250f..a523066 100644 --- a/src/Intercom/Clients/ContactsClient.cs +++ b/src/Intercom/Clients/ContactsClient.cs @@ -4,6 +4,7 @@ using System.Linq; using Intercom.Core; using Intercom.Data; +using Intercom.Factories; using Newtonsoft.Json; namespace Intercom.Clients @@ -19,11 +20,18 @@ public static class ContactSortBy private const String CONTACTS_RESOURCE = "contacts"; - public ContactsClient (Authentication authentication) - : base (INTERCOM_API_BASE_URL, CONTACTS_RESOURCE, authentication) + public ContactsClient (RestClientFactory restClientFactory) + : base (CONTACTS_RESOURCE, restClientFactory) { } + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use ContactsClient(RestClientFactory restClientFactory)")] + public ContactsClient(Authentication authentication) + : base(INTERCOM_API_BASE_URL, CONTACTS_RESOURCE, authentication) + { + } + + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use ContactsClient(RestClientFactory restClientFactory)")] public ContactsClient(String intercomApiUrl, Authentication authentication) : base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, CONTACTS_RESOURCE, authentication) { diff --git a/src/Intercom/Clients/ConversationsClient.cs b/src/Intercom/Clients/ConversationsClient.cs index 3af254b..bcfcf97 100644 --- a/src/Intercom/Clients/ConversationsClient.cs +++ b/src/Intercom/Clients/ConversationsClient.cs @@ -6,6 +6,7 @@ using Intercom.Core; using Intercom.Data; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; using RestSharp.Authenticators; @@ -24,11 +25,18 @@ public static class MessageType private const String CONVERSATIONS_RESOURCE = "conversations"; - public ConversationsClient( Authentication authentication) + public ConversationsClient( RestClientFactory restClientFactory) + : base(CONVERSATIONS_RESOURCE, restClientFactory) + { + } + + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use ConversationsClient(RestClientFactory restClientFactory)")] + public ConversationsClient(Authentication authentication) : base(INTERCOM_API_BASE_URL, CONVERSATIONS_RESOURCE, authentication) { } + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use ConversationsClient(RestClientFactory restClientFactory)")] public ConversationsClient(String intercomApiUrl, Authentication authentication) : base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, CONVERSATIONS_RESOURCE, authentication) { diff --git a/src/Intercom/Clients/CountsClient.cs b/src/Intercom/Clients/CountsClient.cs index 4c3a6a4..cbbfe85 100644 --- a/src/Intercom/Clients/CountsClient.cs +++ b/src/Intercom/Clients/CountsClient.cs @@ -6,6 +6,7 @@ using Intercom.Core; using Intercom.Data; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; using RestSharp.Authenticators; @@ -15,11 +16,18 @@ public class CountsClient: Client { private const String COUNTS_RESOURCE = "counts"; - public CountsClient (Authentication authentication) - : base (INTERCOM_API_BASE_URL, COUNTS_RESOURCE, authentication) + public CountsClient (RestClientFactory restClientFactory) + : base (COUNTS_RESOURCE, restClientFactory) { } + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use CountsClient(RestClientFactory restClientFactory)")] + public CountsClient(Authentication authentication) + : base(INTERCOM_API_BASE_URL, COUNTS_RESOURCE, authentication) + { + } + + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use CountsClient(RestClientFactory restClientFactory)")] public CountsClient(String intercomApiUrl, Authentication authentication) : base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, COUNTS_RESOURCE, authentication) { diff --git a/src/Intercom/Clients/EventClient.cs b/src/Intercom/Clients/EventClient.cs index 51b66cc..afc4ce2 100644 --- a/src/Intercom/Clients/EventClient.cs +++ b/src/Intercom/Clients/EventClient.cs @@ -6,6 +6,7 @@ using Intercom.Core; using Intercom.Data; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; using RestSharp.Authenticators; @@ -13,15 +14,22 @@ namespace Intercom.Clients { public class EventsClient : Client { - private const String COMPANIES_RESOURCE = "events"; + private const String EVENTS_RESOURCE = "events"; - public EventsClient (Authentication authentication) - : base (INTERCOM_API_BASE_URL, COMPANIES_RESOURCE, authentication) + public EventsClient (RestClientFactory restClientFactory) + : base (EVENTS_RESOURCE, restClientFactory) { } + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use EventsClient(RestClientFactory restClientFactory)")] + public EventsClient(Authentication authentication) + : base(INTERCOM_API_BASE_URL, EVENTS_RESOURCE, authentication) + { + } + + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use EventsClient(RestClientFactory restClientFactory)")] public EventsClient(String intercomApiUrl, Authentication authentication) - : base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, COMPANIES_RESOURCE, authentication) + : base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, EVENTS_RESOURCE, authentication) { } diff --git a/src/Intercom/Clients/NotesClient.cs b/src/Intercom/Clients/NotesClient.cs index cde9f61..793ddf3 100644 --- a/src/Intercom/Clients/NotesClient.cs +++ b/src/Intercom/Clients/NotesClient.cs @@ -7,6 +7,7 @@ using Intercom.Core; using Intercom.Data; using Intercom.Exceptions; +using Intercom.Factories; using Newtonsoft.Json; using RestSharp; using RestSharp.Authenticators; @@ -17,11 +18,18 @@ public class NotesClient : Client { private const String NOTES_RESOURCE = "notes"; + public NotesClient(RestClientFactory restClientFactory) + : base(NOTES_RESOURCE, restClientFactory) + { + } + + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use NotesClient(RestClientFactory restClientFactory)")] public NotesClient(Authentication authentication) : base(INTERCOM_API_BASE_URL, NOTES_RESOURCE, authentication) { } + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use NotesClient(RestClientFactory restClientFactory)")] public NotesClient(String intercomApiUrl, Authentication authentication) : base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, NOTES_RESOURCE, authentication) { diff --git a/src/Intercom/Clients/SegmentsClient.cs b/src/Intercom/Clients/SegmentsClient.cs index 09e292d..fd84336 100644 --- a/src/Intercom/Clients/SegmentsClient.cs +++ b/src/Intercom/Clients/SegmentsClient.cs @@ -6,6 +6,7 @@ using Intercom.Core; using Intercom.Data; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; namespace Intercom.Clients @@ -14,11 +15,18 @@ public class SegmentsClient: Client { private const String SEGMENTS_RESOURCE = "segments"; + public SegmentsClient(RestClientFactory restClientFactory) + : base(SEGMENTS_RESOURCE, restClientFactory) + { + } + + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use SegmentsClient(RestClientFactory restClientFactory)")] public SegmentsClient(Authentication authentication) : base(INTERCOM_API_BASE_URL, SEGMENTS_RESOURCE, authentication) { } + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use SegmentsClient(RestClientFactory restClientFactory)")] public SegmentsClient(String intercomApiUrl, Authentication authentication) : base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, SEGMENTS_RESOURCE, authentication) { diff --git a/src/Intercom/Clients/TagsClient.cs b/src/Intercom/Clients/TagsClient.cs index c81ca59..68a27e8 100644 --- a/src/Intercom/Clients/TagsClient.cs +++ b/src/Intercom/Clients/TagsClient.cs @@ -6,6 +6,7 @@ using Intercom.Core; using Intercom.Data; using Intercom.Exceptions; +using Intercom.Factories; using Newtonsoft.Json; using RestSharp; using RestSharp.Authenticators; @@ -23,11 +24,18 @@ public enum EntityType private const String TAGS_RESOURCE = "tags"; + public TagsClient(RestClientFactory restClientFactory) + : base(TAGS_RESOURCE, restClientFactory) + { + } + + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use TagsClient(RestClientFactory restClientFactory)")] public TagsClient(Authentication authentication) : base(INTERCOM_API_BASE_URL, TAGS_RESOURCE, authentication) { } + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use TagsClient(RestClientFactory restClientFactory)")] public TagsClient(String intercomApiUrl, Authentication authentication) : base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, TAGS_RESOURCE, authentication) { diff --git a/src/Intercom/Clients/UserConversationsClient.cs b/src/Intercom/Clients/UserConversationsClient.cs index 743914a..724a6fc 100644 --- a/src/Intercom/Clients/UserConversationsClient.cs +++ b/src/Intercom/Clients/UserConversationsClient.cs @@ -7,6 +7,7 @@ using Intercom.Core; using Intercom.Data; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; using RestSharp.Authenticators; @@ -18,11 +19,18 @@ public class UserConversationsClient: Client private const String MESSAGES_RESOURCE = "messages"; private const String REPLY_RESOURCE = "reply"; + public UserConversationsClient(RestClientFactory restClientFactory) + : base(CONVERSATIONS_RESOURCE, restClientFactory) + { + } + + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use UserConversationsClient(RestClientFactory restClientFactory)")] public UserConversationsClient(Authentication authentication) : base(INTERCOM_API_BASE_URL, CONVERSATIONS_RESOURCE, authentication) { } + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use UserConversationsClient(RestClientFactory restClientFactory)")] public UserConversationsClient(String intercomApiUrl, Authentication authentication) : base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, CONVERSATIONS_RESOURCE, authentication) { diff --git a/src/Intercom/Clients/UsersClient.cs b/src/Intercom/Clients/UsersClient.cs index 6938a8f..d2a1d23 100644 --- a/src/Intercom/Clients/UsersClient.cs +++ b/src/Intercom/Clients/UsersClient.cs @@ -7,6 +7,7 @@ using Intercom.Core; using Intercom.Data; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; using RestSharp.Authenticators; using Newtonsoft.Json; @@ -26,11 +27,18 @@ private static class UserSortBy private const String USERS_RESOURCE = "users"; private const String PERMANENT_DELETE_RESOURCE = "user_delete_requests"; + public UsersClient(RestClientFactory restClientFactory) + : base(USERS_RESOURCE, restClientFactory) + { + } + + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use UsersClient(RestClientFactory restClientFactory)")] public UsersClient(Authentication authentication) : base(INTERCOM_API_BASE_URL, USERS_RESOURCE, authentication) { } + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use UsersClient(RestClientFactory restClientFactory)")] public UsersClient(String intercomApiUrl, Authentication authentication) : base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, USERS_RESOURCE, authentication) { @@ -292,7 +300,7 @@ public User UpdateLastSeenAt(User user, long timestamp) else if (!String.IsNullOrEmpty(user.email)) body = JsonConvert.SerializeObject(new { email = user.email, last_request_at = timestamp }); else - throw new ArgumentException("you need to provide either 'user.id', 'user.user_id', 'user.email' to update a user's last seet at."); + throw new ArgumentException("you need to provide either 'user.id', 'user.user_id', 'user.email' to update a user's last seen at."); ClientResponse result = null; result = Post(body); @@ -328,7 +336,7 @@ public User UpdateLastSeenAt(User user) else if (!String.IsNullOrEmpty(user.email)) body = JsonConvert.SerializeObject(new { email = user.email, update_last_request_at = true }); else - throw new ArgumentException("you need to provide either 'user.id', 'user.user_id', 'user.email' to update a user's last seet at."); + throw new ArgumentException("you need to provide either 'user.id', 'user.user_id', 'user.email' to update a user's last seen at."); ClientResponse result = null; result = Post(body); diff --git a/src/Intercom/Clients/VisitorsClient.cs b/src/Intercom/Clients/VisitorsClient.cs index d589c3e..c9aa526 100644 --- a/src/Intercom/Clients/VisitorsClient.cs +++ b/src/Intercom/Clients/VisitorsClient.cs @@ -7,6 +7,7 @@ using Intercom.Core; using Intercom.Data; using Intercom.Exceptions; +using Intercom.Factories; using RestSharp; using RestSharp.Authenticators; using Newtonsoft.Json; @@ -18,11 +19,18 @@ public class VisitorsClient : Client private const String VISITORS_RESOURCE = "visitors"; private const String VISITORS_CONVERT = "convert"; + public VisitorsClient(RestClientFactory restClientFactory) + : base(VISITORS_RESOURCE, restClientFactory) + { + } + + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use VisitorsClient(RestClientFactory restClientFactory)")] public VisitorsClient(Authentication authentication) : base(INTERCOM_API_BASE_URL, VISITORS_RESOURCE, authentication) { } + [Obsolete("This constructor is deprecated as of 3.0.0 and will soon be removed, please use VisitorsClient(RestClientFactory restClientFactory)")] public VisitorsClient(String intercomApiUrl, Authentication authentication) : base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, VISITORS_RESOURCE, authentication) { diff --git a/src/Intercom/Core/Client.cs b/src/Intercom/Core/Client.cs index 4cc44a3..c714126 100644 --- a/src/Intercom/Core/Client.cs +++ b/src/Intercom/Core/Client.cs @@ -7,6 +7,7 @@ using Intercom.Core; using Intercom.Data; using Intercom.Exceptions; +using Intercom.Factories; using Newtonsoft; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -27,24 +28,34 @@ public class Client protected const String USER_AGENT_HEADER = "User-Agent"; protected const String USER_AGENT_VALUE = "intercom-dotnet/2.0.0"; - protected readonly String URL; protected readonly String RESRC; - protected readonly Authentication AUTH; + protected readonly RestClientFactory _restClientFactory; - public Client(String url, String resource, Authentication authentication) + public Client(String resource, RestClientFactory restClientFactory) + { + if (restClientFactory == null) + throw new ArgumentNullException(nameof(restClientFactory)); + + if (String.IsNullOrEmpty(resource)) + throw new ArgumentNullException(nameof(resource)); + + RESRC = resource; + _restClientFactory = restClientFactory; + } + + public Client(String intercomApiUrl, String resource, Authentication authentication) { if (authentication == null) throw new ArgumentNullException(nameof(authentication)); - - if (String.IsNullOrEmpty(url)) - throw new ArgumentNullException(nameof(url)); if (String.IsNullOrEmpty(resource)) throw new ArgumentNullException(nameof(resource)); - this.URL = url; - this.RESRC = resource; - this.AUTH = authentication; + if (String.IsNullOrEmpty(intercomApiUrl)) + throw new ArgumentNullException(nameof(intercomApiUrl)); + + RESRC = resource; + _restClientFactory = new RestClientFactory(authentication, intercomApiUrl); } protected virtual ClientResponse Get( @@ -55,9 +66,10 @@ protected virtual ClientResponse Get( { ClientResponse clientResponse = null; + IRestClient client = null; try { - IRestClient client = BuildClient(); + client = BuildClient(); IRestRequest request = BuildRequest(httpMethod: Method.GET, headers: headers, parameters: parameters, resource: resource); IRestResponse response = client.Execute(request); clientResponse = HandleResponse(response); @@ -74,7 +86,7 @@ protected virtual ClientResponse Get( { throw new IntercomException(String.Format("An exception occurred " + "while calling the endpoint. Method: {0}, Url: {1}, Resource: {2}, Sub-Resource: {3}", - "GET", URL, RESRC, resource), ex); + "GET", client.BaseUrl, RESRC, resource), ex); } return clientResponse; @@ -93,9 +105,10 @@ protected virtual ClientResponse Post(String body, ClientResponse clientResponse = null; + IRestClient client = null; try { - IRestClient client = BuildClient(); + client = BuildClient(); IRestRequest request = BuildRequest(httpMethod: Method.POST, headers: headers, parameters: parameters, body: body, resource: resource); IRestResponse response = client.Execute(request); clientResponse = HandleResponse (response); @@ -112,7 +125,7 @@ protected virtual ClientResponse Post(String body, { throw new IntercomException(String.Format("An exception occurred " + "while calling the endpoint. Method: {0}, Url: {1}, Resource: {2}, Sub-Resource: {3}, Body: {4}", - "POST", URL, RESRC, resource, body), ex); + "POST", client.BaseUrl, RESRC, resource, body), ex); } return clientResponse; @@ -131,10 +144,11 @@ protected virtual ClientResponse Post(T body, ClientResponse clientResponse = null; + IRestClient client = null; try { String requestBody = Serialize(body); - IRestClient client = BuildClient(); + client = BuildClient(); IRestRequest request = BuildRequest(httpMethod: Method.POST, headers: headers, parameters: parameters, body: requestBody, resource: resource); IRestResponse response = client.Execute(request); clientResponse = HandleResponse (response); @@ -151,7 +165,7 @@ protected virtual ClientResponse Post(T body, { throw new IntercomException(String.Format("An exception occurred " + "while calling the endpoint. Method: {0}, Url: {1}, Resource: {2}, Sub-Resource: {3}, Body-Type: {4}", - "POST", URL, RESRC, resource, typeof(T)), ex); + "POST", client.BaseUrl, RESRC, resource, typeof(T)), ex); } return clientResponse; @@ -170,9 +184,10 @@ protected virtual ClientResponse Put(String body, ClientResponse clientResponse = null; + IRestClient client = null; try { - IRestClient client = BuildClient(); + client = BuildClient(); IRestRequest request = BuildRequest(httpMethod: Method.PUT, headers: headers, parameters: parameters, body: body, resource: resource); IRestResponse response = client.Execute(request); clientResponse = HandleResponse (response); @@ -189,7 +204,7 @@ protected virtual ClientResponse Put(String body, { throw new IntercomException(String.Format("An exception occurred " + "while calling the endpoint. Method: {0}, Url: {1}, Resource: {2}, Sub-Resource: {3}, Body: {4}", - "POST", URL, RESRC, resource, body), ex); + "POST", client.BaseUrl, RESRC, resource, body), ex); } return clientResponse; @@ -208,10 +223,11 @@ protected virtual ClientResponse Put(T body, ClientResponse clientResponse = null; + IRestClient client = null; try { String requestBody = Serialize(body); - IRestClient client = BuildClient(); + client = BuildClient(); IRestRequest request = BuildRequest(httpMethod: Method.PUT, headers: headers, parameters: parameters, body: requestBody, resource: resource); IRestResponse response = client.Execute(request); clientResponse = HandleResponse (response); @@ -228,7 +244,7 @@ protected virtual ClientResponse Put(T body, { throw new IntercomException(String.Format("An exception occurred " + "while calling the endpoint. Method: {0}, Url: {1}, Resource: {2}, Sub-Resource: {3}", - "POST", URL, RESRC, resource), ex); + "POST", client.BaseUrl, RESRC, resource), ex); } return clientResponse; @@ -242,9 +258,10 @@ protected virtual ClientResponse Delete( { ClientResponse clientResponse = null; + IRestClient client = null; try { - IRestClient client = BuildClient(); + client = BuildClient(); IRestRequest request = BuildRequest(httpMethod: Method.DELETE, headers: headers, parameters: parameters, resource: resource); IRestResponse response = client.Execute(request); clientResponse = HandleResponse(response); @@ -261,7 +278,7 @@ protected virtual ClientResponse Delete( { throw new IntercomException(String.Format("An exception occurred " + "while calling the endpoint. Method: {0}, Url: {1}, Resource: {2}, Sub-Resource: {3}", - "POST", URL, RESRC, resource), ex); + "POST", client.BaseUrl, RESRC, resource), ex); } return clientResponse; @@ -295,14 +312,7 @@ protected virtual IRestRequest BuildRequest(Method httpMethod = Method.GET, protected virtual IRestClient BuildClient() { - RestClient client = new RestClient(URL); - - if (!String.IsNullOrEmpty (AUTH.AppId) && !String.IsNullOrEmpty (AUTH.AppKey)) - client.Authenticator = new HttpBasicAuthenticator (AUTH.AppId, AUTH.AppKey); - else - client.Authenticator = new HttpBasicAuthenticator (AUTH.PersonalAccessToken, String.Empty); - - return client; + return _restClientFactory.RestClient; } protected virtual void AddHeaders(IRestRequest request, diff --git a/src/Intercom/Factories/RestClientFactory.cs b/src/Intercom/Factories/RestClientFactory.cs new file mode 100644 index 0000000..fc22e61 --- /dev/null +++ b/src/Intercom/Factories/RestClientFactory.cs @@ -0,0 +1,58 @@ +using System; +using RestSharp; +using RestSharp.Authenticators; +using Intercom.Core; + +namespace Intercom.Factories +{ + public class RestClientFactory + { + + private const String INTERCOM_API_BASE_URL = "https://api.intercom.io/"; + private readonly Authentication _authentication; + private readonly string _url; + private IRestClient _restClient; + + private readonly object padlock = new object(); + + public RestClientFactory(Authentication authentication) + { + _authentication = authentication; + _url = INTERCOM_API_BASE_URL; + } + + public RestClientFactory(Authentication authentication, string url) + { + _authentication = authentication; + _url = url; + } + + public virtual IRestClient RestClient + { + get + { + lock(padlock) + { + if (_restClient != null) + { + return _restClient; + } + ConstructClient(); + return _restClient; + } + } + } + + private void ConstructClient() + { + RestClient client = new RestClient(_url); + + if (!String.IsNullOrEmpty (_authentication.AppId) && !String.IsNullOrEmpty (_authentication.AppKey)) + client.Authenticator = new HttpBasicAuthenticator (_authentication.AppId, _authentication.AppKey); + else + client.Authenticator = new HttpBasicAuthenticator (_authentication.PersonalAccessToken, String.Empty); + + _restClient = client; + } + } +} \ No newline at end of file diff --git a/src/Intercom/Intercom.csproj b/src/Intercom/Intercom.csproj index af547b0..4ce83a7 100644 --- a/src/Intercom/Intercom.csproj +++ b/src/Intercom/Intercom.csproj @@ -2,7 +2,7 @@ netstandard2.0 - 2.0.0 + 3.0.0 Intercom.Dotnet.Client https://raw.githubusercontent.com/intercom/intercom-dotnet/master/src/assets/Intercom.png Intercom diff --git a/src/Intercom/intercom.nuspec b/src/Intercom/intercom.nuspec index 7538728..88ec797 100644 --- a/src/Intercom/intercom.nuspec +++ b/src/Intercom/intercom.nuspec @@ -3,7 +3,7 @@ Intercom.Dotnet.Client Intercom Dotnet Client - 2.0.0 + 3.0.0 Intercom Intercom false