java-asana
java-asana copied to clipboard
Missing method to create attachments for a Project Status Updates
Hi,
I'm using version 1.0.0 of this library from Maven Central: https://mvnrepository.com/artifact/com.asana/asana/1.0.0
This is the latest version by the time of creating this ticket.
I have encountered an issue of not being able to attach files to Project Statuses via the client API. There is simply no such method to call.
The documentation of this part also seems to be a bit ahead of time, because it presents the following Java example which uses a method that is not (yet?) available in the client: https://developers.asana.com/docs/upload-an-attachment
Attachment result = client.attachments.createAttachmentForObject(file, parent, url, name)
.data("field", "value")
.data("field", "value")
.option("pretty", true)
.execute();
Instead, what the client currently offers is only usable for attaching files to tasks: https://github.com/Asana/java-asana/blob/d7281b00d29b279e835c0f3f073f1819463e28d9/src/main/java/com/asana/resources/Attachments.java#L30
I was able to work it around with the following class:
public class ExtendedAttachments extends Attachments {
public ExtendedAttachments(Client client) {
super(client);
}
public ItemRequest<Attachment> createOnProjectStatus(String projectStatus, InputStream fileContent,
String fileName, String fileType) {
return new ItemRequest<>(this, Attachment.class, "/attachments", "POST").data(
new MultipartContent()
.setMediaType(
new HttpMediaType(ContentType.MULTIPART_FORM_DATA.getMimeType()).setParameter(
"boundary",
UUID.randomUUID().toString()))
.addPart(new Part()
.setContent(new InputStreamContent(fileType, fileContent))
.setHeaders(new HttpHeaders().set(
CONTENT_DISPOSITION,
format("form-data; name=\"file\"; filename=\"%s\"", fileName)
)))
.addPart(new Part()
.setContent(new ByteArrayContent(null, projectStatus.getBytes()))
.setHeaders(new HttpHeaders().set(
CONTENT_DISPOSITION,
"form-data; name=\"parent\""
)))
);
}
}