Skip to content

Commit

Permalink
Allow overriding the httpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Clint Checketts committed Jul 21, 2017
1 parent c3a2ac9 commit c96407e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -5,7 +5,7 @@
# Java - Domo API SDK
[![Download](https://api.bintray.com/packages/domoinc/domo-java-sdk/domo-java-sdk/images/download.svg) ](https://bintray.com/domoinc/domo-java-sdk/domo-java-sdk/_latestVersion)
[![License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](http://www.opensource.org/licenses/MIT)
[![Build Status](http://img.shields.io/travis/domoinc/domo-java-sdk.svg?style=flat&branch=master)](https://travis-ci.org/domoinc/domo-java-sdk)
[![CircleCI](https://circleci.com/gh/domoinc/domo-java-sdk.svg?style=svg)](https://circleci.com/gh/domoinc/domo-java-sdk)
[![Coverage Status](https://img.shields.io/coveralls/domoinc/domo-java-sdk.svg?style=flat)](https://coveralls.io/r/domoinc/domo-java-sdk?branch=master)

### About
Expand Down
35 changes: 23 additions & 12 deletions domo-java-sdk-all/src/main/java/com/domo/sdk/request/Config.java
Expand Up @@ -9,6 +9,7 @@
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

public class Config {
private static final Logger LOG = LoggerFactory.getLogger(Config.class);
Expand Down Expand Up @@ -40,17 +41,9 @@ public Config(String clientId,
}

if(httpClient == null) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new Slf4jLoggingInterceptor());
logging.setLevel(this.httpLoggingLevel);
this.httpClient = new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.addInterceptor(new OAuthInterceptor(new UrlBuilder(this), this))
.addInterceptor(logging)
.build();
} else {
this.httpClient = httpClient;
throw new IllegalStateException("HttpClient is required");
}

this.httpClient = httpClient;
}

// Visible for testing.
Expand Down Expand Up @@ -103,10 +96,21 @@ public static class Builder{
private List<Scope> scopes = new ArrayList<>();
private OkHttpClient httpClient;
private HttpLoggingInterceptor.Level httpLoggingLevel;
private AtomicReference<Config> configRef = new AtomicReference<>();

public Builder() {
}

public OkHttpClient.Builder defaultHttpClientBuilder() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new Slf4jLoggingInterceptor());
logging.setLevel(this.httpLoggingLevel);

return new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.addInterceptor(new OAuthInterceptor(new UrlBuilder(configRef), configRef))
.addInterceptor(logging);
}

public Builder clientId(String clientId) {
this.clientId = clientId;
return this;
Expand Down Expand Up @@ -144,13 +148,20 @@ public Config build(){
throw new ConfigException("At lease one scope is required");
}

return new Config(this.clientId,
OkHttpClient client = this.httpClient;
if(client == null) {
client = defaultHttpClientBuilder().build();
}

Config conf = new Config(this.clientId,
this.secret,
this.apiHost,
this.useHttps,
this.scopes,
this.httpClient,
client,
this.httpLoggingLevel);
configRef.set(conf);
return conf;
}

private void require(String name, String value){
Expand Down
Expand Up @@ -24,11 +24,15 @@ public class OAuthInterceptor implements Interceptor {
private final Gson gson = new Gson();

private final UrlBuilder urlBuilder;
private final Config config;
private final AtomicReference<Config> config;

public OAuthInterceptor(UrlBuilder urlBuilder, Config config) {
this(urlBuilder, new AtomicReference<>(config));
}

public OAuthInterceptor(UrlBuilder urlBuilder, AtomicReference<Config> configRef) {
this.urlBuilder = urlBuilder;
this.config = config;
this.config = configRef;
}

@Override
Expand Down Expand Up @@ -83,18 +87,18 @@ private int refreshToken() {
HttpUrl url = urlBuilder.fromPathSegments("oauth/token")
.build();

String scopes = config.getScopes()
String scopes = config.get().getScopes()
.stream().map(s -> s.name().toLowerCase())
.collect(Collectors.joining(" "));

Request request = new Request.Builder()
.header("Authorization", Credentials.basic(config.getClientId(), config.getSecret()))
.header("Authorization", Credentials.basic(config.get().getClientId(), config.get().getSecret()))
.header("Accept", "application/json")
.url(url)
.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"),"grant_type=client_credentials&scope="+scopes))
.build();

try (Response response = config.okHttpClient().newCall(request).execute()) {
try (Response response = config.get().okHttpClient().newCall(request).execute()) {

OAuthResponse oauth = gson.fromJson(response.body().charStream(), OAuthResponse.class);
accessToken.set(oauth.access_token);
Expand Down
Expand Up @@ -2,22 +2,28 @@

import okhttp3.HttpUrl;

import java.util.concurrent.atomic.AtomicReference;

public class UrlBuilder {

private final Config config;
private final AtomicReference<Config> config;

public UrlBuilder(Config config) {
this.config = config;
this.config = new AtomicReference<>(config);
}

public UrlBuilder(AtomicReference<Config> configRef) {
this.config = configRef;
}

public HttpUrl.Builder create() {
HttpUrl.Builder builder = new HttpUrl.Builder();
String prefix = "https";
if(!config.useHttps()){
if(!config.get().useHttps()){
prefix = "http";
}

HttpUrl url = HttpUrl.parse(prefix+"://"+config.getApiHost());
HttpUrl url = HttpUrl.parse(prefix+"://"+config.get().getApiHost());

builder.scheme(url.scheme());
builder.host(url.host());
Expand Down

0 comments on commit c96407e

Please sign in to comment.