Search in sources :

Example 1 with RequestIdHeaderInterceptor

use of com.microsoft.rest.interceptors.RequestIdHeaderInterceptor 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 RequestIdHeaderInterceptor

use of com.microsoft.rest.interceptors.RequestIdHeaderInterceptor 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 RequestIdHeaderInterceptor

use of com.microsoft.rest.interceptors.RequestIdHeaderInterceptor 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 4 with RequestIdHeaderInterceptor

use of com.microsoft.rest.interceptors.RequestIdHeaderInterceptor in project autorest.java by Azure.

the class SubscriptionInCredentialsTests method setup.

@BeforeClass
public static void setup() {
    RestClient restClient = new RestClient.Builder().withBaseUrl("http://localhost:3000").withCredentials(new TokenCredentials(null, UUID.randomUUID().toString())).withSerializerAdapter(new AzureJacksonAdapter()).withResponseBuilderFactory(new AzureResponseBuilder.Factory()).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) 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)

Aggregations

AzureJacksonAdapter (com.microsoft.azure.serializer.AzureJacksonAdapter)4 RestClient (com.microsoft.rest.RestClient)4 RequestIdHeaderInterceptor (com.microsoft.rest.interceptors.RequestIdHeaderInterceptor)4 AzureResponseBuilder (com.microsoft.azure.AzureResponseBuilder)2 TokenCredentials (com.microsoft.rest.credentials.TokenCredentials)2 AutoRestAzureSpecialParametersTestClientImpl (fixtures.azurespecials.implementation.AutoRestAzureSpecialParametersTestClientImpl)2 Interceptor (okhttp3.Interceptor)2 Request (okhttp3.Request)2 Response (okhttp3.Response)2 BeforeClass (org.junit.BeforeClass)2 Test (org.junit.Test)2 RetryHandler (com.microsoft.rest.retry.RetryHandler)1