Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ic added rest client factory #145

Merged
merged 6 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is key to demonstrate the power of the new pattern

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
10 changes: 3 additions & 7 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,13 +18,8 @@ public class AdminConversationsClient: Client
private const String MESSAGES_RESOURCE = "messages";
private const String REPLY_RESOURCE = "reply";

public AdminConversationsClient(Authentication authentication)
: base(INTERCOM_API_BASE_URL, CONVERSATIONS_RESOURCE, authentication)
{
}

public AdminConversationsClient(String intercomApiUrl, Authentication authentication)
: base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, CONVERSATIONS_RESOURCE, authentication)
public AdminConversationsClient(RestClientFactory restClientFactory)
: base(CONVERSATIONS_RESOURCE, restClientFactory)
{
}

Expand Down
10 changes: 3 additions & 7 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,13 +16,8 @@ public class AdminsClient : Client
{
private const String ADMINS_RESOURCE = "admins";

public AdminsClient (Authentication authentication)
: base (INTERCOM_API_BASE_URL, ADMINS_RESOURCE, authentication)
{
}

public AdminsClient(String intercomApiUrl, Authentication authentication)
: base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, ADMINS_RESOURCE, authentication)
public AdminsClient (RestClientFactory restClientFactory)
: base (ADMINS_RESOURCE, restClientFactory)
{
}

Expand Down
10 changes: 3 additions & 7 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,13 +14,8 @@ public class CompanyClient : Client
{
private const String COMPANIES_RESOURCE = "companies";

public CompanyClient(Authentication authentication)
: base(INTERCOM_API_BASE_URL, COMPANIES_RESOURCE, authentication)
{
}

public CompanyClient(String intercomApiUrl, Authentication authentication)
: base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, COMPANIES_RESOURCE, authentication)
public CompanyClient(RestClientFactory restClientFactory)
: base(COMPANIES_RESOURCE, restClientFactory)
{
}

Expand Down
10 changes: 3 additions & 7 deletions src/Intercom/Clients/ContactsClient.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 @@ -19,13 +20,8 @@ public static class ContactSortBy

private const String CONTACTS_RESOURCE = "contacts";

public ContactsClient (Authentication authentication)
: base (INTERCOM_API_BASE_URL, CONTACTS_RESOURCE, authentication)
{
}

public ContactsClient(String intercomApiUrl, Authentication authentication)
: base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, CONTACTS_RESOURCE, authentication)
public ContactsClient (RestClientFactory restClientFactory)
: base (CONTACTS_RESOURCE, restClientFactory)
{
}

Expand Down
10 changes: 3 additions & 7 deletions src/Intercom/Clients/ConversationsClient.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 @@ -24,13 +25,8 @@ public static class MessageType

private const String CONVERSATIONS_RESOURCE = "conversations";

public ConversationsClient( Authentication authentication)
: base(INTERCOM_API_BASE_URL, CONVERSATIONS_RESOURCE, authentication)
{
}

public ConversationsClient(String intercomApiUrl, Authentication authentication)
: base(String.IsNullOrEmpty(intercomApiUrl) ? INTERCOM_API_BASE_URL : intercomApiUrl, CONVERSATIONS_RESOURCE, authentication)
public ConversationsClient( RestClientFactory restClientFactory)
: base(CONVERSATIONS_RESOURCE, restClientFactory)
{
}

Expand Down
Loading