Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Getting collaborators allows to specify fields #1178

Merged
merged 2 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/intTest/java/com/box/sdk/BoxFileIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,21 @@ public void getInfoWithRepresentationsIntegrationTestWithComplexHint() {
}

@Test
public void getRepresentationContentSucceeds() {
public void getRepresentationContentSucceeds() throws InterruptedException {
BoxAPIConnection api = jwtApiForServiceAccount();
String fileName = "smalltest.pdf";
BoxFile file = null;
try {
file = uploadSampleFileToUniqueFolder(api, fileName);

final String fileId = file.getID();
String representationHint = "[jpg?dimensions=32x32]";
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
file.getRepresentationContent(representationHint, outputStream);
byte[] downloadedRepresentationContent = outputStream.toByteArray();
Retry.retry(() -> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails sometimes so retrying usually solves issue.

new BoxFile(api, fileId).getRepresentationContent(representationHint, outputStream);
byte[] downloadedRepresentationContent = outputStream.toByteArray();

assertNotNull(downloadedRepresentationContent);
assertNotNull(downloadedRepresentationContent);
}, 5, 100);
} finally {
deleteFile(file);
}
Expand Down
6 changes: 3 additions & 3 deletions src/intTest/java/com/box/sdk/MetadataIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,6 @@ public void testMultiSelectMetadataCRUD() {

// Update instance multiselect field
actualMD.test("/" + fieldKey + "/0", "bar");
//todo: this check always fails
// actualMD.test("/" + fieldKey + "/1", "foo");
values = new ArrayList<>();
values.add("two");
values.add("one");
Expand All @@ -203,13 +201,15 @@ public void testMultiSelectMetadataCRUD() {

multiSelectValues = updatedMD.getMultiSelect("/" + fieldKey);
assertThat(multiSelectValues, Matchers.hasSize(2));
assertThat(multiSelectValues, containsInAnyOrder("blargh", "foooooo"));
assertThat(multiSelectValues, containsInAnyOrder("foooooo", "bar"));
multiSelectValues = updatedMD.getMultiSelect("/otherMultiSelect");
assertThat(multiSelectValues, hasSize(2));
assertThat(multiSelectValues, containsInAnyOrder("one", "two"));

// Delete metadata template and folder
} finally {
System.out.printf("Template [ID %s] [Key %s]%n", template.getID(), template.getTemplateKey());
System.out.printf("Folder [ID %s]%n", folder.getID());
this.deleteMetadata(api, template);
deleteFolder(folder);

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/box/sdk/BoxCollaboration.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,14 @@ protected static BoxCollaboration.Info create(
* @param api the API connection to use.
* @return a collection of pending collaboration infos.
*/
public static Collection<Info> getPendingCollaborations(BoxAPIConnection api) {
URL url = PENDING_COLLABORATIONS_URL.build(api.getBaseURL());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is PENDING_COLLABORATIONS_URL used now anywhere? Seems not. Constant to be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is public field. Removing it can break someone else code. I'm suggesting leaving it.

public static Collection<Info> getPendingCollaborations(BoxAPIConnection api, String... fields) {
QueryStringBuilder queryBuilder = new QueryStringBuilder();
queryBuilder.appendParam("status", "pending");
if (fields.length > 0) {
queryBuilder.appendParam("fields", fields);
}
URL url = COLLABORATIONS_URL_TEMPLATE.buildWithQuery(api.getBaseURL(), queryBuilder.toString());


BoxJSONRequest request = new BoxJSONRequest(api, url, "GET");
try (BoxJSONResponse response = request.send()) {
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/com/box/sdk/BoxFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public BoxTask.Info addTask(BoxTask.Action action, String message, Date dueAt,
* @return the temporary download URL
*/
public URL getDownloadURL() {
URL url = CONTENT_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
URL url = getDownloadUrl();
BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
request.setFollowRedirects(false);

Expand Down Expand Up @@ -305,7 +305,7 @@ public void download(OutputStream output) {
* @param listener a listener for monitoring the download's progress.
*/
public void download(OutputStream output, ProgressListener listener) {
URL url = CONTENT_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
URL url = getDownloadUrl();
BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
BoxAPIResponse response = request.send();
writeStream(response, output, listener);
Expand Down Expand Up @@ -342,7 +342,7 @@ public void downloadRange(OutputStream output, long rangeStart, long rangeEnd) {
* @param listener a listener for monitoring the download's progress.
*/
public void downloadRange(OutputStream output, long rangeStart, long rangeEnd, ProgressListener listener) {
URL url = CONTENT_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
URL url = getDownloadUrl();
BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
if (rangeEnd > 0) {
request.addHeader("Range", String.format("bytes=%s-%s", rangeStart, rangeEnd));
Expand All @@ -352,6 +352,14 @@ public void downloadRange(OutputStream output, long rangeStart, long rangeEnd, P
writeStream(request.send(), output, listener);
}

/**
* Can be used to override the URL used for file download.
* @return URL for file downalod
*/
protected URL getDownloadUrl() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This of our internal use when we want to change download URL

return CONTENT_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
}

@Override
public BoxFile.Info copy(BoxFolder destination) {
return this.copy(destination, null);
Expand Down Expand Up @@ -516,6 +524,11 @@ public void getRepresentationContent(String representationHint, String assetPath
String repContentURLString = null;
while (repContentURLString == null) {
repContentURLString = this.pollRepInfo(representation.getInfo().getUrl());
try {
Thread.sleep(100);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this sleep ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is basically infinite loop that is doing as many requests as it can. Only thing that can slow it down is rate limiter. This seems like a least we could improve.

} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

this.makeRepresentationContentRequest(repContentURLString, assetPath, output);
Expand Down Expand Up @@ -649,7 +662,7 @@ public boolean canUploadVersion(String name) {
*/
public boolean canUploadVersion(String name, long fileSize) {

URL url = CONTENT_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
URL url = getDownloadUrl();
lukaszsocha2 marked this conversation as resolved.
Show resolved Hide resolved
BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "OPTIONS");

JsonObject preflightInfo = new JsonObject();
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/box/sdk/BoxFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,14 @@ private BoxSharedLink removeCanEditPermissionIfSet(BoxSharedLink sharedLink) {
*
* @return a collection of information about the collaborations for this folder.
*/
public Collection<BoxCollaboration.Info> getCollaborations() {
public Collection<BoxCollaboration.Info> getCollaborations(String... fields) {
BoxAPIConnection api = this.getAPI();
URL url = GET_COLLABORATIONS_URL.build(api.getBaseURL(), this.getID());
QueryStringBuilder queryBuilder = new QueryStringBuilder();
if (fields.length > 0) {
queryBuilder.appendParam("fields", fields);
}
URL url = GET_COLLABORATIONS_URL.buildWithQuery(api.getBaseURL(), queryBuilder.toString(), this.getID());


BoxJSONRequest request = new BoxJSONRequest(api, url, "GET");
try (BoxJSONResponse response = request.send()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"total_count": 1,
"total_count": 2,
"entries": [
{
"type": "collaboration",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"total_count": 2,
"entries": [
{
"type": "collaboration",
"id": "12345",
"item": {
"type": "folder",
"id": "3333",
"sequence_id": "1",
"etag": "1",
"name": "Test Folder"
},
"can_view_path": false
},
{
"type": "collaboration",
"id": "124783",
"item": {
"type": "folder",
"id": "3333",
"sequence_id": "1",
"etag": "1",
"name": "Test Folder"
},
"can_view_path": true
}
]
}
20 changes: 20 additions & 0 deletions src/test/java/com/box/sdk/BoxCollaborationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ public void testGetPendingCollaborationInfoSucceeds() {
assertEquals(BoxCollaboration.Role.EDITOR, pendingCollabInfo.getRole());
}

@Test
public void testGetPendingCollaborationInfoWithFieldsSucceeds() {
final String collaborationURL = "/2.0/collaborations?status=pending&fields=id%2Crole%2Cstatus";

String result = TestUtils.getFixture("BoxCollaboration/GetPendingCollaborationInfo200");

wireMockRule.stubFor(WireMock.get(WireMock.urlEqualTo(collaborationURL))
.withQueryParam("status", WireMock.containing("pending"))
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", APPLICATION_JSON)
.withBody(result)));

Collection<BoxCollaboration.Info> pendingCollaborations =
BoxCollaboration.getPendingCollaborations(this.api, "id", "role", "status");
BoxCollaboration.Info pendingCollabInfo = pendingCollaborations.iterator().next();

assertEquals(BoxCollaboration.Status.PENDING, pendingCollabInfo.getStatus());
assertEquals(BoxCollaboration.Role.EDITOR, pendingCollabInfo.getRole());
}

@Test
public void testGetCollaborationsOnFolderSucceeds() {
final String folderID = "12345";
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/box/sdk/BoxFolderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,33 @@ public void testGetAllFolderCollaborationsSucceeds() {
collaborationInfo2.getAccessibleBy().getGroupType());
}

@Test
public void testGetAllFolderCollaborationsWithFieldsSucceeds() {
final String folderID = "3333";
final String folderCollaborationURL = "/2.0/folders/" + folderID
+ "/collaborations?fields=id%2Ctype%2Ccan_view_path";

String result = TestUtils.getFixture("BoxFolder/GetAllFolderCollaborations200WithFields");

wireMockRule.stubFor(WireMock.get(WireMock.urlEqualTo(folderCollaborationURL))
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", APPLICATION_JSON)
.withBody(result)));

BoxFolder folder = new BoxFolder(this.api, folderID);
Collection<BoxCollaboration.Info> collaborations = folder.getCollaborations("id", "type", "can_view_path");

Iterator<BoxCollaboration.Info> iterator = collaborations.iterator();

BoxCollaboration.Info collaborationInfo = iterator.next();
assertEquals("12345", collaborationInfo.getID());
assertFalse(collaborationInfo.getCanViewPath());

BoxCollaboration.Info collaborationInfo2 = iterator.next();
assertEquals("124783", collaborationInfo2.getID());
assertTrue(collaborationInfo2.getCanViewPath());
}

@Test
public void testCreateMetadataOnFolderSucceedsAndSendsCorrectJson() {
final String folderID = "12345";
Expand Down