Skip to content

Commit 55ad1e5

Browse files
committed
Add support for REST API v0.7
Fixes for #68: * Applied the required code fixes. * Added unit + integration tests.
1 parent cebfa08 commit 55ad1e5

File tree

6 files changed

+441
-15
lines changed

6 files changed

+441
-15
lines changed

.github/workflows/build.yml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ on:
33
# Build PRs and branches.
44
pull_request_target:
55
types: [ opened, synchronize, reopened ]
6-
paths-ignore:
7-
- .github/workflows/deploy-tagged.yml
8-
push:
6+
# paths-ignore:
7+
# - .github/workflows/deploy-tagged.yml
8+
# push:
99
branches:
1010
- '**'
11-
tags-ignore:
12-
- '**'
11+
# tags-ignore:
12+
# - '**'
1313
paths-ignore:
1414
- .github/workflows/deploy-tagged.yml
1515

@@ -47,13 +47,17 @@ jobs:
4747
jdks
4848
4949
- name: Build and test
50+
env:
51+
UPLOADCARE_PUBLIC_KEY: ${{ secrets.UPLOADCARE_PUBLIC_KEY }}
52+
UPLOADCARE_SECRET_KEY: ${{ secrets.UPLOADCARE_SECRET_KEY }}
5053
run: ./gradlew build --stacktrace
5154

5255
publish-snapshot:
5356
name: Publish snapshot to GitHub Packages
5457
runs-on: ubuntu-latest
5558
needs: build
56-
if: github.event_name == 'pull_request' || (github.event_name == 'push' && github.ref == 'refs/heads/master')
59+
environment: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.repo.fork == true && 'external-contributors' || '' }}
60+
if: github.event_name == 'pull_request_target' || (github.event_name == 'push' && github.ref == 'refs/heads/master')
5761
permissions:
5862
contents: read
5963
packages: write

src/main/java/com/uploadcare/api/File.java

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,35 @@ public boolean isImage() {
8282
}
8383

8484
public ImageInfo getImageInfo() {
85-
return fileData.imageInfo;
85+
if (fileData.imageInfo != null) {
86+
return fileData.imageInfo;
87+
}
88+
if (fileData.contentInfo != null) {
89+
return fileData.contentInfo.image;
90+
}
91+
return null;
8692
}
8793

8894
public VideoInfo getVideoInfo() {
89-
return fileData.videoInfo;
95+
if (fileData.videoInfo != null) {
96+
return fileData.videoInfo;
97+
}
98+
if (fileData.contentInfo != null) {
99+
return fileData.contentInfo.video;
100+
}
101+
return null;
102+
}
103+
104+
public ContentInfo getContentInfo() {
105+
return fileData.contentInfo;
106+
}
107+
108+
public Map<String, String> getMetadata() {
109+
return fileData.metadata;
110+
}
111+
112+
public Map<String, AppData> getAppData() {
113+
return fileData.appdata;
90114
}
91115

92116
public Map<String, Float> getRekognitionInfo() {
@@ -260,4 +284,51 @@ public String toString() {
260284
public enum ColorMode {
261285
RGB, RGBA, RGBa, RGBX, L, LA, La, P, PA, CMYK, YCbCr, HSV, LAB
262286
}
287+
288+
public static class MimeInfo {
289+
public String mime;
290+
public String type;
291+
public String subtype;
292+
293+
@Override
294+
public String toString() {
295+
return "MimeInfo{" +
296+
"mime='" + mime + '\'' +
297+
", type='" + type + '\'' +
298+
", subtype='" + subtype + '\'' +
299+
'}';
300+
}
301+
}
302+
303+
public static class ContentInfo {
304+
public MimeInfo mime;
305+
public ImageInfo image;
306+
public VideoInfo video;
307+
308+
@Override
309+
public String toString() {
310+
return "ContentInfo{" +
311+
"mime=" + mime +
312+
", image=" + image +
313+
", video=" + video +
314+
'}';
315+
}
316+
}
317+
318+
public static class AppData {
319+
public Map<String, Object> data;
320+
public String version;
321+
public Date datetimeCreated;
322+
public Date datetimeUpdated;
323+
324+
@Override
325+
public String toString() {
326+
return "AppData{" +
327+
"data=" + data +
328+
", version='" + version + '\'' +
329+
", datetimeCreated=" + datetimeCreated +
330+
", datetimeUpdated=" + datetimeUpdated +
331+
'}';
332+
}
333+
}
263334
}

src/main/java/com/uploadcare/api/RequestHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import static com.uploadcare.urls.UrlUtils.trustedBuild;
3737

3838
/**
39-
* A helper class for doing API calls to the Uploadcare API. Supports API version 0.6.
39+
* A helper class for doing API calls to the Uploadcare API. Supports API version 0.7.
4040
*
4141
* TODO Support of throttled requests needs to be added
4242
*/
@@ -117,7 +117,7 @@ public void setApiHeaders(HttpUriRequest request, String requestBodyMD5) {
117117
String formattedDate = rfc2822(calendar.getTime());
118118

119119
request.addHeader("Content-Type", JSON_CONTENT_TYPE);
120-
request.setHeader("Accept", "application/vnd.uploadcare-v0.6+json");
120+
request.setHeader("Accept", "application/vnd.uploadcare-v0.7+json");
121121
request.setHeader("Date", formattedDate);
122122
request.setHeader("User-Agent",
123123
String.format("javauploadcare/%s/%s", LIBRARY_VERSION, client.getPublicKey()));

src/main/java/com/uploadcare/data/FileData.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public class FileData {
2424
public File.VideoInfo videoInfo;
2525
public Map<String, Float> rekognitionInfo;
2626
public Map<String, String> variations;
27+
public File.ContentInfo contentInfo;
28+
public Map<String, String> metadata;
29+
public Map<String, File.AppData> appdata;
2730

2831
@Override
2932
public String toString() {

src/test/java/com/uploadcare/api/FileTest.java

Lines changed: 151 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,172 @@
33
import com.fasterxml.jackson.databind.DeserializationFeature;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
6+
import com.uploadcare.data.FileData;
67
import org.junit.Assert;
8+
import org.junit.Before;
79
import org.junit.Test;
810

11+
import static org.junit.Assert.*;
12+
913
public class FileTest
1014
{
1115

12-
@Test
13-
public void enumFails() throws Exception {
14-
String json = "{ \"color_mode\": \"RGBa\"}";
16+
private ObjectMapper mapper;
1517

16-
// duplicate the way the mapper is configured in uploadcare
17-
ObjectMapper mapper = new ObjectMapper();
18+
@Before
19+
public void setUp() {
20+
mapper = new ObjectMapper();
1821
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
1922
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
23+
}
24+
25+
@Test
26+
public void enumFails() throws Exception {
27+
String json = "{ \"color_mode\": \"RGBa\"}";
2028

2129
Bug bug = mapper.readValue(json, Bug.class);
2230

2331
Assert.assertTrue("Color mode was not properly converted!", File.ColorMode.RGBa.equals(bug.colorMode));
2432
}
2533

34+
@Test
35+
public void testContentInfoDeserialization() throws Exception {
36+
String json = "{"
37+
+ "\"uuid\": \"22240276-2f06-41f8-9411-755c8ce926ed\","
38+
+ "\"is_image\": true,"
39+
+ "\"is_ready\": true,"
40+
+ "\"mime_type\": \"image/jpeg\","
41+
+ "\"size\": 642,"
42+
+ "\"content_info\": {"
43+
+ " \"mime\": {"
44+
+ " \"mime\": \"image/jpeg\","
45+
+ " \"type\": \"image\","
46+
+ " \"subtype\": \"jpeg\""
47+
+ " },"
48+
+ " \"image\": {"
49+
+ " \"format\": \"JPEG\","
50+
+ " \"width\": 500,"
51+
+ " \"height\": 500,"
52+
+ " \"sequence\": false,"
53+
+ " \"color_mode\": \"RGB\","
54+
+ " \"orientation\": 6"
55+
+ " }"
56+
+ "}"
57+
+ "}";
58+
59+
FileData fileData = mapper.readValue(json, FileData.class);
60+
61+
assertNotNull("contentInfo should not be null", fileData.contentInfo);
62+
assertNotNull("contentInfo.mime should not be null", fileData.contentInfo.mime);
63+
assertEquals("image/jpeg", fileData.contentInfo.mime.mime);
64+
assertEquals("image", fileData.contentInfo.mime.type);
65+
assertEquals("jpeg", fileData.contentInfo.mime.subtype);
66+
67+
assertNotNull("contentInfo.image should not be null", fileData.contentInfo.image);
68+
assertEquals("JPEG", fileData.contentInfo.image.format);
69+
assertEquals(500, fileData.contentInfo.image.width);
70+
assertEquals(500, fileData.contentInfo.image.height);
71+
assertEquals(File.ColorMode.RGB, fileData.contentInfo.image.colorMode);
72+
}
73+
74+
@Test
75+
public void testVideoContentInfoDeserialization() throws Exception {
76+
String json = "{"
77+
+ "\"uuid\": \"abc123\","
78+
+ "\"is_image\": false,"
79+
+ "\"is_ready\": true,"
80+
+ "\"mime_type\": \"video/mp4\","
81+
+ "\"size\": 1048576,"
82+
+ "\"content_info\": {"
83+
+ " \"mime\": {"
84+
+ " \"mime\": \"video/mp4\","
85+
+ " \"type\": \"video\","
86+
+ " \"subtype\": \"mp4\""
87+
+ " },"
88+
+ " \"video\": {"
89+
+ " \"format\": \"MP4\","
90+
+ " \"duration\": 12000,"
91+
+ " \"bitrate\": 1500000,"
92+
+ " \"video\": {"
93+
+ " \"height\": 1080,"
94+
+ " \"width\": 1920,"
95+
+ " \"frame_rate\": 25.0,"
96+
+ " \"bitrate\": 1400000,"
97+
+ " \"codec\": \"h264\""
98+
+ " },"
99+
+ " \"audio\": {"
100+
+ " \"bitrate\": 128000,"
101+
+ " \"codec\": \"aac\","
102+
+ " \"channels\": \"2\","
103+
+ " \"sample_rate\": 44100"
104+
+ " }"
105+
+ " }"
106+
+ "}"
107+
+ "}";
108+
109+
FileData fileData = mapper.readValue(json, FileData.class);
110+
111+
assertNotNull("contentInfo should not be null", fileData.contentInfo);
112+
assertNotNull("contentInfo.mime should not be null", fileData.contentInfo.mime);
113+
assertEquals("video/mp4", fileData.contentInfo.mime.mime);
114+
assertEquals("video", fileData.contentInfo.mime.type);
115+
116+
assertNotNull("contentInfo.video should not be null", fileData.contentInfo.video);
117+
assertEquals("MP4", fileData.contentInfo.video.format);
118+
assertEquals(12000, fileData.contentInfo.video.duration);
119+
assertNotNull("video.video should not be null", fileData.contentInfo.video.video);
120+
assertEquals(1920, fileData.contentInfo.video.video.width);
121+
assertEquals(1080, fileData.contentInfo.video.video.height);
122+
assertNotNull("video.audio should not be null", fileData.contentInfo.video.audio);
123+
assertEquals("aac", fileData.contentInfo.video.audio.codec);
124+
}
125+
126+
@Test
127+
public void testMetadataDeserialization() throws Exception {
128+
String json = "{"
129+
+ "\"uuid\": \"22240276-2f06-41f8-9411-755c8ce926ed\","
130+
+ "\"metadata\": {"
131+
+ " \"subsystem\": \"uploader\","
132+
+ " \"pet\": \"cat\""
133+
+ "}"
134+
+ "}";
135+
136+
FileData fileData = mapper.readValue(json, FileData.class);
137+
138+
assertNotNull("metadata should not be null", fileData.metadata);
139+
assertEquals("uploader", fileData.metadata.get("subsystem"));
140+
assertEquals("cat", fileData.metadata.get("pet"));
141+
}
142+
143+
@Test
144+
public void testAppDataDeserialization() throws Exception {
145+
String json = "{"
146+
+ "\"uuid\": \"22240276-2f06-41f8-9411-755c8ce926ed\","
147+
+ "\"appdata\": {"
148+
+ " \"uc_clamav_virus_scan\": {"
149+
+ " \"data\": {"
150+
+ " \"infected\": false,"
151+
+ " \"infected_with\": null"
152+
+ " },"
153+
+ " \"version\": \"0.104.2\","
154+
+ " \"datetime_created\": \"2021-09-21T11:24:33\","
155+
+ " \"datetime_updated\": \"2021-09-21T11:24:33\""
156+
+ " }"
157+
+ "}"
158+
+ "}";
159+
160+
FileData fileData = mapper.readValue(json, FileData.class);
161+
162+
assertNotNull("appdata should not be null", fileData.appdata);
163+
assertTrue("appdata should contain uc_clamav_virus_scan", fileData.appdata.containsKey("uc_clamav_virus_scan"));
164+
165+
File.AppData appData = fileData.appdata.get("uc_clamav_virus_scan");
166+
167+
assertNotNull("appdata entry should not be null", appData);
168+
assertEquals("0.104.2", appData.version);
169+
assertNotNull("appdata.data should not be null", appData.data);
170+
}
171+
26172
static class Bug {
27173
public File.ColorMode colorMode;
28174
}

0 commit comments

Comments
 (0)