Search in sources :

Example 6 with ICallback

use of com.microsoft.graph.concurrency.ICallback in project msgraph-sdk-java by microsoftgraph.

the class GraphServiceClientTest method testOverrideOfDefaultExecutors.

@Test
public void testOverrideOfDefaultExecutors() {
    IExecutors ex = new IExecutors() {

        @Override
        public void performOnBackground(Runnable runnable) {
        // do nothing
        }

        @Override
        public <Result> void performOnForeground(Result result, ICallback<Result> callback) {
        // do nothing
        }

        @Override
        public <Result> void performOnForeground(int progress, int progressMax, IProgressCallback<Result> callback) {
        // do nothing
        }

        @Override
        public <Result> void performOnForeground(ClientException exception, ICallback<Result> callback) {
        // do nothing
        }
    };
    IGraphServiceClient client = // 
    GraphServiceClient.builder().authenticationProvider(// 
    auth).executors(// 
    ex).buildClient();
    assertEquals(ex, client.getExecutors());
    assertEquals(auth, client.getAuthenticationProvider());
    assertNotNull(client.getHttpProvider());
    assertNotNull(client.getLogger());
    assertNotNull(client.getSerializer());
    assertEquals(ex, ((DefaultHttpProvider) client.getHttpProvider()).getExecutors());
}
Also used : ICallback(com.microsoft.graph.concurrency.ICallback) IExecutors(com.microsoft.graph.concurrency.IExecutors) ClientException(com.microsoft.graph.core.ClientException) IProgressCallback(com.microsoft.graph.concurrency.IProgressCallback) IGraphServiceClient(com.microsoft.graph.models.extensions.IGraphServiceClient) Test(org.junit.Test)

Example 7 with ICallback

use of com.microsoft.graph.concurrency.ICallback in project msgraph-sdk-java by microsoftgraph.

the class BaseStreamRequestTests method testSendWithContentAndCallback.

@Test
public void testSendWithContentAndCallback() {
    final ITestConnectionData data = new ITestConnectionData() {

        @Override
        public int getRequestCode() {
            return 200;
        }

        @Override
        public String getJsonResponse() {
            return "{ \"id\": \"zzz\" }";
        }

        @Override
        public Map<String, String> getHeaders() {
            final HashMap<String, String> map = new HashMap<>();
            map.put("Content-Type", "application/json");
            return map;
        }
    };
    DefaultHttpProvider mProvider = new DefaultHttpProvider(new MockSerializer(null, ""), mAuthenticationProvider, new MockExecutors(), new MockLogger());
    mProvider.setConnectionFactory(new MockConnectionFactory(new MockConnection(data)));
    mBaseClient.setHttpProvider(mProvider);
    final AtomicBoolean success = new AtomicBoolean(false);
    final AtomicBoolean failure = new AtomicBoolean(false);
    final ICallback<InputStream> callback = new ICallback<InputStream>() {

        @Override
        public void success(InputStream inputStream) {
            success.set(true);
        }

        @Override
        public void failure(ClientException ex) {
            failure.set(true);
        }
    };
    final BaseStreamRequest<InputStream> request = new BaseStreamRequest<InputStream>("https://a.b.c", mBaseClient, null, InputStream.class) {
    };
    request.send(new byte[] { 1, 2, 3, 4 }, callback);
    assertTrue(success.get());
    assertFalse(failure.get());
    assertEquals(1, mAuthenticationProvider.getInterceptionCount());
}
Also used : MockSerializer(com.microsoft.graph.serializer.MockSerializer) HashMap(java.util.HashMap) InputStream(java.io.InputStream) MockExecutors(com.microsoft.graph.concurrency.MockExecutors) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ICallback(com.microsoft.graph.concurrency.ICallback) MockLogger(com.microsoft.graph.logger.MockLogger) ClientException(com.microsoft.graph.core.ClientException) Test(org.junit.Test)

Example 8 with ICallback

use of com.microsoft.graph.concurrency.ICallback in project msgraph-sdk-java by microsoftgraph.

the class BaseStreamRequestTests method testSendWithCallback.

@Test
public void testSendWithCallback() {
    final ITestConnectionData data = new ITestConnectionData() {

        @Override
        public int getRequestCode() {
            return 200;
        }

        @Override
        public String getJsonResponse() {
            return "{ \"id\": \"zzz\" }";
        }

        @Override
        public Map<String, String> getHeaders() {
            final HashMap<String, String> map = new HashMap<>();
            map.put("Content-Type", "application/json");
            return map;
        }
    };
    DefaultHttpProvider mProvider = new DefaultHttpProvider(new MockSerializer(null, ""), mAuthenticationProvider, new MockExecutors(), new MockLogger());
    mProvider.setConnectionFactory(new MockConnectionFactory(new MockConnection(data)));
    mBaseClient.setHttpProvider(mProvider);
    final AtomicBoolean success = new AtomicBoolean(false);
    final AtomicBoolean failure = new AtomicBoolean(false);
    final ICallback<InputStream> callback = new ICallback<InputStream>() {

        @Override
        public void success(InputStream inputStream) {
            success.set(true);
        }

        @Override
        public void failure(ClientException ex) {
            failure.set(true);
        }
    };
    final BaseStreamRequest<InputStream> request = new BaseStreamRequest<InputStream>("https://a.b.c", mBaseClient, null, InputStream.class) {
    };
    request.send(callback);
    assertTrue(success.get());
    assertFalse(failure.get());
    assertEquals(1, mAuthenticationProvider.getInterceptionCount());
}
Also used : MockSerializer(com.microsoft.graph.serializer.MockSerializer) HashMap(java.util.HashMap) InputStream(java.io.InputStream) MockExecutors(com.microsoft.graph.concurrency.MockExecutors) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ICallback(com.microsoft.graph.concurrency.ICallback) MockLogger(com.microsoft.graph.logger.MockLogger) ClientException(com.microsoft.graph.core.ClientException) Test(org.junit.Test)

Example 9 with ICallback

use of com.microsoft.graph.concurrency.ICallback 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)

Example 10 with ICallback

use of com.microsoft.graph.concurrency.ICallback in project android-java-snippets-sample by microsoftgraph.

the class MeSnippets method getMeSnippets.

static MeSnippets[] getMeSnippets() {
    return new MeSnippets[] { // Marker element
    new MeSnippets(null) {

        @Override
        public void request(ICallback callback) {
        // Not implemented
        }
    }, /* Get information about signed in user
                 * HTTP GET https://graph.microsoft.com/{version}/me
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/user_get
                 */
    new MeSnippets<JsonObject>(get_me) {

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

                @Override
                public void success(User user) {
                    callback.success(user.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /* Get responsibilities of signed in user
                 * HTTP GET https://graph.microsoft.com/{version}/me?$select=AboutMe,Responsibilities,Tags
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/resources/user
                 */
    new MeSnippets<JsonObject>(get_me_responsibilities) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            final List<Option> options = new LinkedList<>();
            options.add(new QueryOption("$select", "AboutMe,Responsibilities,Tags"));
            mGraphServiceClient.getMe().buildRequest(options).get(new ICallback<User>() {

                @Override
                public void success(User user) {
                    callback.success(user.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /* Get the user's manager
                 * HTTP GET https://graph.microsoft.com/{version}/me/manager
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/resources/user
                 */
    new MeSnippets<JsonObject>(get_me_manager) {

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

                @Override
                public void success(DirectoryObject directoryObject) {
                    callback.success(directoryObject.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /* Get the user's direct reports
                 * HTTP GET https://graph.microsoft.com/{version}/me/directReports
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/resources/user
                 */
    new MeSnippets<JsonObject>(get_me_direct_reports) {

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

                @Override
                public void success(IDirectoryObjectCollectionWithReferencesPage iDirectoryObjectCollectionWithReferencesPage) {
                    callback.success(iDirectoryObjectCollectionWithReferencesPage.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /* Get the group membership of the user
                 * HTTP GET https://graph.microsoft.com/{version}/me/memberOf
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/resources/user
                 */
    new MeSnippets<JsonObject>(get_me_group_membership) {

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

                @Override
                public void success(IDirectoryObjectCollectionWithReferencesPage iDirectoryObjectCollectionWithReferencesPage) {
                    callback.success(iDirectoryObjectCollectionWithReferencesPage.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /* Get the photo of the user
                 * HTTP GET https://graph.microsoft.com/{version}/me/userPhoto
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/resources/user
                 */
    new MeSnippets<JsonObject>(get_me_photo) {

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

                @Override
                public void success(ProfilePhoto profilePhoto) {
                    callback.success(profilePhoto.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    } };
}
Also used : User(com.microsoft.graph.extensions.User) QueryOption(com.microsoft.graph.options.QueryOption) ProfilePhoto(com.microsoft.graph.extensions.ProfilePhoto) LinkedList(java.util.LinkedList) ICallback(com.microsoft.graph.concurrency.ICallback) IDirectoryObjectCollectionWithReferencesPage(com.microsoft.graph.extensions.IDirectoryObjectCollectionWithReferencesPage) QueryOption(com.microsoft.graph.options.QueryOption) Option(com.microsoft.graph.options.Option) ClientException(com.microsoft.graph.core.ClientException) DirectoryObject(com.microsoft.graph.extensions.DirectoryObject)

Aggregations

ICallback (com.microsoft.graph.concurrency.ICallback)11 ClientException (com.microsoft.graph.core.ClientException)10 Test (org.junit.Test)5 InputStream (java.io.InputStream)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 JsonObject (com.google.gson.JsonObject)2 MockExecutors (com.microsoft.graph.concurrency.MockExecutors)2 IDirectoryObjectCollectionWithReferencesPage (com.microsoft.graph.extensions.IDirectoryObjectCollectionWithReferencesPage)2 User (com.microsoft.graph.extensions.User)2 MockLogger (com.microsoft.graph.logger.MockLogger)2 IGraphServiceClient (com.microsoft.graph.models.extensions.IGraphServiceClient)2 Option (com.microsoft.graph.options.Option)2 QueryOption (com.microsoft.graph.options.QueryOption)2 MockSerializer (com.microsoft.graph.serializer.MockSerializer)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 SharedPreferences (android.content.SharedPreferences)1 IExecutors (com.microsoft.graph.concurrency.IExecutors)1 IProgressCallback (com.microsoft.graph.concurrency.IProgressCallback)1 DirectoryObject (com.microsoft.graph.extensions.DirectoryObject)1