Skip to content

Commit c3a39b1

Browse files
Added redact API functionality (#517)
* Added redact API functionality * added test for redacting functionality * added new test to check file has been redacted
1 parent 2b2291c commit c3a39b1

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Newtonsoft.Json;
5+
using ZendeskApi_v2.Models.Shared;
6+
7+
namespace ZendeskApi_v2.Models.Requests
8+
{
9+
public class IndividualAttachmentResponse
10+
{
11+
[JsonProperty("attachment")]
12+
public Attachment Attachment { get; set; }
13+
}
14+
}

src/ZendeskApi_v2/Requests/Attachments.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#endif
1212
using ZendeskApi_v2.Extensions;
1313
using ZendeskApi_v2.Models.HelpCenter.Attachments;
14+
using ZendeskApi_v2.Models.Requests;
1415
using ZendeskApi_v2.Models.Shared;
1516

1617
namespace ZendeskApi_v2.Requests
@@ -22,6 +23,7 @@ public interface IAttachments : ICore
2223
Upload UploadAttachment(ZenFile file, int? timeout = null);
2324
Upload UploadAttachments(IEnumerable<ZenFile> files, int? timeout = null);
2425
bool DeleteUpload(Upload upload);
26+
IndividualAttachmentResponse RedactCommentAttachment(long attachmentId, long ticketId, long commentId);
2527
#endif
2628

2729
#if ASYNC
@@ -37,7 +39,7 @@ public interface IAttachments : ICore
3739
Task<Upload> UploadAttachmentsAsync(IEnumerable<ZenFile> files, int? timeout = null);
3840
Task<bool> DeleteUploadAsync(Upload upload);
3941
Task<ZenFile> DownloadAttachmentAsync(Attachment attachment);
40-
42+
Task<IndividualAttachmentResponse> RedactCommentAttachmentAsync(long attachmentId, long ticketId, long commentId);
4143
#endif
4244
}
4345

@@ -104,6 +106,12 @@ public bool DeleteUpload(Upload upload)
104106
{
105107
return (upload?.Token == null ? false : GenericDelete($"/uploads/{upload.Token}.json"));
106108
}
109+
110+
public IndividualAttachmentResponse RedactCommentAttachment(long attachmentId, long ticketId, long commentId)
111+
{
112+
var resource = $"/tickets/{ticketId}/comments/{commentId}/attachments/{attachmentId}/redact";
113+
return RunRequest<IndividualAttachmentResponse>(resource, RequestMethod.Put);
114+
}
107115
#endif
108116

109117
#if ASYNC
@@ -175,6 +183,12 @@ public async Task<ZenFile> DownloadAttachmentAsync(Attachment attachment)
175183
return file;
176184
}
177185

186+
public async Task<IndividualAttachmentResponse> RedactCommentAttachmentAsync(long attachmentId, long ticketId, long commentId)
187+
{
188+
var resource = $"/tickets/{ticketId}/comments/{commentId}/attachments/{attachmentId}/redact";
189+
return await RunRequestAsync<IndividualAttachmentResponse>(resource, RequestMethod.Put);
190+
}
191+
178192
#endif
179193

180194

test/ZendeskApi_v2.Test/AttachmentTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Collections.Generic;
1010
using System.Linq;
1111
using Newtonsoft.Json;
12+
using System.Net;
1213

1314
namespace Tests
1415
{
@@ -65,5 +66,44 @@ public async Task CanDowloadAttachment()
6566
Assert.That(api.Tickets.Delete(t1.Ticket.Id.Value), Is.True);
6667
Assert.That(api.Attachments.DeleteUpload(res));
6768
}
69+
70+
[Test]
71+
public async Task CanRedactAttachment()
72+
{
73+
//This could probably be brought into a helper for above two tests perhaps
74+
var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "testupload.txt");
75+
76+
var res = await api.Attachments.UploadAttachmentAsync(new ZenFile()
77+
{
78+
ContentType = "text/plain",
79+
FileName = "testupload.txt",
80+
FileData = File.ReadAllBytes(path)
81+
});
82+
83+
var ticket = new Ticket()
84+
{
85+
Subject = "testing attachments",
86+
Priority = TicketPriorities.Normal,
87+
Comment = new Comment()
88+
{
89+
Body = "comments are required for attachments",
90+
Public = true,
91+
Uploads = new List<string>() { res.Token }
92+
},
93+
};
94+
95+
var t1 = await api.Tickets.CreateTicketAsync(ticket);
96+
97+
var comments = api.Tickets.GetTicketComments(t1.Ticket.Id.Value);
98+
99+
var attach = comments.Comments[0].Attachments[0];
100+
101+
var delRes = api.Attachments.RedactCommentAttachment(attach.Id, t1.Ticket.Id.Value, comments.Comments[0].Id.Value);
102+
//Returned correct attachment
103+
Assert.That(delRes.Attachment.Id, Is.EqualTo(attach.Id));
104+
105+
//Check the file has been replaced by redacted.txt
106+
Assert.That(api.Tickets.GetTicketComments(t1.Ticket.Id.Value).Comments[0].Attachments[0].FileName, Is.EqualTo("redacted.txt"));
107+
}
68108
}
69109
}

0 commit comments

Comments
 (0)