Skip to content

Commit

Permalink
remove apache from module api
Browse files Browse the repository at this point in the history
  • Loading branch information
mredolatti committed Sep 12, 2024
1 parent aac8a1b commit 3c853dc
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 68 deletions.
1 change: 1 addition & 0 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<include>io.split.schemas:*</include>
<include>io.codigo.grammar:*</include>
<include>org.apache.httpcomponents.*</include>
<include>org.apache.hc.*</include>
<include>com.google.*</include>
<include>org.yaml:snakeyaml:*</include>

Expand Down
17 changes: 11 additions & 6 deletions client/src/main/java/io/split/client/SplitFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
import static io.split.client.utils.SplitExecutorFactory.buildExecutorService;

public class SplitFactoryImpl implements SplitFactory {
private static final org.slf4j.Logger _log = LoggerFactory.getLogger(SplitFactoryImpl.class);
private static final org.slf4j.Logger _log = LoggerFactory.getLogger(SplitFactoryImpl.class);
private static final String LEGACY_LOG_MESSAGE = "The sdk initialize in localhost mode using Legacy file. The splitFile or "
+
"inputStream doesn't add it to the config.";
Expand Down Expand Up @@ -193,7 +193,8 @@ public SplitFactoryImpl(String apiToken, SplitClientConfig config) throws URISyn
if (config.alternativeHTTPModule() == null) {
_splitHttpClient = buildSplitHttpClient(apiToken, config, _sdkMetadata, _requestDecorator);
} else {
_splitHttpClient = config.alternativeHTTPModule().createClient(apiToken, _sdkMetadata, _requestDecorator);
_splitHttpClient = config.alternativeHTTPModule().createClient(apiToken, _sdkMetadata); // ,
// _requestDecorator);
}

// Roots
Expand Down Expand Up @@ -240,7 +241,8 @@ public SplitFactoryImpl(String apiToken, SplitClientConfig config) throws URISyn
EventsSender eventsSender = EventsSender.create(_splitHttpClient, _eventsRootTarget, _telemetryStorageProducer);
_eventsTask = EventsTask.create(config.eventSendIntervalInMillis(), eventsStorage, eventsSender,
config.getThreadFactory());
_telemetrySyncTask = new TelemetrySyncTask(config.getTelemetryRefreshRate(), _telemetrySynchronizer, config.getThreadFactory());
_telemetrySyncTask = new TelemetrySyncTask(config.getTelemetryRefreshRate(), _telemetrySynchronizer,
config.getThreadFactory());

// Evaluator
_evaluator = new EvaluatorImp(splitCache, segmentCache);
Expand All @@ -263,7 +265,8 @@ public SplitFactoryImpl(String apiToken, SplitClientConfig config) throws URISyn
// SyncManager
SplitTasks splitTasks = SplitTasks.build(_splitSynchronizationTask, _segmentSynchronizationTaskImp,
_impressionsManager, _eventsTask, _telemetrySyncTask, _uniqueKeysTracker);
SplitAPI splitAPI = SplitAPI.build(_splitHttpClient, buildSSEdHttpClient(apiToken, config, _sdkMetadata), _requestDecorator);
SplitAPI splitAPI = SplitAPI.build(_splitHttpClient, buildSSEdHttpClient(apiToken, config, _sdkMetadata),
_requestDecorator);

_syncManager = SyncManagerImp.build(splitTasks, _splitFetcher, splitCache, splitAPI,
segmentCache, _gates, _telemetryStorageProducer, _telemetrySynchronizer, config, splitParser,
Expand Down Expand Up @@ -334,8 +337,10 @@ protected SplitFactoryImpl(String apiToken, SplitClientConfig config, CustomStor
_evaluator = new EvaluatorImp(userCustomSplitAdapterConsumer, userCustomSegmentAdapterConsumer);
_impressionsSender = PluggableImpressionSender.create(customStorageWrapper);
_uniqueKeysTracker = createUniqueKeysTracker(config);
_impressionsManager = buildImpressionsManager(config, userCustomImpressionAdapterConsumer, userCustomImpressionAdapterProducer);
_telemetrySyncTask = new TelemetrySyncTask(config.getTelemetryRefreshRate(), _telemetrySynchronizer, config.getThreadFactory());
_impressionsManager = buildImpressionsManager(config, userCustomImpressionAdapterConsumer,
userCustomImpressionAdapterProducer);
_telemetrySyncTask = new TelemetrySyncTask(config.getTelemetryRefreshRate(), _telemetrySynchronizer,
config.getThreadFactory());

SplitTasks splitTasks = SplitTasks.build(null, null,
_impressionsManager, null, _telemetrySyncTask, _uniqueKeysTracker);
Expand Down
31 changes: 29 additions & 2 deletions client/src/main/java/io/split/client/dtos/SplitHttpResponse.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.split.client.dtos;

import java.util.Map;
import org.apache.hc.core5.http.Header;
import java.util.List;

/**
* A structure for returning http call results information
*/
Expand All @@ -11,15 +11,42 @@ public class SplitHttpResponse {
private final String _body;
private final Header[] _responseHeaders;

public static class Header {
private String _name;
private List<String> _values;

public Header(String name, List<String> values) {
_name = name;
_values = values;
}

public String getName() {
return _name;
}

public List<String> getValues() {
return _values;
}
};

public SplitHttpResponse(Integer statusCode, String statusMessage, String body, Header[] headers) {
_statusCode = statusCode;
_statusMessage = statusMessage;
_body = body;
_responseHeaders = headers;
}

public SplitHttpResponse(Integer statusCode, String statusMessage, String body, List<Header> headers) {
_statusCode = statusCode;
_statusMessage = statusMessage;
_body = body;
_responseHeaders = headers.toArray(new Header[0]);
}

public Integer statusCode() {
return _statusCode;
}

public String statusMessage() {
return _statusMessage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import io.split.client.dtos.ImpressionCount;
import io.split.client.dtos.SplitHttpResponse;
import io.split.client.dtos.TestImpressions;
import io.split.client.utils.Json;
import io.split.client.utils.Utils;

import io.split.service.SplitHttpClient;
import io.split.telemetry.domain.enums.HTTPLatenciesEnum;
import io.split.telemetry.domain.enums.LastSynchronizationRecordsEnum;
import io.split.telemetry.domain.enums.ResourceEnum;
import io.split.telemetry.storage.TelemetryRuntimeProducer;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -67,10 +67,12 @@ private HttpImpressionsSender(SplitHttpClient client, URI impressionBulkTarget,
public void postImpressionsBulk(List<TestImpressions> impressions) {
long initTime = System.currentTimeMillis();
try {
HttpEntity entity = Utils.toJsonEntity(impressions);
Map<String, List<String>> additionalHeaders = Collections.singletonMap(IMPRESSIONS_MODE_HEADER,
Collections.singletonList(_mode.toString()));
SplitHttpResponse response = _client.post(_impressionBulkTarget, entity, additionalHeaders);
Map<String, List<String>> additionalHeaders = new HashMap<>();
additionalHeaders.put(IMPRESSIONS_MODE_HEADER, Collections.singletonList(_mode.toString()));
additionalHeaders.put("Content-Type", Collections.singletonList("application/json"));

SplitHttpResponse response = _client.post(_impressionBulkTarget, Json.toJson(impressions),
additionalHeaders);

if (response.statusCode() < HttpStatus.SC_OK || response.statusCode() >= HttpStatus.SC_MULTIPLE_CHOICES) {
_telemetryRuntimeProducer.recordSyncError(ResourceEnum.IMPRESSION_SYNC, response.statusCode());
Expand All @@ -95,8 +97,12 @@ public void postCounters(HashMap<ImpressionCounter.Key, Integer> raw) {
}

try {

Map<String, List<String>> additionalHeaders = new HashMap<>();
additionalHeaders.put("Content-Type", Collections.singletonList("application/json"));

SplitHttpResponse response = _client.post(_impressionCountTarget,
Utils.toJsonEntity(ImpressionCount.fromImpressionCounterData(raw)),
Json.toJson(ImpressionCount.fromImpressionCounterData(raw)),
null);

if (response.statusCode() < HttpStatus.SC_OK || response.statusCode() >= HttpStatus.SC_MULTIPLE_CHOICES) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
import java.io.IOException;

public interface CustomHttpModule {
public SplitHttpClient createClient(String apiToken, SDKMetadata sdkMetadata, RequestDecorator requestDecorator) throws IOException;
public SplitHttpClient createClient(String apiToken, SDKMetadata sdkMetadata) throws IOException;
}
18 changes: 12 additions & 6 deletions client/src/main/java/io/split/service/HttpPostImp.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package io.split.service;

import io.split.client.dtos.SplitHttpResponse;
import io.split.client.utils.Utils;
import io.split.client.utils.Json;
import io.split.telemetry.domain.enums.HttpParamsWrapper;
import io.split.telemetry.storage.TelemetryRuntimeProducer;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;

Expand All @@ -25,16 +28,19 @@ public HttpPostImp(SplitHttpClient client, TelemetryRuntimeProducer telemetryRun

public void post(URI uri, Object object, String posted, HttpParamsWrapper httpParamsWrapper) {
long initTime = System.currentTimeMillis();
HttpEntity entity = Utils.toJsonEntity(object);

try {
SplitHttpResponse response = _client.post(uri, entity, null);
Map<String, List<String>> headers = new HashMap<>();
headers.put("Content-Type", Collections.singletonList("application/json"));
SplitHttpResponse response = _client.post(uri, Json.toJson(object), headers);
if (response.statusCode() < HttpStatus.SC_OK || response.statusCode() >= HttpStatus.SC_MULTIPLE_CHOICES) {
_telemetryRuntimeProducer.recordSyncError(httpParamsWrapper.getResourceEnum(), response.statusCode());
return;
}
_telemetryRuntimeProducer.recordSyncLatency(httpParamsWrapper.getHttpLatenciesEnum(), System.currentTimeMillis() - initTime);
_telemetryRuntimeProducer.recordSuccessfulSync(httpParamsWrapper.getLastSynchronizationRecordsEnum(), System.currentTimeMillis());
_telemetryRuntimeProducer.recordSyncLatency(httpParamsWrapper.getHttpLatenciesEnum(),
System.currentTimeMillis() - initTime);
_telemetryRuntimeProducer.recordSuccessfulSync(httpParamsWrapper.getLastSynchronizationRecordsEnum(),
System.currentTimeMillis());
} catch (Throwable t) {
_logger.warn("Exception when posting " + posted + object, t);
}
Expand Down
6 changes: 2 additions & 4 deletions client/src/main/java/io/split/service/SplitHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import io.split.engine.common.FetchOptions;
import io.split.client.dtos.SplitHttpResponse;

import org.apache.hc.core5.http.HttpEntity;

import java.io.Closeable;
import java.io.IOException;
import java.net.URI;
Expand All @@ -30,6 +28,6 @@ public interface SplitHttpClient extends Closeable {
* @return The response structure SplitHttpResponse
*/
public SplitHttpResponse post(URI uri,
HttpEntity entity,
String entity,
Map<String, List<String>> additionalHeaders) throws IOException;
}
}
21 changes: 16 additions & 5 deletions client/src/main/java/io/split/service/SplitHttpClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.HttpEntities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hc.core5.http.HttpRequest;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public final class SplitHttpClientImpl implements SplitHttpClient {

Expand Down Expand Up @@ -87,18 +91,22 @@ public SplitHttpResponse get(URI uri, FetchOptions options, Map<String, List<Str
response.getReasonPhrase()));
statusMessage = response.getReasonPhrase();
}

return new SplitHttpResponse(response.getCode(),
statusMessage,
EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8),
response.getHeaders());
Arrays.stream(response.getHeaders()).map(
h -> new SplitHttpResponse.Header(h.getName(), Collections.singletonList(h.getValue())))
.collect(Collectors.toList()));
// response.getHeaders());
} catch (Exception e) {
throw new IllegalStateException(String.format("Problem in http get operation: %s", e), e);
} finally {
Utils.forceClose(response);
}
}

public SplitHttpResponse post(URI uri, HttpEntity entity, Map<String, List<String>> additionalHeaders)
public SplitHttpResponse post(URI uri, String body, Map<String, List<String>> additionalHeaders)
throws IOException {

CloseableHttpResponse response = null;
Expand All @@ -112,7 +120,7 @@ public SplitHttpResponse post(URI uri, HttpEntity entity, Map<String, List<Strin
}
}
}
request.setEntity(entity);
request.setEntity(HttpEntities.create(body, ContentType.APPLICATION_JSON));
request = (HttpPost) _requestDecorator.decorateHeaders(request);

response = _client.execute(request);
Expand All @@ -123,7 +131,10 @@ public SplitHttpResponse post(URI uri, HttpEntity entity, Map<String, List<Strin
_log.warn(String.format("Response status was: %s. Reason: %s", response.getCode(),
response.getReasonPhrase()));
}
return new SplitHttpResponse(response.getCode(), statusMessage, "", response.getHeaders());
return new SplitHttpResponse(response.getCode(), statusMessage, "",
Arrays.stream(response.getHeaders()).map(
h -> new SplitHttpResponse.Header(h.getName(), Collections.singletonList(h.getValue())))
.collect(Collectors.toList()));
} catch (Exception e) {
throw new IOException(String.format("Problem in http post operation: %s", e), e);
} finally {
Expand Down
8 changes: 6 additions & 2 deletions okhttp-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@
<version>4.13.0</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.0.3</version>
</dependency>
<!-- Test deps -->
<dependency>
<groupId>junit</groupId>
Expand Down Expand Up @@ -82,4 +86,4 @@
</dependency>

</dependencies>
</project>
</project>
Loading

0 comments on commit 3c853dc

Please sign in to comment.