Search in sources :

Example 6 with HttpRequest

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

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

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

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

Example 10 with HttpRequest

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

the class RetryPolicyTests method retryMax.

@Test
public void retryMax() {
    final int maxRetries = 5;
    final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {

        int count = -1;

        @Override
        public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
            Assertions.assertTrue(count++ < maxRetries);
            httpCallback.onSuccess(new MockHttpResponse(httpRequest, 500));
        }
    }).policies(new RetryPolicy(new FixedDelay(maxRetries, Duration.of(0, ChronoUnit.MILLIS)))).build();
    final HttpResponse[] httpResponse = new HttpResponse[1];
    CountDownLatch latch = new CountDownLatch(1);
    pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

        @Override
        public void onSuccess(HttpResponse response) {
            httpResponse[0] = response;
            latch.countDown();
        }

        @Override
        public void onError(Throwable error) {
            try {
                throw new RuntimeException(error);
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "retryMax");
    assertNotNull(httpResponse[0]);
    assertEquals(500, httpResponse[0].getStatusCode());
}
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

HttpRequest (com.azure.android.core.http.HttpRequest)37 HttpResponse (com.azure.android.core.http.HttpResponse)22 HttpCallback (com.azure.android.core.http.HttpCallback)18 CountDownLatch (java.util.concurrent.CountDownLatch)18 HttpPipeline (com.azure.android.core.http.HttpPipeline)17 HttpPipelineBuilder (com.azure.android.core.http.HttpPipelineBuilder)17 CancellationToken (com.azure.android.core.util.CancellationToken)15 Test (org.junit.jupiter.api.Test)13 HttpHeaders (com.azure.android.core.http.HttpHeaders)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 HttpClient (com.azure.android.core.http.HttpClient)4 UrlBuilder (com.azure.android.core.http.util.UrlBuilder)4 IOException (java.io.IOException)4 HttpHeader (com.azure.android.core.http.HttpHeader)3 ClientLogger (com.azure.android.core.logging.ClientLogger)3 JacksonSerder (com.azure.android.core.serde.jackson.JacksonSerder)3 List (java.util.List)3 ResourceLock (org.junit.jupiter.api.parallel.ResourceLock)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 LIBRARY_PACKAGE_NAME (com.azure.android.communication.chat.BuildConfig.LIBRARY_PACKAGE_NAME)2