Skip to content

Commit

Permalink
Add builder support for GoogleAnalyticsExecutor #53
Browse files Browse the repository at this point in the history
  • Loading branch information
brsanthu committed May 20, 2019
1 parent 1b9273f commit fcf5f8f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public class GoogleAnalyticsBuilder {
private DefaultRequest defaultRequest = new DefaultRequest();
private HttpClient httpClient;
private ExecutorService executor;
private GoogleAnalyticsExecutor googleAnalyticsExecutor;

public GoogleAnalyticsBuilder withGoogleAnalyticsExecutor(GoogleAnalyticsExecutor googleAnalyticsExecutor) {
this.googleAnalyticsExecutor = googleAnalyticsExecutor;
return this;
}

public GoogleAnalyticsBuilder withConfig(GoogleAnalyticsConfig config) {
this.config = GaUtils.firstNotNull(config, new GoogleAnalyticsConfig());
Expand Down Expand Up @@ -64,7 +70,7 @@ public GoogleAnalytics build() {
discoverer.discoverParameters(config, defaultRequest);
}

return new GoogleAnalyticsImpl(config, defaultRequest, createHttpClient(), createExecutor());
return new GoogleAnalyticsImpl(config, defaultRequest, createHttpClient(), createExecutor(), googleAnalyticsExecutor);
}

protected HttpClient createHttpClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,17 @@ public class GoogleAnalyticsImpl implements GoogleAnalytics, GoogleAnalyticsExec
protected final DefaultRequest defaultRequest;
protected final HttpClient httpClient;
protected final ExecutorService executor;
protected final GoogleAnalyticsExecutor googleAnalyticsExecutor;
protected GoogleAnalyticsStatsImpl stats = new GoogleAnalyticsStatsImpl();
protected List<HttpRequest> currentBatch = new ArrayList<>();

public GoogleAnalyticsImpl(GoogleAnalyticsConfig config, DefaultRequest defaultRequest, HttpClient httpClient, ExecutorService executor) {
public GoogleAnalyticsImpl(GoogleAnalyticsConfig config, DefaultRequest defaultRequest, HttpClient httpClient, ExecutorService executor,
GoogleAnalyticsExecutor googleAnalyticsExecutor) {
this.config = config;
this.defaultRequest = defaultRequest;
this.httpClient = httpClient;
this.executor = executor;
this.googleAnalyticsExecutor = googleAnalyticsExecutor;
}

@Override
Expand All @@ -88,16 +91,24 @@ public DefaultRequest getDefaultRequest() {
}

@Override
public Future<GoogleAnalyticsResponse> postAsync(GoogleAnalyticsRequest<?> request) {
public Future<GoogleAnalyticsResponse> postAsync(GoogleAnalyticsRequest<?> gaReq) {
if (googleAnalyticsExecutor != null) {
return googleAnalyticsExecutor.postAsync(gaReq);
}

if (!config.isEnabled()) {
return null;
}

return executor.submit(() -> post(request));
return executor.submit(() -> post(gaReq));
}

@Override
public GoogleAnalyticsResponse post(GoogleAnalyticsRequest<?> gaReq) {
if (googleAnalyticsExecutor != null) {
return googleAnalyticsExecutor.post(gaReq);
}

GoogleAnalyticsResponse response = new GoogleAnalyticsResponse();
response.setGoogleAnalyticsRequest(gaReq);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ public Map<String, String> getRequestParams() {
return requestParams;
}

public void setRequestParams(Map<String, String> postedParms) {
public GoogleAnalyticsResponse setRequestParams(Map<String, String> postedParms) {
requestParams = postedParms;
return this;
}

public void setStatusCode(int statusCode) {
public GoogleAnalyticsResponse setStatusCode(int statusCode) {
this.statusCode = statusCode;
return this;
}

public int getStatusCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static org.mockito.Mockito.when;

import java.time.ZonedDateTime;
import java.util.concurrent.Future;
import java.util.stream.IntStream;

import org.junit.jupiter.api.BeforeAll;
Expand All @@ -22,6 +23,7 @@
import com.brsanthu.googleanalytics.request.AnyHit;
import com.brsanthu.googleanalytics.request.DefaultRequest;
import com.brsanthu.googleanalytics.request.GoogleAnalyticsParameter;
import com.brsanthu.googleanalytics.request.GoogleAnalyticsRequest;
import com.brsanthu.googleanalytics.request.GoogleAnalyticsResponse;
import com.brsanthu.googleanalytics.request.ScreenViewHit;

Expand Down Expand Up @@ -274,6 +276,25 @@ void testAnonymizeUserIp() throws Exception {
new GoogleAnalyticsConfig().setAnonymizeUserIp(true)).build();
GoogleAnalyticsResponse resp2 = ga2.screenView().userIp(userIp).send();
assertThat(resp2.getRequestParams().get(GoogleAnalyticsParameter.USER_IP.getParameterName())).isEqualTo("176.134.201.0");
}

@Test
void testCustomExecutor() throws Exception {
GoogleAnalyticsExecutor exector = new GoogleAnalyticsExecutor() {
@Override
public Future<GoogleAnalyticsResponse> postAsync(GoogleAnalyticsRequest<?> request) {
return null;
}

@Override
public GoogleAnalyticsResponse post(GoogleAnalyticsRequest<?> request) {
return new GoogleAnalyticsResponse().setStatusCode(400);
}
};

GoogleAnalytics ga = GoogleAnalytics.builder().withGoogleAnalyticsExecutor(exector).build();
GoogleAnalyticsResponse resp = ga.screenView().send();
assertThat(resp.getStatusCode()).isEqualTo(400);

}
}

0 comments on commit fcf5f8f

Please sign in to comment.