Skip to content

Commit

Permalink
feat: GET /filters & GET /filters/{id}
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidRouyer committed Jun 30, 2021
1 parent 9a63371 commit 82d7cc0
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 2 deletions.
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ You can debug this library right from your application by configuring the [NuGet
- [x] deleteFile

- [ ] Filters
- [ ] getFilters
- [x] getFilters
- [ ] getFilterHelpers
- [ ] getFilter
- [x] getFilter
- [ ] addFilter
- [ ] updateFilter
- [ ] deleteFilters
Expand Down
37 changes: 37 additions & 0 deletions src/Pipedrive.net.Tests.Integration/Clients/FiltersClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Threading.Tasks;
using Xunit;

namespace Pipedrive.Tests.Integration.Clients
{
public class FiltersClientTests
{
public class TheGetAllMethod
{
[IntegrationTest]
public async Task CanRetrieveFilters()
{
var pipedrive = Helper.GetAuthenticatedClient();

var filters = await pipedrive.Filter.GetAll(FilterFilters.None);

Assert.True(filters.Count >= 1);
Assert.Equal("All open deals", filters[0].Name);
Assert.True(filters[0].ActiveFlag);
}
}

public class TheGetMethod
{
[IntegrationTest]
public async Task CanRetrieveFilter()
{
var pipedrive = Helper.GetAuthenticatedClient();

var filter = await pipedrive.Filter.Get(1);

Assert.True(filter.ActiveFlag);
Assert.Equal("All open deals", filter.Name);
}
}
}
}
55 changes: 55 additions & 0 deletions src/Pipedrive.net.Tests/Clients/FiltersClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NSubstitute;
using Xunit;

namespace Pipedrive.Tests.Clients
{
public class FiltersClientTests
{
public class TheCtor
{
[Fact]
public void EnsuresNonNullArguments()
{
Assert.Throws<ArgumentNullException>(() => new FiltersClient(null));
}
}

public class TheGetAllMethod
{
[Fact]
public async Task RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new FiltersClient(connection);

await client.GetAll(FilterFilters.None);

Received.InOrder(async () =>
{
await connection.GetAll<Filter>(Arg.Is<Uri>(u => u.ToString() == "filters"),
Arg.Is<Dictionary<string, string>>(d => d.Count == 0));
});
}
}

public class TheGetMethod
{
[Fact]
public async Task RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new FiltersClient(connection);

await client.Get(123);

Received.InOrder(async () =>
{
await connection.Get<Filter>(Arg.Is<Uri>(u => u.ToString() == "filters/123"));
});
}
}
}
}
36 changes: 36 additions & 0 deletions src/Pipedrive.net/Clients/FiltersClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Pipedrive.Helpers;

namespace Pipedrive
{
/// <summary>
/// A client for Pipedrive's Filter API.
/// </summary>
/// <remarks>
/// See the <a href="https://developers.pipedrive.com/docs/api/v1/Filters">Filter API documentation</a> for more information.
public class FiltersClient : ApiClient, IFiltersClient
{
/// <summary>
/// Initializes a new instance of the <see cref="FiltersClient"/> class.
/// </summary>
/// <param name="apiConnection">An API connection</param>
public FiltersClient(IApiConnection apiConnection) : base(apiConnection)
{
}

public Task<IReadOnlyList<Filter>> GetAll(FilterFilters filters)
{
Ensure.ArgumentNotNull(filters, nameof(filters));

var parameters = filters.Parameters;

return ApiConnection.GetAll<Filter>(ApiUrls.Filters(), parameters);
}

public Task<Filter> Get(long id)
{
return ApiConnection.Get<Filter>(ApiUrls.Filter(id));
}
}
}
17 changes: 17 additions & 0 deletions src/Pipedrive.net/Clients/IFiltersClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Pipedrive
{
/// <summary>
/// A client for Pipedrive's Filter API.
/// </summary>
/// <remarks>
/// See the <a href="https://developers.pipedrive.com/docs/api/v1/Filters">Filter API documentation</a> for more information.
public interface IFiltersClient
{
Task<IReadOnlyList<Filter>> GetAll(FilterFilters filters);

Task<Filter> Get(long id);
}
}
19 changes: 19 additions & 0 deletions src/Pipedrive.net/Helpers/ApiUrls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public static class ApiUrls

static readonly Uri _filesUrl = new Uri("files", UriKind.Relative);

static readonly Uri _filtersUrl = new Uri("filters", UriKind.Relative);

static readonly Uri _leadsUrl = new Uri("leads", UriKind.Relative);

static readonly Uri _leadLabelsUrl = new Uri("leadLabels", UriKind.Relative);
Expand Down Expand Up @@ -294,6 +296,23 @@ public static Uri File(long id)
return new Uri($"{_filesUrl}/{id}", UriKind.Relative);
}

/// <summary>
/// Returns the <see cref="Uri"/> that returns all of the filters in response to a GET request.
/// </summary>
public static Uri Filters()
{
return _filtersUrl;
}

/// <summary>
/// Returns the <see cref="Uri"/> for the specified filter.
/// </summary>
/// <param name="id">The id of the filter</param>
public static Uri Filter(long id)
{
return new Uri($"{_filtersUrl}/{id}", UriKind.Relative);
}

/// <summary>
/// Returns the <see cref="Uri"/> that returns all of the leads in response to a GET request.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Pipedrive.net/IPipedriveClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public interface IPipedriveClient

IFilesClient File { get; }

IFiltersClient Filter { get; }

ILeadsClient Lead { get; }

ILeadLabelsClient LeadLabel { get; }
Expand Down
11 changes: 11 additions & 0 deletions src/Pipedrive.net/Models/Common/FilterType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Pipedrive
{
public enum FilterType
{
deals,
org,
people,
products,
activity
}
}
31 changes: 31 additions & 0 deletions src/Pipedrive.net/Models/Request/Filters/FilterFilters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;

namespace Pipedrive
{
public class FilterFilters
{
public static FilterFilters None
{
get { return new FilterFilters(); }
}

public FilterType? Type { get; set; }

/// <summary>
/// Get the query parameters that will be appending onto the search
/// </summary>
public IDictionary<string, string> Parameters
{
get
{
var d = new Dictionary<string, string>();
if (Type.HasValue)
{
d.Add("type", Type.Value.ToString());
}

return d;
}
}
}
}
35 changes: 35 additions & 0 deletions src/Pipedrive.net/Models/Response/Filters/Filter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Newtonsoft.Json;

namespace Pipedrive
{
public class Filter
{
public long Id { get; set; }

public string Name { get; set; }

[JsonProperty("active_flag")]
public bool ActiveFlag { get; set; }

public string Type { get; set; }

[JsonProperty("temporary_flag")]
public string TemporaryFlag { get; set; }

[JsonProperty("user_id")]
public long UserId { get; set; }

[JsonProperty("add_time")]
public DateTime AddTime { get; set; }

[JsonProperty("update_time")]
public DateTime? UpdateTime { get; set; }

[JsonProperty("visible_to")]
public long VisibleTo { get; set; }

[JsonProperty("custom_view_id")]
public long? CustomViewId { get; set; }
}
}
9 changes: 9 additions & 0 deletions src/Pipedrive.net/PipedriveClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public PipedriveClient(IConnection connection)
Deal = new DealsClient(apiConnection);
DealField = new DealFieldsClient(apiConnection);
File = new FilesClient(apiConnection);
Filter = new FiltersClient(apiConnection);
Lead = new LeadsClient(apiConnection);
LeadLabel = new LeadLabelsClient(apiConnection);
LeadSource = new LeadSourcesClient(apiConnection);
Expand Down Expand Up @@ -190,6 +191,14 @@ public Uri BaseAddress
/// </remarks>
public IFilesClient File { get; private set; }

/// <summary>
/// Access Pipedrive's Filter API.
/// </summary>
/// <remarks>
/// Refer to the API documentation for more information: https://developers.pipedrive.com/docs/api/v1/Filters
/// </remarks>
public IFiltersClient Filter { get; private set; }

/// <summary>
/// Access Pipedrive's Lead API.
/// </summary>
Expand Down

0 comments on commit 82d7cc0

Please sign in to comment.