Add method for listing folders with files available

for download (owner only)
This commit is contained in:
Torsten Grote
2022-01-21 13:58:39 -03:00
parent 482258fc92
commit 3a191908c0
3 changed files with 145 additions and 0 deletions

View File

@@ -96,6 +96,17 @@ interface MailboxApi {
void deleteFile(MailboxProperties properties, String folderId,
String fileId) throws IOException, ApiException;
/**
* Lists all contact outboxes that have files available
* for the owner to download.
*
* @return a list of folder names
* to be used with {@link #getFiles(MailboxProperties, String)}.
* @throws IllegalArgumentException if used by non-owner.
*/
List<String> getFolders(MailboxProperties properties)
throws IOException, ApiException;
@Immutable
@JsonSerialize
class MailboxContact {

View File

@@ -234,6 +234,39 @@ class MailboxApiImpl implements MailboxApi {
if (response.code() != 200) throw new ApiException();
}
@Override
public List<String> getFolders(MailboxProperties properties)
throws IOException, ApiException {
if (!properties.isOwner()) throw new IllegalArgumentException();
Response response = sendGetRequest(properties, "/folders");
if (response.code() != 200) throw new ApiException();
ResponseBody body = response.body();
if (body == null) throw new ApiException();
try {
JsonNode node = mapper.readTree(body.string());
JsonNode filesNode = node.get("folders");
if (filesNode == null || !filesNode.isArray()) {
throw new ApiException();
}
List<String> list = new ArrayList<>();
for (JsonNode fileNode : filesNode) {
if (!fileNode.isObject()) throw new ApiException();
ObjectNode objectNode = (ObjectNode) fileNode;
JsonNode idNode = objectNode.get("id");
if (idNode == null || !idNode.isTextual()) {
throw new ApiException();
}
String id = idNode.asText();
if (!isValidToken(id)) throw new ApiException();
list.add(id);
}
return list;
} catch (JacksonException e) {
throw new ApiException();
}
}
/* Helper Functions */
private Response sendGetRequest(MailboxProperties properties, String path)