Search in sources :

Example 1 with AzureJacksonAdapter

use of com.microsoft.azure.serializer.AzureJacksonAdapter in project autorest-clientruntime-for-java by Azure.

the class RequestIdHeaderInterceptorTests method sameRequestIdForRetry.

@Test
public void sameRequestIdForRetry() throws Exception {
    RestClient restClient = new RestClient.Builder().withBaseUrl("http://localhost").withSerializerAdapter(new AzureJacksonAdapter()).withResponseBuilderFactory(new AzureResponseBuilder.Factory()).withInterceptor(new RequestIdHeaderInterceptor()).withInterceptor(new RetryHandler()).withInterceptor(new Interceptor() {

        private String firstRequestId = null;

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if (request.header(REQUEST_ID_HEADER) != null) {
                if (firstRequestId == null) {
                    firstRequestId = request.header(REQUEST_ID_HEADER);
                    return new Response.Builder().code(500).request(request).protocol(Protocol.HTTP_1_1).build();
                } else if (request.header(REQUEST_ID_HEADER).equals(firstRequestId)) {
                    return new Response.Builder().code(200).request(request).protocol(Protocol.HTTP_1_1).build();
                }
            }
            return new Response.Builder().code(400).request(request).protocol(Protocol.HTTP_1_1).build();
        }
    }).build();
    AzureServiceClient serviceClient = new AzureServiceClient(restClient) {
    };
    Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().get().url("http://localhost").build()).execute();
    Assert.assertEquals(200, response.code());
}
Also used : AzureJacksonAdapter(com.microsoft.azure.serializer.AzureJacksonAdapter) RestClient(com.microsoft.rest.RestClient) Request(okhttp3.Request) RequestIdHeaderInterceptor(com.microsoft.rest.interceptors.RequestIdHeaderInterceptor) Response(okhttp3.Response) RetryHandler(com.microsoft.rest.retry.RetryHandler) Interceptor(okhttp3.Interceptor) RequestIdHeaderInterceptor(com.microsoft.rest.interceptors.RequestIdHeaderInterceptor) Test(org.junit.Test)

Example 2 with AzureJacksonAdapter

use of com.microsoft.azure.serializer.AzureJacksonAdapter in project autorest-clientruntime-for-java by Azure.

the class RequestIdHeaderInterceptorTests method newRequestIdForEachCall.

@Test
public void newRequestIdForEachCall() throws Exception {
    RestClient restClient = new RestClient.Builder().withBaseUrl("http://localhost").withSerializerAdapter(new AzureJacksonAdapter()).withResponseBuilderFactory(new AzureResponseBuilder.Factory()).withInterceptor(new RequestIdHeaderInterceptor()).withInterceptor(new Interceptor() {

        private String firstRequestId = null;

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if (request.header(REQUEST_ID_HEADER) != null) {
                if (firstRequestId == null) {
                    firstRequestId = request.header(REQUEST_ID_HEADER);
                    return new Response.Builder().code(200).request(request).protocol(Protocol.HTTP_1_1).build();
                } else if (!request.header(REQUEST_ID_HEADER).equals(firstRequestId)) {
                    return new Response.Builder().code(200).request(request).protocol(Protocol.HTTP_1_1).build();
                }
            }
            return new Response.Builder().code(400).request(request).protocol(Protocol.HTTP_1_1).build();
        }
    }).build();
    AzureServiceClient serviceClient = new AzureServiceClient(restClient) {
    };
    Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().get().url("http://localhost").build()).execute();
    Assert.assertEquals(200, response.code());
    response = serviceClient.restClient().httpClient().newCall(new Request.Builder().get().url("http://localhost").build()).execute();
    Assert.assertEquals(200, response.code());
}
Also used : AzureJacksonAdapter(com.microsoft.azure.serializer.AzureJacksonAdapter) RestClient(com.microsoft.rest.RestClient) Request(okhttp3.Request) RequestIdHeaderInterceptor(com.microsoft.rest.interceptors.RequestIdHeaderInterceptor) Response(okhttp3.Response) Interceptor(okhttp3.Interceptor) RequestIdHeaderInterceptor(com.microsoft.rest.interceptors.RequestIdHeaderInterceptor) Test(org.junit.Test)

Example 3 with AzureJacksonAdapter

use of com.microsoft.azure.serializer.AzureJacksonAdapter in project photon-model by vmware.

the class AzureSdkClients method buildRestClient.

/**
 * Build Azure RestClient with specified executor service and credentials using
 * {@link RestClient.Builder}.
 */
private static RestClient buildRestClient(ApplicationTokenCredentials credentials, ExecutorService executorService) {
    final Retrofit.Builder retrofitBuilder;
    {
        retrofitBuilder = new Retrofit.Builder();
        if (executorService != null) {
            RxJavaCallAdapterFactory rxWithExecutorCallFactory = RxJavaCallAdapterFactory.createWithScheduler(Schedulers.from(executorService));
            retrofitBuilder.addCallAdapterFactory(rxWithExecutorCallFactory);
        }
    }
    final RestClient.Builder restClientBuilder = new RestClient.Builder(new OkHttpClient.Builder(), retrofitBuilder);
    restClientBuilder.withBaseUrl(AzureUtils.getAzureBaseUri());
    restClientBuilder.withCredentials(credentials);
    restClientBuilder.withSerializerAdapter(new AzureJacksonAdapter());
    restClientBuilder.withLogLevel(getRestClientLogLevel());
    restClientBuilder.withInterceptor(new ResourceManagerThrottlingInterceptor());
    if (executorService != null) {
        restClientBuilder.withCallbackExecutor(executorService);
    }
    restClientBuilder.withResponseBuilderFactory(new Factory());
    return restClientBuilder.build();
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) RxJavaCallAdapterFactory(retrofit2.adapter.rxjava.RxJavaCallAdapterFactory) CacheBuilder(com.google.common.cache.CacheBuilder) AzureJacksonAdapter(com.microsoft.azure.serializer.AzureJacksonAdapter) RestClient(com.microsoft.rest.RestClient) Factory(com.microsoft.rest.ServiceResponseBuilder.Factory) RxJavaCallAdapterFactory(retrofit2.adapter.rxjava.RxJavaCallAdapterFactory) ResourceManagerThrottlingInterceptor(com.microsoft.azure.management.resources.fluentcore.utils.ResourceManagerThrottlingInterceptor)

Example 4 with AzureJacksonAdapter

use of com.microsoft.azure.serializer.AzureJacksonAdapter in project autorest.java by Azure.

the class SubscriptionInMethodTests method setup.

@BeforeClass
public static void setup() {
    RestClient restClient = new RestClient.Builder().withBaseUrl("http://localhost:3000").withSerializerAdapter(new AzureJacksonAdapter()).withResponseBuilderFactory(new AzureResponseBuilder.Factory()).withCredentials(new TokenCredentials(null, UUID.randomUUID().toString())).withInterceptor(new RequestIdHeaderInterceptor()).build();
    client = new AutoRestAzureSpecialParametersTestClientImpl(restClient);
    client.withSubscriptionId("1234-5678-9012-3456");
}
Also used : AutoRestAzureSpecialParametersTestClientImpl(fixtures.azurespecials.implementation.AutoRestAzureSpecialParametersTestClientImpl) AzureResponseBuilder(com.microsoft.azure.AzureResponseBuilder) AzureJacksonAdapter(com.microsoft.azure.serializer.AzureJacksonAdapter) RestClient(com.microsoft.rest.RestClient) RequestIdHeaderInterceptor(com.microsoft.rest.interceptors.RequestIdHeaderInterceptor) TokenCredentials(com.microsoft.rest.credentials.TokenCredentials) BeforeClass(org.junit.BeforeClass)

Example 5 with AzureJacksonAdapter

use of com.microsoft.azure.serializer.AzureJacksonAdapter in project autorest.java by Azure.

the class PagingTests method setup.

@BeforeClass
public static void setup() {
    RestClient restClient = new RestClient.Builder().withBaseUrl("http://localhost:3000").withSerializerAdapter(new AzureJacksonAdapter()).withResponseBuilderFactory(new AzureResponseBuilder.Factory()).build();
    client = new AutoRestPagingTestServiceImpl(restClient);
    ;
}
Also used : AzureResponseBuilder(com.microsoft.azure.AzureResponseBuilder) AzureJacksonAdapter(com.microsoft.azure.serializer.AzureJacksonAdapter) RestClient(com.microsoft.rest.RestClient) AutoRestPagingTestServiceImpl(fixtures.paging.implementation.AutoRestPagingTestServiceImpl) BeforeClass(org.junit.BeforeClass)

Aggregations

AzureJacksonAdapter (com.microsoft.azure.serializer.AzureJacksonAdapter)15 RestClient (com.microsoft.rest.RestClient)15 AzureResponseBuilder (com.microsoft.azure.AzureResponseBuilder)11 ApplicationTokenCredentials (com.microsoft.azure.credentials.ApplicationTokenCredentials)6 File (java.io.File)5 Azure (com.microsoft.azure.management.Azure)4 RequestIdHeaderInterceptor (com.microsoft.rest.interceptors.RequestIdHeaderInterceptor)4 BeforeClass (org.junit.BeforeClass)4 Response (okhttp3.Response)3 MappingBuilder (com.github.tomakehurst.wiremock.client.MappingBuilder)2 ResponseDefinitionBuilder (com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder)2 TokenCredentials (com.microsoft.rest.credentials.TokenCredentials)2 AutoRestAzureSpecialParametersTestClientImpl (fixtures.azurespecials.implementation.AutoRestAzureSpecialParametersTestClientImpl)2 Interceptor (okhttp3.Interceptor)2 Request (okhttp3.Request)2 Test (org.junit.Test)2 CacheBuilder (com.google.common.cache.CacheBuilder)1 CloudError (com.microsoft.azure.CloudError)1 Provider (com.microsoft.azure.management.resources.Provider)1 ProviderRegistrationInterceptor (com.microsoft.azure.management.resources.fluentcore.utils.ProviderRegistrationInterceptor)1