Skip to content

Commit 0f58913

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 0f58913

6 files changed

Lines changed: 575 additions & 35 deletions

File tree

.github/workflows/build.yml

Lines changed: 144 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
name: Build and test
22
on:
3-
# Build PRs and branches.
4-
pull_request_target:
5-
types: [ opened, synchronize, reopened ]
3+
pull_request:
4+
types: [opened, synchronize, reopened, closed]
65
paths-ignore:
76
- .github/workflows/deploy-tagged.yml
7+
88
push:
99
branches:
10-
- '**'
10+
- "**"
1111
tags-ignore:
12-
- '**'
12+
- "**"
1313
paths-ignore:
1414
- .github/workflows/deploy-tagged.yml
1515

@@ -19,41 +19,49 @@ jobs:
1919
runs-on: ubuntu-latest
2020
strategy:
2121
matrix:
22-
# https://en.wikipedia.org/wiki/Java_version_history
23-
java: [ '8', '11' ] # LTS
22+
java: ["8", "11"]
2423

2524
steps:
2625
- name: Checkout
2726
uses: actions/checkout@v6
2827
with:
29-
ref: ${{ github.event.pull_request.head.sha }}
28+
# For pull_request, github.sha is a merge commit; we want the PR head SHA.
29+
# For push, github.sha is correct.
30+
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
31+
3032
- name: Setup JDK
3133
uses: actions/setup-java@v5
3234
with:
3335
java-version: ${{ matrix.java }}
34-
distribution: 'temurin'
36+
distribution: temurin
3537

3638
- name: Setup Gradle
3739
uses: gradle/gradle-build-action@v2
3840
with:
39-
# The Gradle wrapper's version (already the default, putting it here to clarity)
4041
gradle-version: wrapper
41-
# Removing unused files from Gradle User Home before saving to cache (i.e. older versions of gradle)
4242
gradle-home-cache-cleanup: true
43-
# Cache downloaded JDKs in addition to the default directories.
4443
gradle-home-cache-includes: |
4544
caches
4645
notifications
4746
jdks
4847
4948
- name: Build and test
49+
env:
50+
UPLOADCARE_PUBLIC_KEY: ${{ secrets.UPLOADCARE_PUBLIC_KEY }}
51+
UPLOADCARE_SECRET_KEY: ${{ secrets.UPLOADCARE_SECRET_KEY }}
5052
run: ./gradlew build --stacktrace
5153

52-
publish-snapshot:
53-
name: Publish snapshot to GitHub Packages
54+
publish-pr-snapshot:
55+
name: Publish PR snapshot to GitHub Packages
5456
runs-on: ubuntu-latest
5557
needs: build
56-
if: github.event_name == 'pull_request' || (github.event_name == 'push' && github.ref == 'refs/heads/master')
58+
59+
# Only publish for PRs from the same repo (forks won't have permissions to publish packages).
60+
if: >
61+
github.event_name == 'pull_request' &&
62+
github.event.action != 'closed' &&
63+
github.event.pull_request.head.repo.full_name == github.repository
64+
5765
permissions:
5866
contents: read
5967
packages: write
@@ -63,30 +71,26 @@ jobs:
6371
uses: actions/checkout@v6
6472
with:
6573
ref: ${{ github.event.pull_request.head.sha }}
74+
6675
- name: Get base version
6776
id: get-version
6877
run: |
6978
BASE_VERSION=$(grep -iE "version([ ]*)=" gradle.properties | cut -f 2 -d "=" | tr -d '[:space:]')
70-
# Strip any existing -SNAPSHOT suffix so we can re-apply it cleanly
7179
BASE_VERSION="${BASE_VERSION%-SNAPSHOT}"
7280
echo "base_version=${BASE_VERSION}" >> $GITHUB_OUTPUT
7381
74-
- name: Determine snapshot version
82+
- name: Determine PR snapshot version
7583
id: snapshot-version
7684
run: |
77-
if [ "${{ github.event_name }}" == "pull_request_target" ]; then
78-
SNAPSHOT_VERSION="${{ steps.get-version.outputs.base_version }}-PR-${{ github.event.pull_request.number }}-SNAPSHOT"
79-
else
80-
SNAPSHOT_VERSION="${{ steps.get-version.outputs.base_version }}-SNAPSHOT"
81-
fi
85+
SNAPSHOT_VERSION="${{ steps.get-version.outputs.base_version }}-PR-${{ github.event.pull_request.number }}-SNAPSHOT"
8286
echo "snapshot_version=${SNAPSHOT_VERSION}" >> $GITHUB_OUTPUT
83-
echo "Publishing snapshot version: ${SNAPSHOT_VERSION}"
87+
echo "Publishing PR snapshot version: ${SNAPSHOT_VERSION}"
8488
8589
- name: Setup JDK
8690
uses: actions/setup-java@v5
8791
with:
88-
java-version: 8
89-
distribution: 'temurin'
92+
java-version: "8"
93+
distribution: temurin
9094

9195
- name: Setup Gradle
9296
uses: gradle/gradle-build-action@v2
@@ -98,8 +102,122 @@ jobs:
98102
notifications
99103
jdks
100104
101-
- name: Publish snapshot to GitHub Packages
105+
- name: Publish PR snapshot to GitHub Packages
102106
env:
103107
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
104108
GITHUB_ACTOR: ${{ github.actor }}
105109
run: ./gradlew publishReleasePublicationToGitHubPackagesRepository -Pversion=${{ steps.snapshot-version.outputs.snapshot_version }} --stacktrace
110+
111+
publish-master-snapshot:
112+
name: Publish master snapshot to GitHub Packages
113+
runs-on: ubuntu-latest
114+
needs: build
115+
116+
# Publish on direct pushes to master (including merge commits).
117+
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
118+
119+
permissions:
120+
contents: read
121+
packages: write
122+
123+
steps:
124+
- name: Checkout
125+
uses: actions/checkout@v6
126+
127+
- name: Get base version
128+
id: get-version
129+
run: |
130+
BASE_VERSION=$(grep -iE "version([ ]*)=" gradle.properties | cut -f 2 -d "=" | tr -d '[:space:]')
131+
BASE_VERSION="${BASE_VERSION%-SNAPSHOT}"
132+
echo "base_version=${BASE_VERSION}" >> $GITHUB_OUTPUT
133+
134+
- name: Determine master snapshot version
135+
id: snapshot-version
136+
run: |
137+
SNAPSHOT_VERSION="${{ steps.get-version.outputs.base_version }}-SNAPSHOT"
138+
echo "snapshot_version=${SNAPSHOT_VERSION}" >> $GITHUB_OUTPUT
139+
echo "Publishing master snapshot version: ${SNAPSHOT_VERSION}"
140+
141+
- name: Setup JDK
142+
uses: actions/setup-java@v5
143+
with:
144+
java-version: "8"
145+
distribution: temurin
146+
147+
- name: Setup Gradle
148+
uses: gradle/gradle-build-action@v2
149+
with:
150+
gradle-version: wrapper
151+
gradle-home-cache-cleanup: true
152+
gradle-home-cache-includes: |
153+
caches
154+
notifications
155+
jdks
156+
157+
- name: Publish master snapshot to GitHub Packages
158+
env:
159+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
160+
GITHUB_ACTOR: ${{ github.actor }}
161+
run: ./gradlew publishReleasePublicationToGitHubPackagesRepository -Pversion=${{ steps.snapshot-version.outputs.snapshot_version }} --stacktrace
162+
163+
cleanup-merged-pr-snapshot:
164+
name: Delete merged PR snapshot from GitHub Packages
165+
runs-on: ubuntu-latest
166+
167+
# Triggered by pull_request.closed (merge) into master
168+
if: >
169+
github.event_name == 'pull_request' &&
170+
github.event.action == 'closed' &&
171+
github.event.pull_request.merged == true &&
172+
github.event.pull_request.base.ref == 'master'
173+
174+
permissions:
175+
packages: write
176+
177+
steps:
178+
- name: Compute PR snapshot version
179+
id: prver
180+
run: |
181+
# Must match the same scheme used in publish-pr-snapshot
182+
# We need base version too:
183+
BASE_VERSION=$(grep -iE "version([ ]*)=" gradle.properties | cut -f 2 -d "=" | tr -d '[:space:]')
184+
BASE_VERSION="${BASE_VERSION%-SNAPSHOT}"
185+
echo "version=${BASE_VERSION}-PR-${{ github.event.pull_request.number }}-SNAPSHOT" >> $GITHUB_OUTPUT
186+
187+
- name: Delete package version via GitHub API
188+
uses: actions/github-script@v8
189+
with:
190+
github-token: ${{ secrets.GITHUB_TOKEN }}
191+
script: |
192+
const owner = context.repo.owner;
193+
const repo = context.repo.repo;
194+
195+
const package_type = "maven";
196+
197+
// TODO: IMPORTANT:
198+
// Set this to the exact "Package name" as shown in the repo's Packages UI.
199+
// For Maven packages this is often the artifactId, but verify.
200+
const package_name = "YOUR_PACKAGE_NAME_HERE";
201+
202+
const versionName = "${{ steps.prver.outputs.version }}";
203+
204+
const versions = await github.paginate(
205+
github.rest.packages.getAllPackageVersionsForPackageOwnedByRepo,
206+
{ owner, repo, package_type, package_name, per_page: 100 }
207+
);
208+
209+
const match = versions.find(v => v.name === versionName);
210+
if (!match) {
211+
core.warning(`No package version found named "${versionName}" in ${package_type}/${package_name}. Nothing to delete.`);
212+
return;
213+
}
214+
215+
await github.rest.packages.deletePackageVersionForRepoOwnedByOwner({
216+
owner,
217+
repo,
218+
package_type,
219+
package_name,
220+
package_version_id: match.id,
221+
});
222+
223+
core.info(`Deleted ${package_type}/${package_name}@${versionName} (id=${match.id}).`);

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() {

0 commit comments

Comments
 (0)