Search in sources :

Example 6 with RestClient

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

Example 7 with RestClient

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

the class TestBase method setup.

@Before
public void setup() throws Exception {
    addTextReplacementRule("https://management.azure.com/", this.mockUri() + "/");
    setupTest(name.getMethodName());
    ApplicationTokenCredentials credentials;
    RestClient restClient;
    String defaultSubscription;
    if (IS_MOCKED) {
        credentials = new AzureTestCredentials();
        restClient = buildRestClient(new RestClient.Builder().withBaseUrl(this.mockUri() + "/").withSerializerAdapter(new AzureJacksonAdapter()).withResponseBuilderFactory(new AzureResponseBuilder.Factory()).withCredentials(credentials).withLogLevel(LogLevel.BODY_AND_HEADERS).withNetworkInterceptor(this.interceptor()), true);
        defaultSubscription = MOCK_SUBSCRIPTION;
        System.out.println(this.mockUri());
        out = System.out;
        System.setOut(new PrintStream(new OutputStream() {

            public void write(int b) {
            //DO NOTHING
            }
        }));
    } else {
        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
        credentials = ApplicationTokenCredentials.fromFile(credFile);
        restClient = buildRestClient(new RestClient.Builder().withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER).withSerializerAdapter(new AzureJacksonAdapter()).withResponseBuilderFactory(new AzureResponseBuilder.Factory()).withInterceptor(new ProviderRegistrationInterceptor(credentials)).withCredentials(credentials).withLogLevel(LogLevel.BODY_AND_HEADERS).withReadTimeout(3, TimeUnit.MINUTES).withNetworkInterceptor(this.interceptor()), false);
        defaultSubscription = credentials.defaultSubscriptionId();
        addTextReplacementRule(defaultSubscription, MOCK_SUBSCRIPTION);
    }
    initializeClients(restClient, defaultSubscription, credentials.domain());
}
Also used : PrintStream(java.io.PrintStream) AzureResponseBuilder(com.microsoft.azure.AzureResponseBuilder) AzureJacksonAdapter(com.microsoft.azure.serializer.AzureJacksonAdapter) OutputStream(java.io.OutputStream) RestClient(com.microsoft.rest.RestClient) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) File(java.io.File) ProviderRegistrationInterceptor(com.microsoft.azure.management.resources.fluentcore.utils.ProviderRegistrationInterceptor) Before(org.junit.Before)

Example 8 with RestClient

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

the class ProviderRegistrationInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Response response = chain.proceed(chain.request());
    if (!response.isSuccessful()) {
        String content = errorBody(response.body());
        AzureJacksonAdapter jacksonAdapter = new AzureJacksonAdapter();
        CloudError cloudError = jacksonAdapter.deserialize(content, CloudError.class);
        if (cloudError != null && "MissingSubscriptionRegistration".equals(cloudError.code())) {
            Pattern pattern = Pattern.compile("/subscriptions/([\\w-]+)/", Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(chain.request().url().toString());
            matcher.find();
            RestClient restClient = new RestClient.Builder().withBaseUrl("https://" + chain.request().url().host()).withCredentials(credentials).withSerializerAdapter(jacksonAdapter).withResponseBuilderFactory(new AzureResponseBuilder.Factory()).build();
            ResourceManager resourceManager = ResourceManager.authenticate(restClient).withSubscription(matcher.group(1));
            pattern = Pattern.compile(".*'(.*)'");
            matcher = pattern.matcher(cloudError.message());
            matcher.find();
            Provider provider = registerProvider(matcher.group(1), resourceManager);
            while (provider.registrationState().equalsIgnoreCase("Unregistered") || provider.registrationState().equalsIgnoreCase("Registering")) {
                SdkContext.sleep(5 * 1000);
                provider = resourceManager.providers().getByName(provider.namespace());
            }
            // Retry
            response = chain.proceed(chain.request());
        }
    }
    return response;
}
Also used : Response(okhttp3.Response) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) AzureJacksonAdapter(com.microsoft.azure.serializer.AzureJacksonAdapter) RestClient(com.microsoft.rest.RestClient) CloudError(com.microsoft.azure.CloudError) ResourceManager(com.microsoft.azure.management.resources.implementation.ResourceManager) Provider(com.microsoft.azure.management.resources.Provider)

Example 9 with RestClient

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

the class PasswordCredentialImpl 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("key=%s", value())).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 10 with RestClient

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

the class ManageSqlDatabase method main.

/**
     * Main entry point.
     * @param args the parameters
     */
public static void main(String[] args) {
    try {
        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
        ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credFile);
        RestClient restClient = new RestClient.Builder().withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER).withSerializerAdapter(new AzureJacksonAdapter()).withReadTimeout(150, TimeUnit.SECONDS).withLogLevel(LogLevel.BODY).withResponseBuilderFactory(new AzureResponseBuilder.Factory()).withCredentials(credentials).build();
        Azure azure = Azure.authenticate(restClient, credentials.domain(), credentials.defaultSubscriptionId()).withDefaultSubscription();
        // Print selected subscription
        System.out.println("Selected subscription: " + azure.subscriptionId());
        runSample(azure);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
Also used : Azure(com.microsoft.azure.management.Azure) AzureResponseBuilder(com.microsoft.azure.AzureResponseBuilder) AzureJacksonAdapter(com.microsoft.azure.serializer.AzureJacksonAdapter) RestClient(com.microsoft.rest.RestClient) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) File(java.io.File)

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