Search in sources :

Example 11 with HttpResponse

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);
        }
    }
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) HttpResponse(com.azure.core.http.HttpResponse)

Example 12 with HttpResponse

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();
}
Also used : HttpResponse(com.azure.core.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 13 with HttpResponse

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();
}
Also used : HttpResponse(com.azure.core.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 14 with HttpResponse

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);
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) HttpResponse(com.azure.core.http.HttpResponse) Date(java.util.Date)

Example 15 with HttpResponse

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);
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) HttpResponse(com.azure.core.http.HttpResponse) IOException(java.io.IOException)

Aggregations

HttpResponse (com.azure.core.http.HttpResponse)18 Test (org.junit.jupiter.api.Test)10 HttpRequest (com.azure.core.http.HttpRequest)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)6 HttpHeaders (com.azure.core.http.HttpHeaders)4 VaultModel (com.github.nagyesta.lowkeyvault.http.management.VaultModel)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)2 IOException (java.io.IOException)2 URL (java.net.URL)2 Mono (reactor.core.publisher.Mono)2 CLOUD_RESOURCE_REQUEST_DATA_KEY (bio.terra.cloudres.azure.resourcemanager.common.Defaults.CLOUD_RESOURCE_REQUEST_DATA_KEY)1 ClientConfig (bio.terra.cloudres.common.ClientConfig)1 OperationAnnotator (bio.terra.cloudres.common.OperationAnnotator)1 OperationData (bio.terra.cloudres.common.OperationData)1 ContentType (com.azure.core.http.ContentType)1 HttpMethod (com.azure.core.http.HttpMethod)1 HttpPipelineCallContext (com.azure.core.http.HttpPipelineCallContext)1