Search in sources :

Example 1 with RestClient

use of com.microsoft.rest.RestClient in project azure-sdk-for-java by Azure.

the class CertificateCredentialImpl method exportAuthFile.

void exportAuthFile(ServicePrincipalImpl servicePrincipal) {
    if (authFile == null) {
        return;
    }
    RestClient restClient = servicePrincipal.manager().roleInner().restClient();
    AzureEnvironment environment = null;
    if (restClient.credentials() instanceof AzureTokenCredentials) {
        environment = ((AzureTokenCredentials) restClient.credentials()).environment();
    } else {
        String baseUrl = restClient.retrofit().baseUrl().toString();
        for (AzureEnvironment env : AzureEnvironment.knownEnvironments()) {
            if (env.resourceManagerEndpoint().toLowerCase().contains(baseUrl.toLowerCase())) {
                environment = env;
            }
        }
        if (environment == null) {
            throw new IllegalArgumentException("Unknown resource manager endpoint " + baseUrl);
        }
    }
    StringBuilder builder = new StringBuilder();
    builder.append(String.format("client=%s", servicePrincipal.applicationId())).append("\n");
    builder.append(String.format("certificate=%s", privateKeyPath)).append("\n");
    builder.append(String.format("certificatePassword=%s", privateKeyPassword)).append("\n");
    builder.append(String.format("tenant=%s", servicePrincipal.manager().tenantId())).append("\n");
    builder.append(String.format("subscription=%s", servicePrincipal.assignedSubscription)).append("\n");
    builder.append(String.format("authURL=%s", normalizeAuthFileUrl(environment.activeDirectoryEndpoint()))).append("\n");
    builder.append(String.format("baseURL=%s", normalizeAuthFileUrl(environment.resourceManagerEndpoint()))).append("\n");
    builder.append(String.format("graphURL=%s", normalizeAuthFileUrl(environment.graphEndpoint()))).append("\n");
    builder.append(String.format("managementURI=%s", normalizeAuthFileUrl(environment.managementEndpoint())));
    try {
        authFile.write(builder.toString().getBytes());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : AzureEnvironment(com.microsoft.azure.AzureEnvironment) RestClient(com.microsoft.rest.RestClient) IOException(java.io.IOException) AzureTokenCredentials(com.microsoft.azure.credentials.AzureTokenCredentials)

Example 2 with RestClient

use of com.microsoft.rest.RestClient 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 3 with RestClient

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

use of com.microsoft.rest.RestClient 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 5 with RestClient

use of com.microsoft.rest.RestClient 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)

Aggregations

RestClient (com.microsoft.rest.RestClient)18 AzureJacksonAdapter (com.microsoft.azure.serializer.AzureJacksonAdapter)15 AzureResponseBuilder (com.microsoft.azure.AzureResponseBuilder)11 ApplicationTokenCredentials (com.microsoft.azure.credentials.ApplicationTokenCredentials)7 File (java.io.File)6 Azure (com.microsoft.azure.management.Azure)4 RequestIdHeaderInterceptor (com.microsoft.rest.interceptors.RequestIdHeaderInterceptor)4 BeforeClass (org.junit.BeforeClass)4 IOException (java.io.IOException)3 Response (okhttp3.Response)3 MappingBuilder (com.github.tomakehurst.wiremock.client.MappingBuilder)2 ResponseDefinitionBuilder (com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder)2 AzureEnvironment (com.microsoft.azure.AzureEnvironment)2 AzureTokenCredentials (com.microsoft.azure.credentials.AzureTokenCredentials)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