Search in sources :

Example 1 with Folder

use of com.microsoft.graph.extensions.Folder in project android-java-snippets-sample by microsoftgraph.

the class DrivesSnippets method getDrivesSnippets.

static DrivesSnippets[] getDrivesSnippets() {
    return new DrivesSnippets[] { // Marker element
    new DrivesSnippets(null) {

        @Override
        public void request(ICallback callback) {
        // No implementation
        }
    }, /* Get the user's drive
                 * GET https://graph.microsoft.com/{version}/me/drive
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/drive_get
                 */
    new DrivesSnippets<JsonObject>(get_me_drive) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            mGraphServiceClient.getMe().getDrive().buildRequest().get(new ICallback<Drive>() {

                @Override
                public void success(Drive drive) {
                    callback.success(drive.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /*
                 * Get files in the root folder
                 * GET https://graph.microsoft.com/{version}/me/drive/root/children
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/item_list_children
                 */
    new DrivesSnippets<JsonObject>(get_me_files) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            // Get files in root folder
            mGraphServiceClient.getMe().getDrive().getRoot().getChildren().buildRequest().get(new ICallback<IDriveItemCollectionPage>() {

                @Override
                public void success(IDriveItemCollectionPage iDriveItemCollectionPage) {
                    callback.success(iDriveItemCollectionPage.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /*
                 * Create a file
                 * PUT https://graph.microsoft.com/{version}/me/drive/root/children/{filename}/content
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/item_post_children
                 */
    new DrivesSnippets<JsonObject>(create_me_file) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            // create a new file
            try {
                String guid = UUID.randomUUID().toString();
                byte[] byteArray = guid.getBytes("UTF-8");
                mGraphServiceClient.getMe().getDrive().getRoot().getChildren(guid).getContent().buildRequest().put(byteArray, new ICallback<DriveItem>() {

                    @Override
                    public void success(DriveItem driveItem) {
                        callback.success(driveItem.getRawObject());
                    }

                    @Override
                    public void failure(ClientException ex) {
                        callback.failure(ex);
                    }
                });
            } catch (UnsupportedEncodingException uee) {
                uee.printStackTrace();
            }
        }
    }, /*
                 * Download the content of a file
                 * GET https://graph.microsoft.com/{version}/me/drive/items/{filename}/content
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/item_downloadcontent
                 */
    new DrivesSnippets<JsonObject>(download_me_file) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            // create a new file to download
            String guid = UUID.randomUUID().toString();
            byte[] byteArray = null;
            try {
                byteArray = guid.getBytes("UTF-8");
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
            mGraphServiceClient.getMe().getDrive().getRoot().getChildren(guid).getContent().buildRequest().put(byteArray, new ICallback<DriveItem>() {

                @Override
                public void success(DriveItem driveItem) {
                    // Get the guid that the service assigned to my file
                    String guid = driveItem.id;
                    mGraphServiceClient.getMe().getDrive().getItems().byId(guid).getContent().buildRequest().get(new ICallback<InputStream>() {

                        @Override
                        public void success(InputStream inputStream) {
                            final InputStreamReader inr = new InputStreamReader(inputStream);
                            String text;
                            try {
                                text = CharStreams.toString(inr);
                                JsonObject result = new JsonObject();
                                result.addProperty("value", text);
                                callback.success(result);
                            } catch (IOException ex) {
                                ex.printStackTrace();
                            }
                        }

                        @Override
                        public void failure(ClientException ex) {
                            callback.failure(ex);
                        }
                    });
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /*
                 * Update the content of a file
                 * PUT https://graph.microsoft.com/{version}/me/drive/items/{filename}/content
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/item_update
                 */
    new DrivesSnippets<JsonObject>(update_me_file) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            // create a new file to update
            String guid = UUID.randomUUID().toString();
            byte[] byteArray = null;
            try {
                byteArray = guid.getBytes("UTF-8");
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
            mGraphServiceClient.getMe().getDrive().getRoot().getChildren(guid).getContent().buildRequest().put(byteArray, new ICallback<DriveItem>() {

                @Override
                public void success(DriveItem driveItem) {
                    // This is the new content that we use to update the file
                    byte[] byteArray = null;
                    try {
                        byteArray = "A plain text file".getBytes("UTF-8");
                        mGraphServiceClient.getMe().getDrive().getItems().byId(driveItem.id).getContent().buildRequest().put(byteArray, new ICallback<DriveItem>() {

                            @Override
                            public void success(DriveItem driveItem) {
                                callback.success(driveItem.getRawObject());
                            }

                            @Override
                            public void failure(ClientException ex) {
                                callback.failure(ex);
                            }
                        });
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /*
                 * Delete the content of a file
                 * DELETE https://graph.microsoft.com/{version}/me/drive/items/{fileId}/
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/item_delete
                 */
    new DrivesSnippets<JsonObject>(delete_me_file) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            // create a new file to delete
            String guid = UUID.randomUUID().toString();
            byte[] byteArray = null;
            try {
                byteArray = guid.getBytes("UTF-8");
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
            mGraphServiceClient.getMe().getDrive().getRoot().getChildren(guid).getContent().buildRequest().put(byteArray, new ICallback<DriveItem>() {

                @Override
                public void success(DriveItem driveItem) {
                    mGraphServiceClient.getMe().getDrive().getItems().byId(driveItem.id).buildRequest().delete(new ICallback<Void>() {

                        @Override
                        public void success(Void aVoid) {
                            callback.success(null);
                        }

                        @Override
                        public void failure(ClientException ex) {
                            callback.failure(ex);
                        }
                    });
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /*
                 * Renames a file
                 * PATCH https://graph.microsoft.com/{version}/me/drive/items/{fileId}/
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/item_update
                 */
    new DrivesSnippets<JsonObject>(rename_me_file) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            // create a new file to rename
            String guid = UUID.randomUUID().toString();
            byte[] byteArray = null;
            try {
                byteArray = guid.getBytes("UTF-8");
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
            mGraphServiceClient.getMe().getDrive().getRoot().getChildren(guid).getContent().buildRequest().put(byteArray, new ICallback<DriveItem>() {

                @Override
                public void success(DriveItem driveItem) {
                    DriveItem renamedDriveItem = new DriveItem();
                    renamedDriveItem.name = "Updated name";
                    mGraphServiceClient.getMe().getDrive().getItems().byId(driveItem.id).buildRequest().patch(renamedDriveItem, new ICallback<DriveItem>() {

                        @Override
                        public void success(DriveItem driveItem) {
                            callback.success(driveItem.getRawObject());
                        }

                        @Override
                        public void failure(ClientException ex) {
                            callback.failure(ex);
                        }
                    });
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /*
                 * Creates a folder
                 * POST https://graph.microsoft.com/me/drive/root/children
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/item_post_children
                 */
    new DrivesSnippets<JsonObject>(create_me_folder) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            String guid = UUID.randomUUID().toString();
            DriveItem driveItem = new DriveItem();
            driveItem.name = guid;
            driveItem.folder = new Folder();
            mGraphServiceClient.getMe().getDrive().getRoot().getChildren().buildRequest().post(driveItem, new ICallback<DriveItem>() {

                @Override
                public void success(DriveItem driveItem) {
                    callback.success(driveItem.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    } };
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) Folder(com.microsoft.graph.extensions.Folder) DriveItem(com.microsoft.graph.extensions.DriveItem) IDriveItemCollectionPage(com.microsoft.graph.extensions.IDriveItemCollectionPage) ICallback(com.microsoft.graph.concurrency.ICallback) Drive(com.microsoft.graph.extensions.Drive) ClientException(com.microsoft.graph.core.ClientException)

Aggregations

JsonObject (com.google.gson.JsonObject)1 ICallback (com.microsoft.graph.concurrency.ICallback)1 ClientException (com.microsoft.graph.core.ClientException)1 Drive (com.microsoft.graph.extensions.Drive)1 DriveItem (com.microsoft.graph.extensions.DriveItem)1 Folder (com.microsoft.graph.extensions.Folder)1 IDriveItemCollectionPage (com.microsoft.graph.extensions.IDriveItemCollectionPage)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1