Search in sources :

Example 1 with ApiKey

use of com.arm.mbed.cloud.sdk.accountmanagement.model.ApiKey in project mbed-cloud-sdk-java by ARMmbed.

the class ApiKeyAdapter method map.

/**
 * Maps API keys.
 *
 * @param apiKeyInfo
 *            an API key.
 * @return an API Key.
 */
public static ApiKey map(ApiKeyInfoResp apiKeyInfo) {
    if (apiKeyInfo == null) {
        return null;
    }
    final ApiKey apiKey = new ApiKey(apiKeyInfo.getId(), apiKeyInfo.getKey(), TranslationUtils.toDate(apiKeyInfo.getCreatedAt()), TranslationUtils.toTimeStamp(apiKeyInfo.getCreationTime()), TranslationUtils.toTimeStamp(apiKeyInfo.getLastLoginTime()));
    apiKey.setName(apiKeyInfo.getName());
    apiKey.setOwnerId(apiKeyInfo.getOwner());
    apiKey.setGroup(apiKeyInfo.getGroups());
    apiKey.setStatus(toStatus(apiKeyInfo.getStatus()));
    return apiKey;
}
Also used : ApiKey(com.arm.mbed.cloud.sdk.accountmanagement.model.ApiKey)

Example 2 with ApiKey

use of com.arm.mbed.cloud.sdk.accountmanagement.model.ApiKey in project mbed-cloud-sdk-java by ARMmbed.

the class AccountManagement method updateApiKey.

/**
 * Updates an API key.
 * <p>
 * Example:
 *
 * <pre>
 * {@code
 * try {
 *     ApiKey apiKey = new ApiKey();
 *     String apiKeyId = "015f4ac587f500000000000100100249";
 *     apiKey.setId(apiKeyId);
 *     apiKey.setName("NewKeyName");
 *
 *     ApiKey newApiKey = accountManagementApi.updateApiKey(apiKey);
 *     System.out.println("New Api Key name: " + newApiKey.getName());
 *     assert apiKeyId == newApiKey.getId();
 *
 * } catch (MbedCloudException e) {
 *     e.printStackTrace();
 * }
 * }
 * </pre>
 *
 * @param apiKey
 *            The API key to update.
 * @return updated API key.
 * @throws MbedCloudException
 *             if a problem occurred during request processing.
 */
@API
@Nullable
public ApiKey updateApiKey(@NonNull ApiKey apiKey) throws MbedCloudException {
    checkNotNull(apiKey, TAG_API_KEY);
    checkNotNull(apiKey.getId(), TAG_API_KEY_UUID);
    checkModelValidity(apiKey, TAG_API_KEY);
    ApiKey updatedApiKey = null;
    if (apiKey.hasStatusBeenUpdated()) {
        updatedApiKey = apiKey;
    } else {
        updatedApiKey = apiKey.clone();
        updatedApiKey.setStatus(null);
    }
    final ApiKey finalApiKey = updatedApiKey;
    return CloudCaller.call(this, "updateApiKey()", ApiKeyAdapter.getMapper(), new CloudCall<ApiKeyInfoResp>() {

        @Override
        public Call<ApiKeyInfoResp> call() {
            return endpoint.getDeveloper().updateApiKey(finalApiKey.getId(), ApiKeyAdapter.reverseMapUpdate(finalApiKey));
        }
    });
}
Also used : CloudCall(com.arm.mbed.cloud.sdk.common.CloudCaller.CloudCall) Call(retrofit2.Call) ApiKey(com.arm.mbed.cloud.sdk.accountmanagement.model.ApiKey) ApiKeyInfoResp(com.arm.mbed.cloud.sdk.internal.iam.model.ApiKeyInfoResp) API(com.arm.mbed.cloud.sdk.annotations.API) Nullable(com.arm.mbed.cloud.sdk.annotations.Nullable)

Example 3 with ApiKey

use of com.arm.mbed.cloud.sdk.accountmanagement.model.ApiKey in project mbed-cloud-sdk-java by ARMmbed.

the class AccountManagement method addApiKey.

/**
 * Adds an API key.
 * <p>
 * Example:
 *
 * <pre>
 * {@code
 * try {
 *     ApiKey apiKey = new ApiKey();
 *     apiKey.setName("QuickstartKey");
 *
 *     ApiKey newApiKey = accountManagementApi.addApiKey(apiKey);
 *     System.out.println("Api Key: " + newApiKey.getKey());
 *
 * } catch (MbedCloudException e) {
 *     e.printStackTrace();
 * }
 * }
 * </pre>
 *
 * @param apiKey
 *            The API key to add.
 * @return added API Key.
 * @throws MbedCloudException
 *             if a problem occurred during request processing.
 */
@API
@NonNull
public ApiKey addApiKey(@NonNull ApiKey apiKey) throws MbedCloudException {
    checkNotNull(apiKey, TAG_API_KEY);
    checkModelValidity(apiKey, TAG_API_KEY);
    final ApiKey finalApiKey = apiKey;
    return CloudCaller.call(this, "addApiKey()", ApiKeyAdapter.getMapper(), new CloudCall<ApiKeyInfoResp>() {

        @Override
        public Call<ApiKeyInfoResp> call() {
            return endpoint.getDeveloper().createApiKey(ApiKeyAdapter.reverseMapAdd(finalApiKey));
        }
    });
}
Also used : CloudCall(com.arm.mbed.cloud.sdk.common.CloudCaller.CloudCall) Call(retrofit2.Call) ApiKey(com.arm.mbed.cloud.sdk.accountmanagement.model.ApiKey) ApiKeyInfoResp(com.arm.mbed.cloud.sdk.internal.iam.model.ApiKeyInfoResp) NonNull(com.arm.mbed.cloud.sdk.annotations.NonNull) API(com.arm.mbed.cloud.sdk.annotations.API)

Example 4 with ApiKey

use of com.arm.mbed.cloud.sdk.accountmanagement.model.ApiKey in project mbed-cloud-sdk-java by ARMmbed.

the class AccountManagementExamples method listGroups.

/**
 * Lists the last 5 groups and their contents.
 */
@SuppressWarnings("boxing")
@Example
public void listGroups() {
    ConnectionOptions config = Configuration.get();
    AccountManagement api = new AccountManagement(config);
    try {
        // Defining query options
        GroupListOptions options = new GroupListOptions();
        options.setLimit(5);
        options.setOrder(Order.DESC);
        // Listing groups.
        ListResponse<Group> groups = api.listGroups(options);
        for (Group group : groups.getData()) {
            log("Group", group);
            Paginator<ApiKey> apiKeyIterator = api.listAllGroupApiKeys(group.getId(), null);
            while (apiKeyIterator.hasNext()) {
                log("API key of group [" + group.getId() + "]", apiKeyIterator.next());
            }
            Paginator<User> userIterator = api.listAllGroupUsers(group.getId(), null);
            while (userIterator.hasNext()) {
                log("User of group [" + group.getId() + "]", userIterator.next());
            }
        }
    } catch (Exception e) {
        logError("last API Metadata", api.getLastApiMetadata());
        fail(e.getMessage());
    }
}
Also used : Group(com.arm.mbed.cloud.sdk.accountmanagement.model.Group) ApiKey(com.arm.mbed.cloud.sdk.accountmanagement.model.ApiKey) User(com.arm.mbed.cloud.sdk.accountmanagement.model.User) GroupListOptions(com.arm.mbed.cloud.sdk.accountmanagement.model.GroupListOptions) ConnectionOptions(com.arm.mbed.cloud.sdk.common.ConnectionOptions) AccountManagement(com.arm.mbed.cloud.sdk.AccountManagement) AbstractExample(utils.AbstractExample) Example(utils.Example)

Example 5 with ApiKey

use of com.arm.mbed.cloud.sdk.accountmanagement.model.ApiKey in project mbed-cloud-sdk-java by ARMmbed.

the class AccountManagementExamples method listApiKeys.

/**
 * Lists the first 5 API Keys.
 */
@SuppressWarnings("boxing")
@Example
public void listApiKeys() {
    ConnectionOptions config = Configuration.get();
    AccountManagement api = new AccountManagement(config);
    try {
        // Defining query options
        ApiKeyListOptions options = new ApiKeyListOptions();
        options.setLimit(5);
        // Listing API keys.
        Paginator<ApiKey> apikeys = api.listAllApiKeys(options);
        for (ApiKey apiKey : apikeys) {
            log("API key", apiKey);
        }
    } catch (Exception e) {
        logError("last API Metadata", api.getLastApiMetadata());
        fail(e.getMessage());
    }
}
Also used : ApiKey(com.arm.mbed.cloud.sdk.accountmanagement.model.ApiKey) ApiKeyListOptions(com.arm.mbed.cloud.sdk.accountmanagement.model.ApiKeyListOptions) ConnectionOptions(com.arm.mbed.cloud.sdk.common.ConnectionOptions) AccountManagement(com.arm.mbed.cloud.sdk.AccountManagement) AbstractExample(utils.AbstractExample) Example(utils.Example)

Aggregations

ApiKey (com.arm.mbed.cloud.sdk.accountmanagement.model.ApiKey)5 AccountManagement (com.arm.mbed.cloud.sdk.AccountManagement)2 API (com.arm.mbed.cloud.sdk.annotations.API)2 CloudCall (com.arm.mbed.cloud.sdk.common.CloudCaller.CloudCall)2 ConnectionOptions (com.arm.mbed.cloud.sdk.common.ConnectionOptions)2 ApiKeyInfoResp (com.arm.mbed.cloud.sdk.internal.iam.model.ApiKeyInfoResp)2 Call (retrofit2.Call)2 AbstractExample (utils.AbstractExample)2 Example (utils.Example)2 ApiKeyListOptions (com.arm.mbed.cloud.sdk.accountmanagement.model.ApiKeyListOptions)1 Group (com.arm.mbed.cloud.sdk.accountmanagement.model.Group)1 GroupListOptions (com.arm.mbed.cloud.sdk.accountmanagement.model.GroupListOptions)1 User (com.arm.mbed.cloud.sdk.accountmanagement.model.User)1 NonNull (com.arm.mbed.cloud.sdk.annotations.NonNull)1 Nullable (com.arm.mbed.cloud.sdk.annotations.Nullable)1