GeensClient

The GeensClient is the client that can be used to execute the necessary operations on the Geens account of the logged in user.
After authentication (see OAuth Authentication page) was successfull, the GeensClient can be initialised as follows:

GeensClient geensClient = new GeensClient(geensAuthentication);

Below follows a list of operations you can do with the GeensClient.

Create Directory

To create a directory you pass a parentId and a title to the createDirectory method.
The parentId can be found by using geensClient.getItems or Item.ITEM_HAS_NO_PARENT when you want to create a root directory.

int folderId = geensClient.createDirectory(Item.ITEM_HAS_NO_PARENT, "SDKExample");

Create File

To create a new file the geensClient.createFile method can be used. The id of the parentfolder has to be passed together with the title, extension and the mimeType. The content of the file should be passed as an InputStream (FileInputStream, ByteArrayInputStream,...)
A completableFuture will be returned as the file upload will happen in the background. To wait on the completion and the id of the file, the "get()" method can be called.

File f = getFileFromResources("test.png");
 CompletableFuture<IntegerResult> createFileFuture = geensClient.createFile(folderId, "test", "png", "image/png", new FileInputStream(f));
IntegerResult idResult = createFileFuture.get();
if(!idResult.isSuccess()) {
    throw new RuntimeException(idResult.getError());
}
Integer createdFileId = idResult.getInteger();

Get Item

You can get Item by it's id.

Item item = geensClient.getItem(itemId);

List Items

You can list items of directory.


ItemList itemList = geensClient.getItems(parentId , itemId);
ItemList itemList1 = geensClient.getItems(parent , itemId);
ItemList itemList2 = geensClient.getItems(parentId , item);
ItemList itemList3 = geensClient.getItems(parent , item);

Delete item

geensClient.deleteItem(itemId);

Update item

To update an item there are multiple available methods:

  • updateDirectory(Item item)
    • Allows you to update the metadata of a directory
  • updateFile(Item item)
    • Allows you to update the metadata of a file
  • updateFile(Item item, InputStream, fileContent)
    • Allows you to update the metadata and content of a file
String newTitle = "updatedTest";
item.setTitle(newTitle);
Item updatedFileName = geensClient.updateFile(item);
newTitle = "test2";
item.setTitle(newTitle);
File f2 = getFileFromResources("test2.png");
CompletableFuture<ItemResult> updatedFileFuture = geensClient.updateFile(item, new FileInputStream(f2));
ItemResult updatedFileResult = updatedFileFuture.get();
if(!updatedFileResult.isSuccess()) {
    throw new RuntimeException(updatedFileResult.getError());
}
Item updatedFile = updatedFileResult.getItem();

Download Item

To download a specific file, first retrieve the item by the geensClient.getItem() method. This item object can be passed as an input to geensClient.downloadFile().
An OutputStream can be passed to process the downloading file. A ByteArrayOutputStream can be used to store the data in a byte array or a FileOutputStream can be used to store it in a file.
This method returns a future which will indicate when the download is finished. By doing a get() call on the future, the thread waits until the download is finished.

//Download item
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CompletableFuture downloadFuture = geensClient.downloadFile(item, outputStream);
downloadFuture.get();

byte[] downloadedBytes = outputStream.toByteArray();
File parentDirectory = f.getParentFile();
File downloadedFile = new File(parentDirectory, "testDownload.png");
FileOutputStream fos = new FileOutputStream(downloadedFile);
CompletableFuture downloadFileFuture = geensClient.downloadFile(item, fos);
downloadFileFuture.get();

Copy Item

One or multiple items can be copied to a specified destination by using the copyItem() or copyItems() operation. Next to single files, directories with all its contents can be copied as well.
Copying a single file can be done by passing the item object or its id as well as the id of the parent.

CompletableFuture<ItemListResult> copyOneFuture = geensClient.copyItem(item, folderId);
ItemListResult copyOneResult = copyOneFuture.get();
if(!copyOneResult.isSuccess()) {
    throw new RuntimeException(copyOneResult.getError());
}

item = copyOneResult.getItemList().get(0);
int itemId = item.getId();
CompletableFuture<ItemListResult> copyOneFuture = geensClient.copyItem(itemId, folderId);
ItemListResult copyOneResult = copyOneFuture.get();
if(!copyOneResult.isSuccess()) {
    throw new RuntimeException(copyOneResult.getError());
}

item = copyOneResult.getItemList().get(0);

Copying multiple files can be done by passing a list of item objects or an array of ids together with the id of the parent.
In this scenario copyEmptyDirectory and copyFilledDirectory are 2 id's of exisiting directories.

Integer[] copyIds = {copyEmptyDirectory, folderId};
CompletableFuture<ItemListResult> copyFuture = geensClient.copyItems(copyIds, copyDestionationId);
ItemListResult copiedItemsResult = copyFuture.get();
if(!copiedItemsResult.isSuccess()) {
    throw new RuntimeException(copiedItemsResult.getError());
}
List<Item> copiedItems = copiedItemsResult.getItemList();
CompletableFuture<List<Item>> copyFuture = geensClient.copyItems(items, copyDestionationId);
ItemListResult copiedItemsResult = copyFuture.get();
if(!copiedItemsResult.isSuccess()) {
    throw new RuntimeException(copiedItemsResult.getError());
}
List<Item> copiedItems = copiedItemsResult.getItemList();

Move Item

Items can be moved to other directory by using moveItems() operation. Next to single files, directories with all its contents can be copied as well.

 Item [] items = {folder1Item, item};
 ItemList movedItems = geensClient.moveItem(items, -1);
 Item movedItem = movedItems.findItemById(folderId2);