Search in sources :

Example 1 with HttpCallback

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

the class HttpClientTests method sendRequest.

private String sendRequest(String requestPath, String method) {
    CountDownLatch latch = new CountDownLatch(1);
    final String[] content = new String[1];
    final Throwable[] throwable = new Throwable[1];
    content[0] = null;
    throwable[0] = null;
    createHttpClient().send(new HttpRequest(HttpMethod.GET, REQUEST_HOST + ":" + getWireMockPort() + "/" + requestPath), CancellationToken.NONE, new HttpCallback() {

        @Override
        public void onSuccess(HttpResponse response) {
            try {
                content[0] = response.getBodyAsString();
            } finally {
                latch.countDown();
            }
        }

        @Override
        public void onError(Throwable error) {
            try {
                throwable[0] = error;
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "method");
    if (throwable[0] != null) {
        throw new RuntimeException(throwable[0]);
    } else {
        return content[0];
    }
}
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 2 with HttpCallback

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

the class ChatThreadAsyncClientTest method setupMockTest.

private void setupMockTest() {
    final NoOpHttpClient httpClient = new NoOpHttpClient() {

        @Override
        public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
            httpCallback.onSuccess(ChatResponseMocker.createReadReceiptsResponse(httpRequest));
        }
    };
    this.client = getChatClientBuilder(httpClient).buildAsyncClient();
    String mockThreadId = "19:4b72178530934b7790135dd9359205e0@thread.v2";
    this.chatThreadClient = client.getChatThreadClient(mockThreadId);
    this.threadId = chatThreadClient.getChatThreadId();
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) NoOpHttpClient(com.azure.android.core.test.http.NoOpHttpClient) CancellationToken(com.azure.android.core.util.CancellationToken) HttpCallback(com.azure.android.core.http.HttpCallback)

Example 3 with HttpCallback

use of com.azure.android.core.http.HttpCallback 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 4 with HttpCallback

use of com.azure.android.core.http.HttpCallback 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 5 with HttpCallback

use of com.azure.android.core.http.HttpCallback 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)

Aggregations

HttpCallback (com.azure.android.core.http.HttpCallback)22 HttpResponse (com.azure.android.core.http.HttpResponse)21 CountDownLatch (java.util.concurrent.CountDownLatch)21 HttpPipeline (com.azure.android.core.http.HttpPipeline)18 HttpRequest (com.azure.android.core.http.HttpRequest)18 Test (org.junit.jupiter.api.Test)15 HttpPipelineBuilder (com.azure.android.core.http.HttpPipelineBuilder)14 CancellationToken (com.azure.android.core.util.CancellationToken)14 HttpHeaders (com.azure.android.core.http.HttpHeaders)4 ResourceLock (org.junit.jupiter.api.parallel.ResourceLock)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 HttpCallDispatcher (com.azure.android.core.http.HttpCallDispatcher)2 HttpClient (com.azure.android.core.http.HttpClient)2 NoOpHttpClient (com.azure.android.core.test.http.NoOpHttpClient)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Charset (java.nio.charset.Charset)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1