Skip to content

Commit

Permalink
Ic added rest client factory (#145)
Browse files Browse the repository at this point in the history
* Added Rest Client Factory

* Added tests and version bump

* Made backwards compatible

* Version change

* Made factory thread safe

* Version bump and doc update
  • Loading branch information
ianintercom authored and choran committed Nov 8, 2018
1 parent f84a9a5 commit d0e6eba
Show file tree
Hide file tree
Showing 30 changed files with 300 additions and 57 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<ReleaseVersion>2.0.0</ReleaseVersion>
<ReleaseVersion>3.0.0</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 4 additions & 1 deletion src/Intercom.Tests/Clients/AdminClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()]
Expand Down
5 changes: 4 additions & 1 deletion src/Intercom.Tests/Clients/AdminConversationsClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()]
Expand Down
5 changes: 4 additions & 1 deletion src/Intercom.Tests/Clients/CompanyClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()]
Expand Down
5 changes: 4 additions & 1 deletion src/Intercom.Tests/Clients/ContactClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()]
Expand Down
5 changes: 4 additions & 1 deletion src/Intercom.Tests/Clients/ConversationsClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()]
Expand Down
5 changes: 4 additions & 1 deletion src/Intercom.Tests/Clients/EventsClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()]
Expand Down
5 changes: 4 additions & 1 deletion src/Intercom.Tests/Clients/NotesClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()]
Expand Down
5 changes: 4 additions & 1 deletion src/Intercom.Tests/Clients/TagsClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Intercom.Core;
using Intercom.Data;
using Intercom.Exceptions;
using Intercom.Factories;
using Moq;
using Newtonsoft.Json;
using NUnit.Framework;
Expand All @@ -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()]
Expand Down
38 changes: 36 additions & 2 deletions src/Intercom.Tests/Clients/UserClientTest.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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>();
restClient = restClientMock.Object;
}
var restClientFactoryMock = new Mock<RestClientFactory>(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<ArgumentNullException>(() => usersClient.Create(null));
}

[Test()]
public void Create_NoUserIdOrEmail_ThrowException()
{
SetupMock();
Assert.Throws<ArgumentException>(() => usersClient.Create(new User()));
}

[Test()]
public void Archive_NoIdOrUserIdOrEmail_ThrowException()
{
SetupMock();
Assert.Throws<ArgumentException>(() => usersClient.Archive(new User()));
}

[Test()]
public void PermanentlyDeleteUser_NoId_ThrowException()
{
SetupMock();
Assert.Throws<ArgumentNullException>(() => usersClient.PermanentlyDeleteUser(null));
}

[Test()]
public void Update_NoIdOrUserIdOrEmail_ThrowException()
{
SetupMock();
Assert.Throws<ArgumentException>(() => usersClient.Update(new User()));
}

[Test()]
public void View_ByStringId_ReturnsObjectAsExpected()
{
var userId = "id";
var restClientMock = new Mock<RestClient>();
var restResponse = new RestResponse()
{
StatusCode = HttpStatusCode.OK,
Content = $"{{ \"type\": \"user\", \"id\": \"530370b477ad7120001d\", \"user_id\": \"{userId}\", \"email\": \"[email protected]\" }}",
};
restClientMock.Setup(x => x.Execute(It.IsAny<IRestRequest>())).Returns(restResponse);
var restClient = restClientMock.Object;
SetupMock(restClient);
Assert.AreEqual(userId, usersClient.View(userId).user_id);
}
}
}
5 changes: 4 additions & 1 deletion src/Intercom.Tests/Clients/UserConversationsClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()]
Expand Down
2 changes: 1 addition & 1 deletion src/Intercom.Tests/Intercom.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<ReleaseVersion>2.0.0</ReleaseVersion>
<ReleaseVersion>3.0.0</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions src/Intercom/Clients/AdminConversationsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Intercom.Core;
using Intercom.Data;
using Intercom.Exceptions;
using Intercom.Factories;
using RestSharp;
using RestSharp.Authenticators;

Expand All @@ -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)
{
Expand Down
12 changes: 10 additions & 2 deletions src/Intercom/Clients/AdminsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Intercom.Core;
using Intercom.Data;
using Intercom.Exceptions;
using Intercom.Factories;
using RestSharp;
using RestSharp.Authenticators;

Expand All @@ -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)
{
Expand Down
8 changes: 8 additions & 0 deletions src/Intercom/Clients/CompanyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using Intercom.Core;
using Intercom.Data;
using Intercom.Factories;
using Newtonsoft.Json;

namespace Intercom.Clients
Expand All @@ -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)
{
Expand Down
Loading

0 comments on commit d0e6eba

Please sign in to comment.