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);
}
}
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());
}
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());
}
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();
}
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");
}
Aggregations