Search in sources :

Example 16 with IotHubServiceSasToken

use of com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken in project azure-iot-sdk-java by Azure.

the class RegistryManager method getStatistics.

/**
     * Get device statistics
     *
     * @return RegistryStatistics object containing the requested data
     * @throws IOException This exception is thrown if the IO operation failed
     * @throws IotHubException This exception is thrown if the response verification failed
     */
public RegistryStatistics getStatistics() throws IOException, IotHubException, JsonSyntaxException {
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_054: [The function shall get the URL for the device]
    URL url = iotHubConnectionString.getUrlDeviceStatistics();
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_055: [The function shall create a new SAS token for the device]
    String sasTokenString = new IotHubServiceSasToken(this.iotHubConnectionString).toString();
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_056: [The function shall create a new HttpRequest for getting statistics a device from IotHub]
    HttpRequest request = CreateRequest(url, HttpMethod.GET, new byte[0], sasTokenString);
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_057: [The function shall send the created request and get the response]
    HttpResponse response = request.send();
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_058: [The function shall verify the response status and throw proper Exception]
    IotHubExceptionManager.httpResponseVerification(response);
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_059: [The function shall create a new RegistryStatistics object from the response and return with it]
    String bodyStr = new String(response.getBody(), StandardCharsets.UTF_8);
    RegistryStatistics registryStatistics = gson.fromJson(bodyStr, RegistryStatistics.class);
    return registryStatistics;
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) URL(java.net.URL)

Aggregations

IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)16 HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)10 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)10 URL (java.net.URL)9 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)6 Test (org.junit.Test)5 Expectations (mockit.Expectations)2 IotHubServiceClientProtocol (com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol)1 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)1 AmqpSend (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSend)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 URLEncoder (java.net.URLEncoder)1 ArrayList (java.util.ArrayList)1 Mac (javax.crypto.Mac)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1 JsonArray (javax.json.JsonArray)1 JsonObject (javax.json.JsonObject)1 JsonReader (javax.json.JsonReader)1 Base64 (org.apache.commons.codec.binary.Base64)1