Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit d6e2423

Browse files
committed
As a workaround for not currently being able to add attachments as streams (see restsharp/RestSharp#70 ) - the attachment is added as a byte array.
1 parent 3842338 commit d6e2423

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/SymphonyOSS.RestApiClient/Api/AgentApi/AttachmentsApi.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
using System;
1819
using System.IO;
20+
using RestSharp;
21+
using RestSharp.Extensions;
1922

2023
namespace SymphonyOSS.RestApiClient.Api.AgentApi
2124
{
@@ -56,11 +59,14 @@ public AttachmentsApi(IAuthTokens authTokens, Configuration configuration, IApiE
5659
/// be used on a message in that stream.
5760
/// </summary>
5861
/// <param name="sid">Stream ID.</param>
59-
/// <param name="file">The file to upload as an attachment.</param>
62+
/// <param name="file">The path of the file to upload as an attachment.</param>
6063
/// <returns>Attachment info.</returns>
61-
public AttachmentInfo UploadAttachment(string sid, Stream file)
64+
public AttachmentInfo UploadAttachment(string sid, string file)
6265
{
63-
return _apiExecutor.Execute(_attachmentsApi.V1StreamSidAttachmentCreatePost, sid, _authTokens.SessionToken, _authTokens.KeyManagerToken, file);
66+
using (var stream = File.OpenRead(file))
67+
{
68+
return _apiExecutor.Execute(UploadAttachment, sid, Path.GetFileName(file), stream);
69+
}
6470
}
6571

6672
/// <summary>
@@ -74,5 +80,17 @@ public byte[] DownloadAttachment(string sid, string messageId, string fileId)
7480
{
7581
return _apiExecutor.Execute(_attachmentsApi.V1StreamSidAttachmentGet, sid, fileId, messageId, _authTokens.SessionToken, _authTokens.KeyManagerToken);
7682
}
83+
84+
private AttachmentInfo UploadAttachment(string sid, string filename, Stream file)
85+
{
86+
var request = new RestRequest("v1/stream/" + sid + "/attachment/create", Method.POST);
87+
request.AddHeader("sessionToken", _authTokens.SessionToken);
88+
request.AddHeader("keyManagerToken", _authTokens.KeyManagerToken);
89+
request.AddFile("file", file.ReadAsBytes(), filename, "application/octet-stream");
90+
91+
var apiClient = _attachmentsApi.Configuration.ApiClient;
92+
var response = apiClient.RestClient.Execute(request);
93+
return (AttachmentInfo)apiClient.Deserialize(response, typeof(AttachmentInfo));
94+
}
7795
}
7896
}

test/SymphonyOSS.RestApiClient.Tests/AttachmentsApiTest.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,20 @@ public void EnsureDownloadAttachment_uses_retry_strategy()
5555
}
5656

5757
[Fact]
58-
public void EnsureUploadAttachment_uses_retry_strategy()
58+
public void EnsureUploadAttachment_throws_FileNotFoundException()
5959
{
6060
const string sid = "sid";
61-
var file = new Mock<Stream>().Object;
62-
_attachmentsApi.UploadAttachment(sid, file);
63-
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, string, string, Stream, AttachmentInfo>>(), sid, "sessionToken", "keyManagerToken", file));
61+
var file = "does_not_exist.png";
62+
Exception exception = null;
63+
try
64+
{
65+
_attachmentsApi.UploadAttachment(sid, file);
66+
}
67+
catch (Exception e)
68+
{
69+
exception = e;
70+
}
71+
Assert.IsType<FileNotFoundException>(exception);
6472
}
6573
}
6674
}

0 commit comments

Comments
 (0)