Skip to content

Commit

Permalink
33 merge with master
Browse files Browse the repository at this point in the history
  • Loading branch information
volkodavs committed Oct 2, 2019
2 parents 7481221 + c0c7c01 commit 26777ad
Show file tree
Hide file tree
Showing 134 changed files with 3,989 additions and 1,056 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.matchbook.sdk.core;


import com.matchbook.sdk.core.exceptions.MatchbookSDKException;

public interface StreamObserver<V> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public enum ErrorType {

HTTP, UNAUTHENTICATED
HTTP, UNAUTHENTICATED, PARSING

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.matchbook.sdk.core.exceptions;

public class MatchbookSDKParsingException extends MatchbookSDKException {

private static final long serialVersionUID = 4808543873575620833L;

public MatchbookSDKParsingException(String message, Throwable cause) {
super(message, cause, ErrorType.PARSING);
}

public MatchbookSDKParsingException(String message) {
super(message, ErrorType.PARSING);
}

public MatchbookSDKParsingException(Throwable cause) {
super(cause, ErrorType.PARSING);
}

}
26 changes: 13 additions & 13 deletions matchbook-sdk-rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<artifactId>matchbook-sdk-rest</artifactId>

<dependencies>
<!--Matchbook SDK-->
<!-- Matchbook SDK-->
<dependency>
<groupId>com.matchbook</groupId>
<artifactId>matchbook-sdk-core</artifactId>
Expand All @@ -23,12 +23,12 @@
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
Expand All @@ -52,14 +52,14 @@
<version>${jackson.version}</version>
</dependency>

<!-- Clients -->
<!-- HTTP -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>

<!--Logs-->
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -68,13 +68,13 @@

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>test</scope>
</dependency>
Expand All @@ -92,18 +92,18 @@
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>${wiremock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>${wiremock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,66 +1,73 @@
package com.matchbook.sdk.rest;

import java.io.IOException;
import java.io.InputStream;
import java.util.stream.Collectors;

import com.matchbook.sdk.core.StreamObserver;
import com.matchbook.sdk.core.exceptions.MatchbookSDKHttpException;
import com.matchbook.sdk.core.exceptions.MatchbookSDKParsingException;
import com.matchbook.sdk.rest.configs.HttpCallback;
import com.matchbook.sdk.rest.configs.Parser;
import com.matchbook.sdk.rest.configs.Serializer;
import com.matchbook.sdk.rest.dtos.RestRequest;
import com.matchbook.sdk.rest.dtos.RestResponse;
import com.matchbook.sdk.rest.readers.Reader;

import java.io.IOException;
import java.io.InputStream;
import java.util.stream.Collectors;

abstract class AbstractRestClient {
abstract class AbstractClientRest implements Client {

protected final ConnectionManager connectionManager;

protected AbstractRestClient(ConnectionManager connectionManager) {
protected AbstractClientRest(ConnectionManager connectionManager) {
this.connectionManager = connectionManager;
}

protected <REQ extends RestRequest, RESP extends RestResponse<T>, T> void getRequest(String url,
protected <REQ extends RestRequest, RESP extends RestResponse, T> void getRequest(
String url,
REQ request,
StreamObserver<T> observer,
Class<RESP> responseClass) {
Reader<T, RESP> reader) {
String requestUrl = buildUrl(url, request);
Serializer serializer = connectionManager.getSerializer();
connectionManager.getHttpClient().get(requestUrl, new RestCallback<>(observer, responseClass, serializer));
connectionManager.getHttpClient().get(requestUrl, new RestCallback<>(observer, serializer, reader));
}

protected <REQ extends RestRequest, RESP extends RestResponse<T>, T> void postRequest(String url,
protected <REQ extends RestRequest, RESP extends RestResponse, T> void postRequest(
String url,
REQ request,
StreamObserver<T> observer,
Class<RESP> responseClass) {
Reader<T, RESP> reader) {
try {
Serializer serializer = connectionManager.getSerializer();
String requestBody = serializer.writeObjectAsString(request);
connectionManager.getHttpClient().post(url, requestBody, new RestCallback<>(observer, responseClass, serializer));
connectionManager.getHttpClient().post(url, requestBody, new RestCallback<>(observer, serializer, reader));
} catch (IOException e) {
observer.onError(new MatchbookSDKHttpException(e.getMessage(), e));
}
}

protected <REQ extends RestRequest, RESP extends RestResponse<T>, T> void putRequest(String url,
protected <REQ extends RestRequest, RESP extends RestResponse, T> void putRequest(
String url,
REQ request,
StreamObserver<T> observer,
Class<RESP> responseClass) {
Reader<T, RESP> reader) {
try {
Serializer serializer = connectionManager.getSerializer();
String requestBody = serializer.writeObjectAsString(request);
connectionManager.getHttpClient().put(url, requestBody, new RestCallback<>(observer, responseClass, serializer));
connectionManager.getHttpClient().put(url, requestBody, new RestCallback<>(observer, serializer, reader));
} catch (IOException e) {
observer.onError(new MatchbookSDKHttpException(e.getMessage(), e));
}
}

protected <REQ extends RestRequest, RESP extends RestResponse<T>, T> void deleteRequest(String url,
protected <REQ extends RestRequest, RESP extends RestResponse, T> void deleteRequest(
String url,
REQ request,
StreamObserver<T> observer,
Class<RESP> responseClass) {
Reader<T, RESP> reader) {
String requestUrl = buildUrl(url, request);
Serializer serializer = connectionManager.getSerializer();
connectionManager.getHttpClient().delete(requestUrl, new RestCallback<>(observer, responseClass, serializer));
connectionManager.getHttpClient().delete(requestUrl, new RestCallback<>(observer, serializer, reader));
}

protected String buildSportsUrl(String path) {
Expand All @@ -77,26 +84,29 @@ private <REQ extends RestRequest> String buildUrl(String baseUrl, REQ request) {
.collect(Collectors.joining("&"));
}

private class RestCallback<T, RESP extends RestResponse<T>> implements HttpCallback {
private static class RestCallback<T, RESP extends RestResponse> implements HttpCallback {

private final StreamObserver<T> observer;
private final Class<RESP> responseClass;
private final Serializer serializer;
private final Reader<T, RESP> reader;

private RestCallback(StreamObserver<T> observer, Class<RESP> responseClass, Serializer serializer) {
private RestCallback(StreamObserver<T> observer, Serializer serializer, Reader<T, RESP> reader) {
this.observer = observer;
this.responseClass = responseClass;
this.serializer = serializer;
this.reader = reader;
}

@Override
public void onResponse(InputStream inputStream) {
try {
RestResponse<T> restResponse = serializer.readObject(inputStream, responseClass);
restResponse.getContent().forEach(observer::onNext);
try (Parser parser = serializer.newParser(inputStream)) {
reader.startReading(parser);
while (reader.hasMoreItems()) {
T item = reader.readNextItem();
observer.onNext(item);
}
observer.onCompleted();
} catch (IOException e) {
MatchbookSDKHttpException exception = new MatchbookSDKHttpException(e.getMessage(), e);
MatchbookSDKParsingException exception = new MatchbookSDKParsingException(e.getMessage(), e);
observer.onError(exception);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public String toString() {
", password=" + Arrays.toString(password) +
", sportsUrl=" + sportsUrl +
", loginUrl=" + loginUrl +
", httpClientConfig=" + httpConfig +
", httpConfig=" + httpConfig +
"}";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
package com.matchbook.sdk.rest;

import java.io.Closeable;
import java.util.Objects;

import com.matchbook.sdk.rest.configs.HttpClient;
import com.matchbook.sdk.rest.configs.Serializer;
import com.matchbook.sdk.rest.configs.wrappers.HttpClientWrapper;
import com.matchbook.sdk.rest.configs.wrappers.SerializerWrapper;
import com.matchbook.sdk.rest.workers.SessionManager;
import com.matchbook.sdk.rest.workers.SessionKeepAliveScheduler;

public class ConnectionManager implements Closeable {

public class ConnectionManager {
private final ClientConfig clientConfig;
private final HttpClient httpClient;
private final Serializer serializer;

private ClientConfig clientConfig;
private HttpClient httpClient;
private Serializer serializer;
private SessionKeepAliveScheduler sessionKeepAliveScheduler;

public ConnectionManager(ConnectionManager.Builder builder) {
private ConnectionManager(ConnectionManager.Builder builder) {
this.clientConfig = builder.clientConfig;
this.httpClient = builder.httpClient;
this.serializer = builder.serializer;

if (builder.sessionAutoManage) {
SessionManager sessionManager = new SessionManager(this);
sessionManager.keepAlive();
if (builder.autoManageSession) {
sessionKeepAliveScheduler = new SessionKeepAliveScheduler(this);
sessionKeepAliveScheduler.start();
}
}

Expand All @@ -35,24 +40,32 @@ public Serializer getSerializer() {
return serializer;
}

@Override
public void close() {
httpClient.close();
if (Objects.nonNull(sessionKeepAliveScheduler)) {
sessionKeepAliveScheduler.stop();
}
}

public static class Builder {

private final ClientConfig clientConfig;
private final HttpClient httpClient;
private final Serializer serializer;

private boolean sessionAutoManage = true;

public Builder sessionAutoManage(boolean sessionAutoManage) {
this.sessionAutoManage = sessionAutoManage;
return this;
}
private boolean autoManageSession;

public Builder(ClientConfig clientConfig) {
this.clientConfig = clientConfig;

this.httpClient = new HttpClientWrapper(clientConfig.getHttpConfig());
this.serializer = new SerializerWrapper();
autoManageSession = true;
}

public Builder autoManageSession(boolean autoManageSession) {
this.autoManageSession = autoManageSession;
return this;
}

public ConnectionManager build() {
Expand Down
Loading

0 comments on commit 26777ad

Please sign in to comment.