Skip to content

Commit

Permalink
Merge pull request Azure#254 from RikkiGibson/HttpClientFactory
Browse files Browse the repository at this point in the history
Add HTTP client factory
  • Loading branch information
RikkiGibson authored Oct 12, 2017
2 parents 33f4a92 + 5a3e89b commit 9c70b5d
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
import com.microsoft.rest.annotations.POST;
import com.microsoft.rest.annotations.PathParam;
import com.microsoft.rest.http.HttpClient;
import com.microsoft.rest.http.HttpClient.Configuration;
import com.microsoft.rest.http.RxNettyAdapter;
import com.microsoft.rest.policy.RequestPolicy;
import com.microsoft.rest.protocol.SerializerAdapter;
import rx.Single;
import rx.functions.Func1;

import java.net.Proxy;
import java.util.Collections;
import java.util.Date;

/**
Expand All @@ -43,9 +46,8 @@ final class RefreshTokenClient {
}

private static HttpClient createHttpClient(Proxy proxy) {
return new RxNettyAdapter.Builder()
.withProxy(proxy)
.build();
return new RxNettyAdapter.Factory()
.create(new Configuration(Collections.<RequestPolicy.Factory>emptyList(), proxy));
}

AuthenticationResult refreshToken(String tenant, String clientId, String resource, String refreshToken, boolean isMultipleResoureRefreshToken) {
Expand Down
40 changes: 27 additions & 13 deletions client-runtime/src/main/java/com/microsoft/rest/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* An instance of this class stores configuration for setting up specific service clients.
*/
public final class RestClient {
private final HttpClient.Factory httpClientFactory;
private final HttpClient httpClient;
private final Proxy proxy;
private final String baseURL;
Expand All @@ -53,21 +54,20 @@ private RestClient(RestClient.Builder builder) {
this.logLevel = builder.logLevel;
this.customPolicyFactories = builder.customPolicyFactories;

final RxNettyAdapter.Builder httpClientBuilder = new RxNettyAdapter.Builder()
.withRequestPolicy(new UserAgentPolicy.Factory(userAgent))
.withRequestPolicy(new RetryPolicy.Factory())
.withRequestPolicy(new AddCookiesPolicy.Factory());
if (credentials != null) {
httpClientBuilder.withRequestPolicy(new CredentialsPolicy.Factory(credentials));
}
httpClientBuilder.withRequestPolicies(customPolicyFactories)
.withRequestPolicy(new LoggingPolicy.Factory(logLevel));
this.httpClientFactory = builder.httpClientFactory;

if (proxy != null) {
httpClientBuilder.withProxy(proxy);
List<RequestPolicy.Factory> policyFactories = new ArrayList<>();
policyFactories.add(new UserAgentPolicy.Factory(userAgent));
policyFactories.add(new RetryPolicy.Factory());
policyFactories.add(new AddCookiesPolicy.Factory());
if (credentials != null) {
policyFactories.add(new CredentialsPolicy.Factory(credentials));
}
policyFactories.addAll(customPolicyFactories);
policyFactories.add(new LoggingPolicy.Factory(logLevel));

this.httpClient = httpClientBuilder.build();
HttpClient.Configuration configuration = new HttpClient.Configuration(policyFactories, proxy);
this.httpClient = httpClientFactory.create(configuration);
}

/**
Expand Down Expand Up @@ -162,6 +162,7 @@ public static class Builder {
private final long defaultReadTimeoutMillis = 10000;
private final long defaultConnectionTimeoutMillis = 10000;

private HttpClient.Factory httpClientFactory;
private Proxy proxy;
/** The dynamic base URL with variables wrapped in "{" and "}". */
private String baseUrl;
Expand All @@ -180,6 +181,7 @@ public static class Builder {
private LogLevel logLevel = LogLevel.NONE;

private Builder(final RestClient restClient) {
this.httpClientFactory = restClient.httpClientFactory;
this.proxy = restClient.proxy;
this.baseUrl = restClient.baseURL;
this.userAgent = restClient.userAgent;
Expand All @@ -194,7 +196,19 @@ private Builder(final RestClient restClient) {
/**
* Creates an instance of the builder.
*/
public Builder() { }
public Builder() {
this.httpClientFactory = new RxNettyAdapter.Factory();
}

/**
* Sets the httpClientFactory.
* @param httpClientFactory the httpClientFactory to use.
* @return the builder itself for chaining.
*/
public Builder withHttpClientFactory(HttpClient.Factory httpClientFactory) {
this.httpClientFactory = httpClientFactory;
return this;
}

/**
* Sets the proxy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ public String call(byte[] bytes) {
public BufferedHttpResponse buffer() {
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.microsoft.rest.policy.RequestPolicy;
import rx.Single;

import java.net.Proxy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -57,4 +58,48 @@ public final Single<HttpResponse> sendRequestAsync(HttpRequest request) {
* @return A {@link Single} representing the HTTP response that will arrive asynchronously.
*/
protected abstract Single<HttpResponse> sendRequestInternalAsync(HttpRequest request);

/**
* The set of parameters used to create an HTTP client.
*/
public static final class Configuration {
private final List<RequestPolicy.Factory> policyFactories;
private final Proxy proxy;

/**
* @return The policy factories to use when creating RequestPolicies to intercept requests.
*/
public List<RequestPolicy.Factory> policyFactories() {
return policyFactories;
}

/**
* @return The optional proxy to use.
*/
public Proxy proxy() {
return proxy;
}

/**
* Creates a Configuration.
* @param policyFactories The policy factories to use when creating RequestPolicies to intercept requests.
* @param proxy The optional proxy to use.
*/
public Configuration(List<RequestPolicy.Factory> policyFactories, Proxy proxy) {
this.policyFactories = policyFactories;
this.proxy = proxy;
}
}

/**
* Creates an HttpClient from a Configuration.
*/
public interface Factory {
/**
* Creates an HttpClient with the given Configuration.
* @param configuration the configuration.
* @return the HttpClient.
*/
HttpClient create(Configuration configuration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ public abstract class HttpResponse {
public HttpResponse buffer() {
return new BufferedHttpResponse(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -174,63 +172,26 @@ protected InputStream next(InputStream state, Observer<? super byte[]> observer)
}

/**
* The builder class for building a RxNettyAdapter.
* The factory for creating an RxNettyAdapter.
*/
public static class Builder {
private final List<RequestPolicy.Factory> requestPolicyFactories = new ArrayList<>();
private final List<ChannelHandlerConfig> channelHandlerConfigs = new ArrayList<>();

/**
* Add the provided RequestPolicy.Factory to this Builder's configuration.
* @param requestPolicyFactory The RequestPolicy.Factory to add.
* @return The Builder itself for chaining.
*/
public Builder withRequestPolicy(RequestPolicy.Factory requestPolicyFactory) {
requestPolicyFactories.add(requestPolicyFactory);
return this;
}

/**
* Add the provided RequestPolicy.Factories to this Builder's configuration.
* @param requestPolicyFactories The RequestPolicy.Factories to add.
* @return The Builder itself for chaining.
*/
public Builder withRequestPolicies(Collection<RequestPolicy.Factory> requestPolicyFactories) {
this.requestPolicyFactories.addAll(requestPolicyFactories);
return this;
}

/**
* Add the provided ChannelHandlerConfig to this Builder's configuration.
* @param channelHandlerConfig The ChannelHandlerConfig to add.
* @return The Builder itself for chaining.
*/
public Builder withChannelHandler(ChannelHandlerConfig channelHandlerConfig) {
channelHandlerConfigs.add(channelHandlerConfig);
return this;
}

/**
* Add a Proxy to the RxNettyAdapter that will be built from this Builder.
* @param proxy The Proxy to add.
* @return The Builder itself for chaining.
*/
public Builder withProxy(final Proxy proxy) {
return withChannelHandler(new ChannelHandlerConfig(new Func0<ChannelHandler>() {
@Override
public ChannelHandler call() {
return new HttpProxyHandler(proxy.address());
}
},
false));
}
public static class Factory implements HttpClient.Factory {
@Override
public HttpClient create(final Configuration configuration) {
final List<ChannelHandlerConfig> channelHandlerConfigs;
final Proxy proxy = configuration.proxy();
if (proxy != null) {
ChannelHandlerConfig channelHandlerConfig = new ChannelHandlerConfig(new Func0<ChannelHandler>() {
@Override
public ChannelHandler call() {
return new HttpProxyHandler(proxy.address());
}
}, false);
channelHandlerConfigs = Collections.singletonList(channelHandlerConfig);
} else {
channelHandlerConfigs = Collections.emptyList();
}

/**
* Build a RxNettyAdapter using this Builder's configuration.
* @return An RxNettyAdapter that uses this Builder's configuration.
*/
public RxNettyAdapter build() {
return new RxNettyAdapter(requestPolicyFactories, channelHandlerConfigs);
return new RxNettyAdapter(configuration.policyFactories(), channelHandlerConfigs);
}
}
}

0 comments on commit 9c70b5d

Please sign in to comment.