Search in sources :

Example 6 with HttpResponse

use of com.azure.android.core.http.HttpResponse in project azure-sdk-for-android by Azure.

the class PlaybackClient method send.

@Override
public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
    try {
        HttpResponse httpResponse = playbackHttpResponse(httpRequest);
        httpCallback.onSuccess(httpResponse);
    } catch (Throwable error) {
        httpCallback.onError(error);
    }
}
Also used : HttpResponse(com.azure.android.core.http.HttpResponse)

Example 7 with HttpResponse

use of com.azure.android.core.http.HttpResponse in project azure-sdk-for-android by Azure.

the class RegistrarClient method register.

public void register(String skypeUserToken, String deviceRegistrationToken, SecretKey cryptoKey, SecretKey authKey) throws Throwable {
    String registrarServiceUrl = getRegistrarServiceUrl(skypeUserToken);
    HttpRequest request = new HttpRequest(HttpMethod.POST, registrarServiceUrl);
    request.setHeader(USER_AGENT_HEADER, PLATFORM_UI_VERSION).setHeader(CONTENT_TYPE_HEADER, CONTENT_TYPE_JSON).setHeader(SKYPE_TOKEN_HEADER, skypeUserToken);
    addRequestBody(request, deviceRegistrationToken, cryptoKey, authKey);
    CountDownLatch latch = new CountDownLatch(1);
    final Throwable[] requestError = { null };
    this.httpClient.send(request, CancellationToken.NONE, new HttpCallback() {

        @Override
        public void onSuccess(HttpResponse response) {
            int statusCode = response.getStatusCode();
            RegistrarClient.this.logger.info("Registrar register http response code:" + statusCode);
            if (statusCode != 202) {
                requestError[0] = new RuntimeException("Registrar register request failed with http status code " + statusCode + ". Error message: " + response.getBodyAsString());
            }
            latch.countDown();
        }

        @Override
        public void onError(Throwable error) {
            requestError[0] = error;
            latch.countDown();
        }
    });
    awaitOnLatch(latch);
    if (requestError[0] != null) {
        throw logger.logThrowableAsError(requestError[0]);
    }
    this.logger.info("Register succeed! RegistrationId:" + registrationId);
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) HttpCallback(com.azure.android.core.http.HttpCallback) HttpResponse(com.azure.android.core.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 8 with HttpResponse

use of com.azure.android.core.http.HttpResponse in project azure-sdk-for-android by Azure.

the class RegistrarClient method unregister.

public void unregister(String skypeUserToken) throws Throwable {
    if (registrationId == null) {
        return;
    }
    String registrarServiceUrl = getRegistrarServiceUrl(skypeUserToken);
    String unregisterUrl = registrarServiceUrl + "/" + registrationId;
    HttpRequest request = new HttpRequest(HttpMethod.DELETE, unregisterUrl);
    request.setHeader(USER_AGENT_HEADER, PLATFORM_UI_VERSION).setHeader(CONTENT_TYPE_HEADER, CONTENT_TYPE_JSON).setHeader(SKYPE_TOKEN_HEADER, skypeUserToken);
    CountDownLatch latch = new CountDownLatch(1);
    final Throwable[] requestError = { null };
    this.httpClient.send(request, CancellationToken.NONE, new HttpCallback() {

        @Override
        public void onSuccess(HttpResponse response) {
            int statusCode = response.getStatusCode();
            RegistrarClient.this.logger.info("Registrar unregister http response code:" + statusCode);
            if (statusCode != 202) {
                requestError[0] = new RuntimeException("Registrar unregister request failed with http status code " + statusCode + ". Error message: " + response.getBodyAsString());
            }
            latch.countDown();
        }

        @Override
        public void onError(Throwable error) {
            requestError[0] = error;
            latch.countDown();
        }
    });
    awaitOnLatch(latch);
    if (requestError[0] != null) {
        throw logger.logThrowableAsError(requestError[0]);
    }
    logger.info("Unregister succeed! RegistrationId:" + registrationId);
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) HttpCallback(com.azure.android.core.http.HttpCallback) HttpResponse(com.azure.android.core.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 9 with HttpResponse

use of com.azure.android.core.http.HttpResponse in project azure-sdk-for-android by Azure.

the class ProtocolPolicyTests method withNoOverwrite.

@Test
public void withNoOverwrite() {
    final HttpPipeline pipeline = createPipeline("ftp", false, "https://www.bing.com");
    CountDownLatch latch = new CountDownLatch(1);
    pipeline.send(createHttpRequest("https://www.bing.com"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

        @Override
        public void onSuccess(HttpResponse response) {
            latch.countDown();
        }

        @Override
        public void onError(Throwable error) {
            try {
                throw new RuntimeException(error);
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "withNoOverwrite");
}
Also used : HttpPipeline(com.azure.android.core.http.HttpPipeline) HttpCallback(com.azure.android.core.http.HttpCallback) HttpResponse(com.azure.android.core.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 10 with HttpResponse

use of com.azure.android.core.http.HttpResponse in project azure-sdk-for-android by Azure.

the class RequestIdPolicyTests method newRequestIdForEachCall.

@Test
public void newRequestIdForEachCall() {
    HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {

        String firstRequestId = null;

        @Override
        public void send(HttpRequest request, CancellationToken cancellationToken, HttpCallback httpCallback) {
            if (firstRequestId != null) {
                String newRequestId = request.getHeaders().getValue(REQUEST_ID_HEADER);
                Assertions.assertNotNull(newRequestId, "newRequestId should not be null");
                Assertions.assertNotEquals(newRequestId, firstRequestId);
            }
            firstRequestId = request.getHeaders().getValue(REQUEST_ID_HEADER);
            if (firstRequestId == null) {
                Assertions.fail("The firstRequestId should not be null.");
            }
            httpCallback.onSuccess(mockResponse);
        }
    }).policies(new RequestIdPolicy()).build();
    CountDownLatch latch1 = new CountDownLatch(1);
    pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

        @Override
        public void onSuccess(HttpResponse response) {
            latch1.countDown();
        }

        @Override
        public void onError(Throwable error) {
            try {
                throw new RuntimeException(error);
            } finally {
                latch1.countDown();
            }
        }
    });
    awaitOnLatch(latch1, "newRequestIdForEachCall");
    CountDownLatch latch2 = new CountDownLatch(1);
    pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

        @Override
        public void onSuccess(HttpResponse response) {
            latch2.countDown();
        }

        @Override
        public void onError(Throwable error) {
            try {
                throw new RuntimeException(error);
            } finally {
                latch2.countDown();
            }
        }
    });
    awaitOnLatch(latch2, "newRequestIdForEachCall");
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) CancellationToken(com.azure.android.core.util.CancellationToken) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) HttpCallback(com.azure.android.core.http.HttpCallback) HttpResponse(com.azure.android.core.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch) HttpPipeline(com.azure.android.core.http.HttpPipeline) Test(org.junit.jupiter.api.Test)

Aggregations

HttpResponse (com.azure.android.core.http.HttpResponse)32 HttpCallback (com.azure.android.core.http.HttpCallback)21 CountDownLatch (java.util.concurrent.CountDownLatch)21 HttpRequest (com.azure.android.core.http.HttpRequest)20 HttpPipeline (com.azure.android.core.http.HttpPipeline)18 Test (org.junit.jupiter.api.Test)18 HttpPipelineBuilder (com.azure.android.core.http.HttpPipelineBuilder)14 CancellationToken (com.azure.android.core.util.CancellationToken)13 HttpHeaders (com.azure.android.core.http.HttpHeaders)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 IOException (java.io.IOException)5 NextPolicyCallback (com.azure.android.core.http.NextPolicyCallback)4 PolicyCompleter (com.azure.android.core.http.PolicyCompleter)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 HttpHeader (com.azure.android.core.http.HttpHeader)3 HttpMethod (com.azure.android.core.http.HttpMethod)3 PagedResponse (com.azure.android.core.rest.util.paging.PagedResponse)3 JacksonSerder (com.azure.android.core.serde.jackson.JacksonSerder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3