use of com.microsoft.azure.sdk.iot.service.ProxyOptions in project azure-iot-sdk-java by Azure.
the class DigitalTwinClientTests method getDigitalTwinWithProxy.
@Test
@StandardTierHubOnlyTest
public void getDigitalTwinWithProxy() {
// arrange
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(testProxyHostname, testProxyPort));
ProxyOptions proxyOptions = new ProxyOptions(proxy);
DigitalTwinClientOptions clientOptions = DigitalTwinClientOptions.builder().proxyOptions(proxyOptions).httpReadTimeout(0).build();
digitalTwinClient = new DigitalTwinClient(IOTHUB_CONNECTION_STRING, clientOptions);
// act
BasicDigitalTwin response = digitalTwinClient.getDigitalTwin(deviceId, BasicDigitalTwin.class);
ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> responseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(deviceId, BasicDigitalTwin.class);
// assert
assertEquals(response.getMetadata().getModelId(), E2ETestConstants.THERMOSTAT_MODEL_ID);
assertEquals(responseWithHeaders.body().getMetadata().getModelId(), E2ETestConstants.THERMOSTAT_MODEL_ID);
}
use of com.microsoft.azure.sdk.iot.service.ProxyOptions in project azure-iot-sdk-java by Azure.
the class JobClient method getJob.
/**
* Get the current job on the iotHub.
*
* @param jobId Unique Job Id for this job
* @return a jobResult object
* @throws IllegalArgumentException if the jobId is invalid
* @throws IOException if the function cannot create a URL for the job, or the IO failed on request
* @throws IotHubException if the http request failed
*/
public synchronized JobResult getJob(String jobId) throws IllegalArgumentException, IOException, IotHubException {
URL url;
if (Tools.isNullOrEmpty(jobId)) {
throw new IllegalArgumentException("jobId cannot be null or empty");
}
try {
url = IotHubConnectionString.getUrlJobs(this.hostName, jobId);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Invalid JobId to create url");
}
ProxyOptions proxyOptions = options.getProxyOptions();
Proxy proxy = proxyOptions != null ? proxyOptions.getProxy() : null;
HttpResponse response = DeviceOperations.request(this.getAuthenticationToken(), url, HttpMethod.GET, new byte[] {}, null, options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
return new JobResult(response.getBody());
}
use of com.microsoft.azure.sdk.iot.service.ProxyOptions in project azure-iot-sdk-java by Azure.
the class JobClient method queryJobResponse.
/**
* Query the iot hub for a jobs response. Query response are limited by page size per attempt
*
* @param jobType The type of job to query for
* @param jobStatus The status of the job to query for
* @param pageSize The value to which to limit the job response size by
* @return A query object on which to look for responses by
* @throws IOException If any of the input parameters are incorrect
* @throws IotHubException If IotHub failed to respond
*/
public synchronized Query queryJobResponse(JobType jobType, JobStatus jobStatus, Integer pageSize) throws IOException, IotHubException {
if (pageSize <= 0) {
throw new IllegalArgumentException("pagesize cannot be negative or zero");
}
Query jobResponseQuery = new Query(pageSize, QueryType.JOB_RESPONSE);
String jobTypeString = (jobType == null) ? null : jobType.toString();
String jobStatusString = (jobStatus == null) ? null : jobStatus.toString();
ProxyOptions proxyOptions = options.getProxyOptions();
Proxy proxy = proxyOptions != null ? proxyOptions.getProxy() : null;
jobResponseQuery.sendQueryRequest(this.credentialCache, this.azureSasCredential, this.iotHubConnectionString, IotHubConnectionString.getUrlQuery(this.hostName, jobTypeString, jobStatusString), HttpMethod.GET, options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
return jobResponseQuery;
}
use of com.microsoft.azure.sdk.iot.service.ProxyOptions in project azure-iot-sdk-java by Azure.
the class DeviceTwin method getTwinOperation.
private void getTwinOperation(URL url, DeviceTwinDevice device) throws IotHubException, IOException {
ProxyOptions proxyOptions = options.getProxyOptions();
Proxy proxy = proxyOptions != null ? proxyOptions.getProxy() : null;
HttpResponse response = DeviceOperations.request(this.getAuthenticationToken(), url, HttpMethod.GET, new byte[0], String.valueOf(requestId++), options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
String twin = new String(response.getBody(), StandardCharsets.UTF_8);
TwinState twinState = TwinState.createFromTwinJson(twin);
device.setVersion(twinState.getVersion());
device.setModelId(twinState.getModelId());
device.setETag(twinState.getETag());
device.setTags(twinState.getTags());
device.setDesiredProperties(twinState.getDesiredProperty());
device.setReportedProperties(twinState.getReportedProperty());
device.setCapabilities(twinState.getCapabilities());
device.setConfigurations(twinState.getConfigurations());
device.setConnectionState(twinState.getConnectionState());
}
use of com.microsoft.azure.sdk.iot.service.ProxyOptions in project azure-iot-sdk-java by Azure.
the class DeviceTwin method updateTwin.
/**
* This method updates device twin for the specified device.
* <p>This API uses the IoT Hub PATCH API when sending updates, but it sends the full twin with each patch update.
* As a result, devices subscribed to twin will receive notifications that each property is changed when this API is
* called, even if only some of the properties were changed.</p>
* <p>See <a href="https://docs.microsoft.com/en-us/rest/api/iothub/service/devices/updatetwin">PATCH</a> for
* more details.</p>
*
* @param device The device with a valid Id for which device twin is to be updated.
* @throws IOException This exception is thrown if the IO operation failed.
* @throws IotHubException This exception is thrown if the response verification failed.
*/
public synchronized void updateTwin(DeviceTwinDevice device) throws IotHubException, IOException {
if (device == null || device.getDeviceId() == null || device.getDeviceId().length() == 0) {
throw new IllegalArgumentException("Instantiate a device and set device Id to be used.");
}
if ((device.getDesiredMap() == null || device.getDesiredMap().isEmpty()) && (device.getTagsMap() == null || device.getTagsMap().isEmpty())) {
throw new IllegalArgumentException("Set either desired properties or tags for the device to be updated.");
}
URL url;
if ((device.getModuleId() == null) || device.getModuleId().length() == 0) {
url = IotHubConnectionString.getUrlTwin(this.hostName, device.getDeviceId());
} else {
url = IotHubConnectionString.getUrlModuleTwin(this.hostName, device.getDeviceId(), device.getModuleId());
}
TwinState twinState = new TwinState(device.getTagsMap(), device.getDesiredMap(), null);
String twinJson = twinState.toJsonElement().toString();
ProxyOptions proxyOptions = options.getProxyOptions();
Proxy proxy = proxyOptions != null ? proxyOptions.getProxy() : null;
DeviceOperations.request(this.getAuthenticationToken(), url, HttpMethod.PATCH, twinJson.getBytes(StandardCharsets.UTF_8), String.valueOf(requestId++), options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
}
Aggregations