use of com.azure.core.http.HttpResponse in project vividus by vividus-framework.
the class AbstractAzureResourceManagementSteps method saveHttpResponseAsVariable.
protected void saveHttpResponseAsVariable(String urlPath, String apiVersion, Set<VariableScope> scopes, String variableName) {
// Workaround for https://github.com/Azure/azure-sdk-for-java/issues/27268
String url = resourceManagerEndpoint + urlPath + "?api-version=" + apiVersion;
HttpRequest httpRequest = new HttpRequest(HttpMethod.GET, url);
try (HttpResponse httpResponse = httpPipeline.send(httpRequest).block()) {
String responseBody = httpResponse.getBodyAsString().block();
if (httpResponse.getStatusCode() == HttpResponseStatus.OK.code()) {
variableContext.putVariable(scopes, variableName, responseBody);
} else {
softAssert.recordFailedAssertion("Azure REST API HTTP request execution is failed: " + responseBody);
}
}
}
use of com.azure.core.http.HttpResponse in project ApplicationInsights-Java by microsoft.
the class QuickPulseNetworkHelperTest method testIsSuccessWith200.
@Test
void testIsSuccessWith200() {
HttpResponse response = mock(HttpResponse.class);
Mockito.doReturn(200).when(response).getStatusCode();
boolean result = new QuickPulseNetworkHelper().isSuccess(response);
assertThat(result).isTrue();
}
use of com.azure.core.http.HttpResponse in project ApplicationInsights-Java by microsoft.
the class QuickPulseNetworkHelperTest method testIsSuccessWith500.
@Test
void testIsSuccessWith500() {
HttpResponse response = mock(HttpResponse.class);
Mockito.doReturn(500).when(response).getStatusCode();
boolean result = new QuickPulseNetworkHelper().isSuccess(response);
assertThat(result).isFalse();
}
use of com.azure.core.http.HttpResponse in project ApplicationInsights-Java by microsoft.
the class QuickPulsePingSender method ping.
public QuickPulseHeaderInfo ping(String redirectedEndpoint) {
String instrumentationKey = getInstrumentationKey();
if (Strings.isNullOrEmpty(instrumentationKey)) {
// turn off quick pulse.
return new QuickPulseHeaderInfo(QuickPulseStatus.QP_IS_OFF);
}
Date currentDate = new Date();
String endpointPrefix = Strings.isNullOrEmpty(redirectedEndpoint) ? getQuickPulseEndpoint() : redirectedEndpoint;
HttpRequest request = networkHelper.buildPingRequest(currentDate, getQuickPulsePingUri(endpointPrefix), quickPulseId, machineName, telemetryClient.getRoleName(), instanceName);
long sendTime = System.nanoTime();
HttpResponse response = null;
try {
request.setBody(buildPingEntity(currentDate.getTime()));
response = httpPipeline.send(request).block();
if (response == null) {
// this shouldn't happen, the mono should complete with a response or a failure
throw new AssertionError("http response mono returned empty");
}
// response body is not consumed below
LazyHttpClient.consumeResponseBody(response);
if (networkHelper.isSuccess(response)) {
QuickPulseHeaderInfo quickPulseHeaderInfo = networkHelper.getQuickPulseHeaderInfo(response);
switch(quickPulseHeaderInfo.getQuickPulseStatus()) {
case QP_IS_OFF:
case QP_IS_ON:
lastValidTransmission = sendTime;
operationLogger.recordSuccess();
return quickPulseHeaderInfo;
default:
break;
}
}
} catch (Throwable t) {
if (!NetworkFriendlyExceptions.logSpecialOneTimeFriendlyException(t, getQuickPulseEndpoint(), friendlyExceptionThrown, logger)) {
operationLogger.recordFailure(t.getMessage(), t);
}
} finally {
if (response != null) {
response.close();
}
}
return onPingError(sendTime);
}
use of com.azure.core.http.HttpResponse in project ApplicationInsights-Java by microsoft.
the class AzureMetadataService method run.
@Override
public void run() {
HttpRequest request = new HttpRequest(HttpMethod.GET, ENDPOINT);
request.setHeader("Metadata", "true");
HttpResponse response;
try {
response = LazyHttpClient.getInstance().send(request).block();
} catch (RuntimeException e) {
logger.debug("Shutting down AzureMetadataService scheduler: is not running on Azure VM or VMSS");
logger.trace(e.getMessage(), e);
scheduledExecutor.shutdown();
return;
}
if (response == null) {
// this shouldn't happen, the mono should complete with a response or a failure
throw new AssertionError("http response mono returned empty");
}
String json = response.getBodyAsString().block();
if (json == null) {
// this shouldn't happen, the mono should complete with a response or a failure
throw new AssertionError("response body mono returned empty");
}
MetadataInstanceResponse metadataInstanceResponse;
try {
metadataInstanceResponse = mapper.readValue(json, MetadataInstanceResponse.class);
} catch (IOException e) {
logger.debug("Shutting down AzureMetadataService scheduler:" + " error parsing response from Azure Metadata Service: {}", json, e);
scheduledExecutor.shutdown();
return;
}
updateMetadata(metadataInstanceResponse);
}
Aggregations