Skip to content

Commit

Permalink
fix: create/edit file
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidRouyer committed May 18, 2020
1 parent fe4c4f8 commit c37f291
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 26 deletions.
55 changes: 43 additions & 12 deletions src/Pipedrive.net.Tests.Integration/Clients/FilesClientTests.cs
@@ -1,4 +1,6 @@
using System.Threading.Tasks;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Xunit;

namespace Pipedrive.Tests.Integration.Clients
Expand Down Expand Up @@ -64,18 +66,21 @@ public async Task ReturnsDistinctInfosBasedOnStartPage()
}
}

/*public class TheCreateMethod
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 newFile = new NewFile(new RawFile(
"image.jpg",
ReadFile(GetFileFromPath(@"./Fixtures/image.jpg")),
"image/jpg"))
{
DealId = 1
};

var file = await fixture.Create(newFile);
Assert.NotNull(file);
Expand All @@ -93,8 +98,14 @@ 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 newFile = new NewFile(new RawFile(
"image.jpg",
ReadFile(GetFileFromPath(@"./Fixtures/image.jpg")),
"image/jpg"))
{
DealId = 1
};

var file = await fixture.Create(newFile);

var editFile = file.ToUpdate();
Expand All @@ -116,8 +127,14 @@ 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 newFile = new NewFile(new RawFile(
"image.jpg",
ReadFile(GetFileFromPath(@"./Fixtures/image.jpg")),
"image/jpg"))
{
DealId = 1
};

var file = await fixture.Create(newFile);

var createdFile = await fixture.Get(file.Id);
Expand All @@ -126,10 +143,24 @@ public async Task CanDelete()

await fixture.Delete(createdFile.Id);

var deletedFile = await fixture.Get(createdFile.Id);
var deletedFile = await fixture.Get(createdFile.Id);

Assert.False(deletedFile.ActiveFlag);
}
}*/
}

private static FileStream GetFileFromPath(string filePath)
{
return new FileStream(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), filePath), FileMode.Open);
}

private static byte[] ReadFile(Stream fileStream)
{
using (var ms = new MemoryStream())
{
fileStream.CopyTo(ms);
return ms.ToArray();
}
}
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<None Remove="Fixtures\image.jpg" />
<None Remove="Fixtures\webhook_activity_update.json" />
<None Remove="Fixtures\webhook_deal_create.json" />
<None Remove="Fixtures\webhook_deal_update.json" />
Expand All @@ -16,6 +17,9 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Fixtures\image.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Fixtures\webhook_activity_update.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
Expand Down
12 changes: 8 additions & 4 deletions src/Pipedrive.net.Tests/Clients/FilesClientTests.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using NSubstitute;
using Xunit;
Expand Down Expand Up @@ -71,7 +72,7 @@ public async Task RequestsCorrectUrl()
}
}

/*public class TheCreateMethod
public class TheCreateMethod
{
[Fact]
public async Task EnsuresNonNullArguments()
Expand All @@ -87,13 +88,16 @@ public async Task PostsToTheCorrectUrl()
var connection = Substitute.For<IApiConnection>();
var client = new FilesClient(connection);

var newFile = new NewFile(new MemoryStream());
var newFile = new NewFile(new RawFile("abc.pdf", new byte[] { }, "application/pdf"));
await client.Create(newFile);

await connection.Received().Post<File>(Arg.Is<Uri>(u => u.ToString() == "files"),
Arg.Is<NewFile>(nc => nc.File == new MemoryStream()));
Arg.Is<MultipartFormDataContent>(
nc => nc is MultipartFormDataContent),
Arg.Is<string>(a => a == "application/json"),
Arg.Is<string>(c => c == "multipart/form-data"));
}
}*/
}

public class TheEditMethod
{
Expand Down
17 changes: 13 additions & 4 deletions src/Pipedrive.net/Clients/FilesClient.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Pipedrive.Helpers;

Expand Down Expand Up @@ -39,41 +40,49 @@ public Task<File> Get(long id)
return ApiConnection.Get<File>(ApiUrls.File(id));
}

/*public async Task<File> Create(NewFile data)
public async Task<File> Create(NewFile data)
{
Ensure.ArgumentNotNull(data, nameof(data));

var content = new MultipartFormDataContent();
content.Add(new StreamContent(data.File), "\"file\"");

var byteArrayContent = new ByteArrayContent(data.File.Content);
byteArrayContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(data.File.ContentType);

content.Add(byteArrayContent, "file", data.File.Name);

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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Pipedrive.net/Clients/IFilesClient.cs
Expand Up @@ -14,7 +14,7 @@ public interface IFilesClient

Task<File> Get(long id);

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

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

Expand Down
18 changes: 18 additions & 0 deletions src/Pipedrive.net/Models/Common/RawFile.cs
@@ -0,0 +1,18 @@
namespace Pipedrive
{
public class RawFile
{
public string Name { get; set; }

public byte[] Content { get; set; }

public string ContentType { get; set; }

public RawFile(string name, byte[] content, string contentType)
{
this.Name = name;
this.Content = content;
this.ContentType = contentType;
}
}
}
6 changes: 5 additions & 1 deletion src/Pipedrive.net/Models/Request/FileUpdate.cs
@@ -1,9 +1,13 @@
namespace Pipedrive
using Newtonsoft.Json;

namespace Pipedrive
{
public class FileUpdate
{
[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("description")]
public string Description { get; set; }
}
}
7 changes: 3 additions & 4 deletions src/Pipedrive.net/Models/Request/NewFile.cs
@@ -1,12 +1,11 @@
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json;

namespace Pipedrive
{
public class NewFile
{
[JsonProperty("file")]
public Stream File { get; set; }
public RawFile File { get; set; }

[JsonProperty("deal_id")]
public long? DealId { get; set; }
Expand All @@ -26,7 +25,7 @@ public class NewFile
[JsonProperty("note_id")]
public long? NoteId { get; set; }

public NewFile(Stream file)
public NewFile(RawFile file)
{
this.File = file;
}
Expand Down

0 comments on commit c37f291

Please sign in to comment.