use of com.azure.android.core.http.HttpPipeline in project azure-sdk-for-android by Azure.
the class HttpLoggingPolicyTests method redactQueryParameters.
/**
* Tests that a query string will be properly redacted before it is logged.
*/
@ParameterizedTest
@MethodSource("redactQueryParametersSupplier")
@ResourceLock("SYSTEM_OUT")
public void redactQueryParameters(String requestUrl, String expectedQueryString, Set<String> allowedQueryParameters) {
HttpPipeline pipeline = new HttpPipelineBuilder().policies(new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC).setAllowedQueryParamNames(allowedQueryParameters))).httpClient(new NoOpHttpClient()).build();
CountDownLatch latch = new CountDownLatch(1);
// pipeline.send(new HttpRequest(HttpMethod.POST, requestUrl), CONTEXT, new HttpCallback() {..})
pipeline.send(new HttpRequest(HttpMethod.POST, requestUrl), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {
@Override
public void onSuccess(HttpResponse response) {
latch.countDown();
}
@Override
public void onError(Throwable error) {
try {
assertTrue(false, "unexpected call to pipeline::send onError" + error.getMessage());
} finally {
latch.countDown();
}
}
});
awaitOnLatch(latch, "redactQueryParameters");
assertTrue(convertOutputStreamToString(logCaptureStream).contains(expectedQueryString));
}
use of com.azure.android.core.http.HttpPipeline in project azure-sdk-for-android by Azure.
the class ChatThreadClientBuilder method createInternalClient.
private AzureCommunicationChatServiceImpl createInternalClient() {
if (endpoint == null) {
throw logger.logExceptionAsError(new NullPointerException("'endpoint' is required."));
}
HttpPipeline pipeline;
if (this.httpPipeline != null) {
pipeline = this.httpPipeline;
} else {
if (this.communicationTokenCredential == null) {
throw logger.logExceptionAsError(new NullPointerException("CommunicationTokenCredential is required."));
}
HttpPipelinePolicy authorizationPolicy = chain -> {
final CompletableFuture<CommunicationAccessToken> tokenFuture = this.communicationTokenCredential.getToken();
final CommunicationAccessToken token;
try {
token = tokenFuture.get();
} catch (ExecutionException e) {
chain.completedError(e);
return;
} catch (InterruptedException e) {
chain.completedError(e);
return;
}
HttpRequest httpRequest = chain.getRequest();
httpRequest.getHeaders().put("Authorization", "Bearer " + token.getToken());
chain.processNextPolicy(httpRequest);
};
pipeline = createHttpPipeline(this.httpClient, authorizationPolicy, this.customPolicies);
}
AzureCommunicationChatServiceImplBuilder clientBuilder = new AzureCommunicationChatServiceImplBuilder().apiVersion((this.serviceVersion == null) ? ChatServiceVersion.getLatest().getVersion() : this.serviceVersion.getVersion()).endpoint(this.endpoint).pipeline(pipeline);
return clientBuilder.buildClient();
}
use of com.azure.android.core.http.HttpPipeline in project azure-sdk-for-android by Azure.
the class AzureCommunicationChatServiceImplBuilder method createHttpPipeline.
private HttpPipeline createHttpPipeline() {
if (httpLogOptions == null) {
httpLogOptions = new HttpLogOptions();
}
List<HttpPipelinePolicy> policies = new ArrayList<>();
String clientName = properties.get(SDK_NAME);
if (clientName == null) {
clientName = "UnknownName";
}
String clientVersion = properties.get(SDK_VERSION);
if (clientVersion == null) {
clientVersion = "UnknownVersion";
}
policies.add(new UserAgentPolicy(null, clientName, clientVersion));
policies.add(retryPolicy == null ? RetryPolicy.withExponentialBackoff() : retryPolicy);
policies.add(new CookiePolicy());
policies.addAll(this.pipelinePolicies);
policies.add(new HttpLoggingPolicy(httpLogOptions));
HttpPipeline httpPipeline = new HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])).httpClient(httpClient).build();
return httpPipeline;
}
use of com.azure.android.core.http.HttpPipeline in project azure-sdk-for-android by Azure.
the class ChatClientBuilder method buildAsyncClient.
/**
* Create asynchronous client applying CommunicationTokenCredential, UserAgentPolicy,
* RetryPolicy, and CookiePolicy.
* Additional HttpPolicies specified by additionalPolicies will be applied after them
*
* @throws NullPointerException if endpoint or CommunicationTokenCredential is not set.
* @return A {@link ChatAsyncClient} instance.
*/
public ChatAsyncClient buildAsyncClient() {
if (this.endpoint == null) {
throw logger.logExceptionAsError(new NullPointerException("Endpoint is required."));
}
HttpPipeline pipeline;
if (this.httpPipeline != null) {
pipeline = this.httpPipeline;
} else {
if (this.communicationTokenCredential == null) {
throw logger.logExceptionAsError(new NullPointerException("CommunicationTokenCredential is required."));
}
HttpPipelinePolicy authorizationPolicy = chain -> {
final CompletableFuture<CommunicationAccessToken> tokenFuture = this.communicationTokenCredential.getToken();
final CommunicationAccessToken token;
try {
token = tokenFuture.get();
} catch (ExecutionException e) {
chain.completedError(e);
return;
} catch (InterruptedException e) {
chain.completedError(e);
return;
}
HttpRequest httpRequest = chain.getRequest();
httpRequest.getHeaders().put("Authorization", "Bearer " + token.getToken());
chain.processNextPolicy(httpRequest);
};
pipeline = createHttpPipeline(this.httpClient, authorizationPolicy, this.customPolicies);
}
AzureCommunicationChatServiceImplBuilder clientBuilder = new AzureCommunicationChatServiceImplBuilder().apiVersion((this.serviceVersion == null) ? ChatServiceVersion.getLatest().getVersion() : this.serviceVersion.getVersion()).endpoint(this.endpoint).pipeline(pipeline);
return new ChatAsyncClient(clientBuilder.buildClient(), this.communicationTokenCredential);
}
use of com.azure.android.core.http.HttpPipeline in project azure-sdk-for-android by Azure.
the class RestProxyXMLTests method canDeserializeXMLWithAttributes.
@Test
public void canDeserializeXMLWithAttributes() throws Exception {
JacksonSerder serializer = new JacksonSerder();
final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new MockXMLHTTPClient()).build();
MyXMLServiceWithAttributes myXMLService = RestProxy.create(MyXMLServiceWithAttributes.class, pipeline, serializer);
final SlideshowOrError slideshowOrError = new SlideshowOrError();
CountDownLatch latch = new CountDownLatch(1);
myXMLService.getSlideshow(new Callback<Response<Slideshow>>() {
@Override
public void onSuccess(Response<Slideshow> response) {
try {
slideshowOrError.slideshow = response.getValue();
} finally {
latch.countDown();
}
}
@Override
public void onFailure(Throwable error) {
try {
slideshowOrError.error = error;
} finally {
latch.countDown();
}
}
});
awaitOnLatch(latch, "canDeserializeXMLWithAttributes");
if (slideshowOrError.error != null) {
fail(slideshowOrError.error);
} else {
Slideshow slideshow = slideshowOrError.slideshow;
assertEquals("Sample Slide Show", slideshow.title());
assertEquals("Date of publication", slideshow.date());
assertEquals("Yours Truly", slideshow.author());
assertEquals(2, slideshow.slides().length);
assertEquals("all", slideshow.slides()[0].type());
assertEquals("Wake up to WonderWidgets!", slideshow.slides()[0].title());
assertEquals(0, slideshow.slides()[0].items().length);
assertEquals("all", slideshow.slides()[1].type());
assertEquals("Overview", slideshow.slides()[1].title());
assertEquals(3, slideshow.slides()[1].items().length);
assertEquals("Why WonderWidgets are great", slideshow.slides()[1].items()[0]);
assertEquals("", slideshow.slides()[1].items()[1]);
assertEquals("Who buys WonderWidgets", slideshow.slides()[1].items()[2]);
String xml = serializer.serialize(slideshow, SerdeEncoding.XML);
Slideshow newSlideshow = serializer.deserialize(xml, Slideshow.class, SerdeEncoding.XML);
String newXML = serializer.serialize(newSlideshow, SerdeEncoding.XML);
assertEquals(xml, newXML);
}
}
Aggregations