Search in sources :

Example 1 with EnrollmentGroup

use of com.microsoft.azure.sdk.iot.provisioning.service.configs.EnrollmentGroup in project azure-iot-sdk-java by Azure.

the class EnrollmentGroupManager method createOrUpdate.

/**
 * Create or update an enrollmentGroup record.
 *
 * @param enrollmentGroup is an {@link EnrollmentGroup} that describes the enrollmentGroup that will be created or
 *                        updated. It cannot be {@code null}.
 * @return a {@link EnrollmentGroup} with the result of the creation or update request.
 * @throws IllegalArgumentException if the provided parameter is not correct.
 * @throws ProvisioningServiceClientTransportException if the SDK failed to send the request to the Device Provisioning Service.
 * @throws ProvisioningServiceClientException if the Device Provisioning Service was not able to create or update the enrollmentGroup.
 */
EnrollmentGroup createOrUpdate(EnrollmentGroup enrollmentGroup) throws ProvisioningServiceClientException {
    /* SRS_ENROLLMENT_GROUP_MANAGER_21_005: [The createOrUpdate shall throw IllegalArgumentException if the provided enrollmentGroup is null.] */
    if (enrollmentGroup == null) {
        throw new IllegalArgumentException("enrollmentGroup cannot be null.");
    }
    /* SRS_ENROLLMENT_GROUP_MANAGER_21_006: [The createOrUpdate shall send a Http request for the path `enrollmentGroups/[enrollmentGroupId]`.] */
    String id = enrollmentGroup.getEnrollmentGroupId();
    String enrollmentGroupPath = EnrollmentGroupManager.getEnrollmentGroupPath(id);
    /* SRS_ENROLLMENT_GROUP_MANAGER_21_007: [The createOrUpdate shall send a Http request with a body with the enrollmentGroup content in JSON format.] */
    String enrollmentGroupPayload = enrollmentGroup.toJson();
    /* SRS_ENROLLMENT_GROUP_MANAGER_21_045: [If the enrollmentGroup contains eTag, the createOrUpdate shall send a Http request with `If-Match` the eTag in the header.] */
    Map<String, String> headerParameters = new HashMap<>();
    if (!Tools.isNullOrEmpty(enrollmentGroup.getEtag())) {
        headerParameters.put(CONDITION_KEY, enrollmentGroup.getEtag());
    }
    /* SRS_ENROLLMENT_GROUP_MANAGER_21_008: [The createOrUpdate shall send a Http request with a Http verb `PUT`.] */
    /* SRS_ENROLLMENT_GROUP_MANAGER_21_009: [The createOrUpdate shall throw ProvisioningServiceClientTransportException if the request failed. Threw by the callee.] */
    /* SRS_ENROLLMENT_GROUP_MANAGER_21_010: [The createOrUpdate shall throw ProvisioningServiceClientException if the Device Provisioning Service could not successfully execute the request. Threw by the callee.] */
    HttpResponse httpResponse = contractApiHttp.request(HttpMethod.PUT, enrollmentGroupPath, headerParameters, enrollmentGroupPayload);
    /* SRS_ENROLLMENT_GROUP_MANAGER_21_042: [The createOrUpdate shall throw ProvisioningServiceClientServiceException if the heepResponse contains a null body.] */
    byte[] body = httpResponse.getBody();
    if (body == null) {
        throw new ProvisioningServiceClientServiceException("Http response for createOrUpdate cannot contains a null body");
    }
    /* SRS_ENROLLMENT_GROUP_MANAGER_21_011: [The createOrUpdate shall return an EnrollmentGroup object created from the body of the response for the Http request .] */
    return new EnrollmentGroup(new String(body, StandardCharsets.UTF_8));
}
Also used : ProvisioningServiceClientServiceException(com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientServiceException) HashMap(java.util.HashMap) EnrollmentGroup(com.microsoft.azure.sdk.iot.provisioning.service.configs.EnrollmentGroup) HttpResponse(com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse)

Example 2 with EnrollmentGroup

use of com.microsoft.azure.sdk.iot.provisioning.service.configs.EnrollmentGroup in project azure-iot-sdk-java by Azure.

the class EnrollmentGroupManager method get.

/**
 * Get enrollmentGroup information.
 *
 * @see ProvisioningServiceClient#getEnrollmentGroup(String)
 *
 * @param enrollmentGroupId the {@code String} that identifies the enrollmentGroup. It cannot be {@code null} or empty.
 * @return An {@link EnrollmentGroup} with the enrollmentGroup information.
 * @throws IllegalArgumentException if the provided parameter is not correct.
 * @throws ProvisioningServiceClientTransportException if the SDK failed to send the request to the Device Provisioning Service.
 * @throws ProvisioningServiceClientException if the Device Provisioning Service was not able to execute the get operation.
 */
EnrollmentGroup get(String enrollmentGroupId) throws ProvisioningServiceClientException {
    /* SRS_ENROLLMENT_GROUP_MANAGER_21_020: [The get shall throw IllegalArgumentException if the provided enrollmentGroupId is null or empty.] */
    if (Tools.isNullOrEmpty(enrollmentGroupId)) {
        throw new IllegalArgumentException("enrollmentGroupId cannot be null or empty.");
    }
    /* SRS_ENROLLMENT_GROUP_MANAGER_21_021: [The get shall send a Http request for the path `enrollmentGroups/[enrollmentGroupId]`.] */
    String enrollmentGroupPath = EnrollmentGroupManager.getEnrollmentGroupPath(enrollmentGroupId);
    /* SRS_ENROLLMENT_GROUP_MANAGER_21_022: [The get shall send a Http request with a Http verb `GET`.] */
    /* SRS_ENROLLMENT_GROUP_MANAGER_21_023: [The get shall throw ProvisioningServiceClientTransportException if the request failed. Threw by the callee.] */
    /* SRS_ENROLLMENT_GROUP_MANAGER_21_024: [The get shall throw ProvisioningServiceClientException if the Device Provisioning Service could not successfully execute the request. Threw by the callee.] */
    HttpResponse httpResponse = contractApiHttp.request(HttpMethod.GET, enrollmentGroupPath, null, "");
    /* SRS_ENROLLMENT_GROUP_MANAGER_21_043: [The get shall throw ProvisioningServiceClientServiceException if the heepResponse contains a null body.] */
    byte[] body = httpResponse.getBody();
    if (body == null) {
        throw new ProvisioningServiceClientServiceException("Http response for get cannot contains a null body");
    }
    /* SRS_ENROLLMENT_GROUP_MANAGER_21_025: [The get shall return an EnrollmentGroup object created from the body of the response for the Http request .] */
    return new EnrollmentGroup(new String(body, StandardCharsets.UTF_8));
}
Also used : ProvisioningServiceClientServiceException(com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientServiceException) EnrollmentGroup(com.microsoft.azure.sdk.iot.provisioning.service.configs.EnrollmentGroup) HttpResponse(com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse)

Aggregations

HttpResponse (com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse)2 EnrollmentGroup (com.microsoft.azure.sdk.iot.provisioning.service.configs.EnrollmentGroup)2 ProvisioningServiceClientServiceException (com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientServiceException)2 HashMap (java.util.HashMap)1