Skip to content

Commit

Permalink
feat: Files API [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidRouyer committed May 6, 2018
1 parent 6ed9d4a commit 5e8669d
Show file tree
Hide file tree
Showing 11 changed files with 506 additions and 0 deletions.
135 changes: 135 additions & 0 deletions src/Pipedrive.net.Tests.Integration/Clients/FilesClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using System.Threading.Tasks;
using Xunit;

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

var filters = new FileFilters
{
PageSize = 3,
PageCount = 1
};

var files = await pipedrive.File.GetAll(filters);
Assert.Equal(3, files.Count);
}

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

var filters = new FileFilters
{
PageSize = 2,
PageCount = 1,
StartPage = 1
};

var files = await pipedrive.File.GetAll(filters);
Assert.Equal(2, files.Count);
}

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

var startFilters = new FileFilters
{
PageSize = 1,
PageCount = 1
};

var firstPage = await pipedrive.File.GetAll(startFilters);

var skipStartFilters = new FileFilters
{
PageSize = 1,
PageCount = 1,
StartPage = 1
};

var secondPage = await pipedrive.File.GetAll(skipStartFilters);

Assert.NotEqual(firstPage[0].Id, secondPage[0].Id);
}
}

/*public class TheCreateMethod
{
[IntegrationTest]
public async Task CanCreate()
{
var pipedrive = Helper.GetAuthenticatedClient();
var fixture = pipedrive.File;
var imageUrl = @"./Content/image.jpg";
FileStream reader = new FileStream(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), imageUrl), FileMode.Open);
var newFile = new NewFile(reader);
var file = await fixture.Create(newFile);
Assert.NotNull(file);
var retrieved = await fixture.Get(file.Id);
Assert.NotNull(retrieved);
}
}
public class TheEditMethod
{
[IntegrationTest]
public async Task CanEdit()
{
var pipedrive = Helper.GetAuthenticatedClient();
var fixture = pipedrive.File;
byte[] data = System.Convert.FromBase64String("R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==");
var newFile = new NewFile(new MemoryStream(data));
var file = await fixture.Create(newFile);
var editFile = file.ToUpdate();
editFile.Name = "updated-name";
editFile.Description = "updated-description";
var updatedFile = await fixture.Edit(file.Id, editFile);
Assert.Equal("updated-name", updatedFile.Name);
Assert.Equal("updated-description", updatedFile.Description);
}
}
public class TheDeleteMethod
{
[IntegrationTest]
public async Task CanDelete()
{
var pipedrive = Helper.GetAuthenticatedClient();
var fixture = pipedrive.File;
byte[] data = System.Convert.FromBase64String("R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==");
var newFile = new NewFile(new MemoryStream(data));
var file = await fixture.Create(newFile);
var createdFile = await fixture.Get(file.Id);
Assert.NotNull(createdFile);
await fixture.Delete(createdFile.Id);
var deletedFile = await fixture.Get(createdFile.Id);
Assert.False(deletedFile.ActiveFlag);
}
}*/
}
}
139 changes: 139 additions & 0 deletions src/Pipedrive.net.Tests/Clients/FilesClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using NSubstitute;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Xunit;

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

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

await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll(null));
}

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

var filters = new FileFilters
{
PageSize = 1,
PageCount = 1,
StartPage = 0,
};

await client.GetAll(filters);

Received.InOrder(async () =>
{
await connection.GetAll<File>(
Arg.Is<Uri>(u => u.ToString() == "files"),
Arg.Is<Dictionary<string, string>>(d => d.Count == 0),
Arg.Is<ApiOptions>(o => o.PageSize == 1
&& o.PageCount == 1
&& o.StartPage == 0)
);
});
}
}

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

await client.Get(123);

Received.InOrder(async () =>
{
await connection.Get<File>(Arg.Is<Uri>(u => u.ToString() == "files/123"));
});
}
}

/*public class TheCreateMethod
{
[Fact]
public async Task EnsuresNonNullArguments()
{
var client = new FilesClient(Substitute.For<IApiConnection>());
await Assert.ThrowsAsync<ArgumentNullException>(() => client.Create(null));
}
[Fact]
public async Task PostsToTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new FilesClient(connection);
var newFile = new NewFile(new MemoryStream());
await client.Create(newFile);
await connection.Received().Post<File>(Arg.Is<Uri>(u => u.ToString() == "files"),
Arg.Is<NewFile>(nc => nc.File == new MemoryStream()));
}
}*/

public class TheEditMethod
{
[Fact]
public async Task EnsuresNonNullArguments()
{
var client = new FilesClient(Substitute.For<IApiConnection>());

await Assert.ThrowsAsync<ArgumentNullException>(() => client.Edit(1, null));
}

[Fact]
public void PutsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new FilesClient(connection);

var editFile = new FileUpdate { Name = "name", Description = "description" };
client.Edit(123, editFile);

connection.Received().Put<File>(Arg.Is<Uri>(u => u.ToString() == "files/123"),
Arg.Is<FileUpdate>(nc => nc.Name == "name"
&& nc.Description == "description"));
}
}

public class TheDeleteMethod
{
[Fact]
public void DeletesCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new FilesClient(connection);

client.Delete(123);

connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "files/123"));
}
}
}
}
91 changes: 91 additions & 0 deletions src/Pipedrive.net/Clients/FilesClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Pipedrive.Helpers;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace Pipedrive
{
/// <summary>
/// A client for Pipedrive's File API.
/// </summary>
/// <remarks>
/// See the <a href="https://developers.pipedrive.com/docs/api/v1/#!/Files">File API documentation</a> for more information.
public class FilesClient : ApiClient, IFilesClient
{
/// <summary>
/// Initializes a new File API client.
/// </summary>
/// <param name="apiConnection">An API connection</param>
public FilesClient(IApiConnection apiConnection) : base(apiConnection)
{
}

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

var parameters = filters.Parameters;
var options = new ApiOptions
{
StartPage = filters.StartPage,
PageCount = filters.PageCount,
PageSize = filters.PageSize
};

return ApiConnection.GetAll<File>(ApiUrls.Files(), parameters, options);
}

public Task<File> Get(long id)
{
return ApiConnection.Get<File>(ApiUrls.File(id));
}

/*public async Task<File> Create(NewFile data)
{
Ensure.ArgumentNotNull(data, nameof(data));
var content = new MultipartFormDataContent();
content.Add(new StreamContent(data.File), "\"file\"");
if (data.DealId.HasValue)
{
content.Add(new StringContent(data.DealId.ToString()), "deal_id");
}
if (data.PersonId.HasValue)
{
content.Add(new StringContent(data.PersonId.ToString()), "person_id");
}
if (data.OrgId.HasValue)
{
content.Add(new StringContent(data.OrgId.ToString()), "org_id");
}
if (data.ProductId.HasValue)
{
content.Add(new StringContent(data.ProductId.ToString()), "product_id");
}
if (data.ActivityId.HasValue)
{
content.Add(new StringContent(data.ActivityId.ToString()), "activity_id");
}
if (data.NoteId.HasValue)
{
content.Add(new StringContent(data.NoteId.ToString()), "note_id");
}
var contentString = content.ReadAsStringAsync();
return await ApiConnection.Post<File>(ApiUrls.Files(), content, "application/json", "multipart/form-data");
}*/

public Task<File> Edit(long id, FileUpdate data)
{
Ensure.ArgumentNotNull(data, nameof(data));

return ApiConnection.Put<File>(ApiUrls.File(id), data);
}

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

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

Task<File> Get(long id);

/*Task<File> Create(NewFile data);*/

Task<File> Edit(long id, FileUpdate data);

Task Delete(long id);
}
}
Loading

0 comments on commit 5e8669d

Please sign in to comment.