Search in sources :

Example 11 with ICallback

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

the class UsersSnippets method getUsersSnippets.

static UsersSnippets[] getUsersSnippets() {
    return new UsersSnippets[] { // Marker element
    new UsersSnippets(null) {

        @Override
        public void request(ICallback callback) {
        // Not implemented
        }
    }, /*
                 * Gets all of the users in your tenant\'s directory.
                 * HTTP GET https://graph.microsoft.com/{version}/myOrganization/users
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/user_list
                 */
    new UsersSnippets<JsonObject>(get_organization_users) {

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

                @Override
                public void success(IUserCollectionPage iUserCollectionPage) {
                    callback.success(iUserCollectionPage.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /*
                 * Gets all of the users in your tenant's directory who are from the United States, using $filter.
                 * HTTP GET https://graph.microsoft.com/{version}/myOrganization/users?$filter=country eq \'United States\'
                 * @see http://graph.microsoft.io/docs/overview/query_parameters
                 */
    new UsersSnippets<JsonObject>(get_organization_filtered_users) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            final List<Option> options = new LinkedList<>();
            options.add(new QueryOption("$filter", "country eq 'United States'"));
            mGraphServiceClient.getUsers().buildRequest(options).get(new ICallback<IUserCollectionPage>() {

                @Override
                public void success(IUserCollectionPage iUserCollectionPage) {
                    callback.success(iUserCollectionPage.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /*
                 * Adds a new user to the tenant's directory
                 * HTTP POST https://graph.microsoft.com/{version}/myOrganization/users
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/user_post_users
                 */
    new UsersSnippets<JsonObject>(insert_organization_user) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            // Use a random UUID for the user name
            String randomUserName = UUID.randomUUID().toString();
            // create the user
            User user = new User();
            user.accountEnabled = true;
            user.displayName = "SAMPLE " + randomUserName;
            user.mailNickname = randomUserName;
            // get the tenant from preferences
            SharedPreferences prefs = SharedPrefsUtil.getSharedPreferences();
            String tenant = prefs.getString(PREF_USER_TENANT, "");
            user.userPrincipalName = randomUserName + "@" + tenant;
            // initialize a password & say whether or not the user must change it
            PasswordProfile password = new PasswordProfile();
            password.password = UUID.randomUUID().toString().substring(0, 16);
            password.forceChangePasswordNextSignIn = false;
            user.passwordProfile = password;
            mGraphServiceClient.getUsers().buildRequest().post(user, new ICallback<User>() {

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

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    } };
}
Also used : User(com.microsoft.graph.extensions.User) IUserCollectionPage(com.microsoft.graph.extensions.IUserCollectionPage) SharedPreferences(android.content.SharedPreferences) QueryOption(com.microsoft.graph.options.QueryOption) LinkedList(java.util.LinkedList) PasswordProfile(com.microsoft.graph.extensions.PasswordProfile) ICallback(com.microsoft.graph.concurrency.ICallback) QueryOption(com.microsoft.graph.options.QueryOption) Option(com.microsoft.graph.options.Option) ClientException(com.microsoft.graph.core.ClientException)

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