Skip to content

Commit fe8b3f6

Browse files
authored
Implemented UploadFile.fromByteArray (#1224)
for richmenu upload api, support uploading with byte array
1 parent 094b4ac commit fe8b3f6

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

clients/line-bot-client-base/src/main/java/com/linecorp/bot/client/base/BotAwareJacksonConverter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ private RequestBody buildRequestBodyFromUploadFile(Object o) {
6868
return RequestBody.create(uploadFile.src(), MediaType.parse(uploadFile.contentType()));
6969
} else if (o instanceof UploadFile.FileUploadFile uploadFile) {
7070
return RequestBody.create(uploadFile.src(), MediaType.parse(uploadFile.contentType()));
71+
} else if (o instanceof UploadFile.ByteArrayUploadFile uploadFile) {
72+
return RequestBody.create(uploadFile.src(), MediaType.parse(uploadFile.contentType()));
7173
}
7274

7375
throw new IllegalArgumentException("Unsupported object: " + o);

clients/line-bot-client-base/src/main/java/com/linecorp/bot/client/base/UploadFile.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
import java.io.File;
2222

2323
public interface UploadFile {
24+
/**
25+
* Upload file from byte array.
26+
*/
27+
static ByteArrayUploadFile fromByteArray(byte[] src, String contentType) {
28+
return new ByteArrayUploadFile(src, contentType);
29+
}
30+
2431
/**
2532
* Upload file from String.
2633
*/
@@ -57,6 +64,12 @@ static FileUploadFile fromFile(File src) {
5764
return new FileUploadFile(src, "text/plain");
5865
}
5966

67+
record ByteArrayUploadFile(
68+
byte[] src,
69+
String contentType
70+
) implements UploadFile {
71+
}
72+
6073
record StringUploadFile(
6174
String src,
6275
String contentType

clients/line-bot-messaging-api-client/src/test/java/com/linecorp/bot/messaging/client/MessagingApiBlobClientExTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import java.io.IOException;
3535
import java.net.URI;
36+
import java.nio.charset.StandardCharsets;
3637
import java.util.Map;
3738

3839
import org.junit.jupiter.api.AfterEach;
@@ -134,4 +135,24 @@ public void setRichMenuImageTest() {
134135

135136
target.setRichMenuImage(richMenuId, body).join();
136137
}
138+
139+
@Test
140+
public void setRichMenuImageByteArrayTest() {
141+
stubFor(post(urlPathTemplate("/v2/bot/richmenu/{richMenuId}/content"))
142+
.withHeader("content-type", containing("image/jpeg")).willReturn(
143+
aResponse()
144+
.withStatus(200)
145+
.withHeader("content-type", "application/json")
146+
.withBody("{}")));
147+
148+
String richMenuId = Arranger.some(String.class, Map.of(
149+
"message", () -> new TextMessage("hello"),
150+
"recipient", () -> null,
151+
"filter", () -> null));
152+
UploadFile body = UploadFile.fromByteArray(
153+
"HELLO_FILE".getBytes(StandardCharsets.UTF_8),
154+
"image/jpeg");
155+
156+
target.setRichMenuImage(richMenuId, body).join();
157+
}
137158
}

0 commit comments

Comments
 (0)