Skip to content

Commit

Permalink
Merge pull request #28 from LiveTex/custom_auth_endpoint
Browse files Browse the repository at this point in the history
Implemented custom auth endpoint support
  • Loading branch information
maxxx authored Apr 22, 2022
2 parents f6ef716 + 4e29b54 commit 116a5b5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ dependencies {
new LiveTex.Builder(Const.TOUCHPOINT).build();
```

Укажите Touchpoint (берется в личном кабинете).
Укажите Touchpoint (берется в личном кабинете). Если у вас свой эндпоинт авторизации, укажите его через setAuthEndpoint(endpoint).

Далее можно обращаться к синглтону через `LiveTex.getInstance()`.

Expand Down
2 changes: 1 addition & 1 deletion sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

// androidx
implementation "androidx.annotation:annotation:1.2.0"
implementation "androidx.annotation:annotation:1.3.0"
// network
implementation 'com.squareup.okhttp3:okhttp:3.14.7'
implementation 'com.squareup.okhttp3:logging-interceptor:3.14.7'
Expand Down
15 changes: 13 additions & 2 deletions sdk/src/main/java/ru/livetex/sdk/LiveTex.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public static class Builder {
@NonNull
private String host = "visitor-api.livetex.ru/";
@NonNull
private String authEndpoint = "https://visitor-api.livetex.ru/v1/auth";
@NonNull
private final String touchpoint;
@Nullable
private String deviceToken = null;
Expand All @@ -59,13 +61,22 @@ public Builder(@NonNull String touchpoint) {

/**
* Set custom host in format "[subdomain.]domain.zone/"
* Host used for auth request and for websocket connection (but can be changed by auth response)
* Host used for for websocket connection (mostly can be changed by auth response) and files upload
*/
public Builder setHost(@NonNull String host) {
this.host = host;
return this;
}

/**
* Set custom auth endpoint like "https://visitor-api.livetex.ru/v1/auth"
* Used for auth request
*/
public Builder setAuthEndpoint(@NonNull String authEndpoint) {
this.authEndpoint = authEndpoint;
return this;
}

/**
* deviceToken is unique device identifier. Used for pushes, so it should be Firebase token.
*/
Expand Down Expand Up @@ -108,7 +119,7 @@ public Builder setWebsocketLoggingEnabled() {

public void build() {
instance = new LiveTex(this);
NetworkManager.init(host, touchpoint, deviceToken, deviceType, isNetworkLoggingEnabled);
NetworkManager.init(host, authEndpoint, touchpoint, deviceToken, deviceType, isNetworkLoggingEnabled);
messageHandler.init(isWebsocketLoggingEnabled);
websocketListener.init();
}
Expand Down
14 changes: 8 additions & 6 deletions sdk/src/main/java/ru/livetex/sdk/network/NetworkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public enum ConnectionState {
}

// Endpoint for auth request.
private final String authHost;
// Endpoint for web socket connection. Can be changed by auth response.
private final String authEndpoint;
// Endpoint for web socket connection. Will be changed by auth response.
private String wsEndpoint;
// Endpoint for file upload. Can be changed by auth response.
private String uploadEndpoint;
Expand All @@ -67,12 +67,13 @@ public enum ConnectionState {
private boolean reconnectRequired = true;

private NetworkManager(@NonNull String host,
@NonNull String authEndpoint,
@NonNull String touchpoint,
@Nullable String deviceToken,
@Nullable String deviceType,
boolean isNetworkLoggingEnabled) {
this.authHost = "https://" + host + "v1/";
this.wsEndpoint = "wss://" + host + "v1/ws/{visitorToken}";
this.authEndpoint = authEndpoint;
this.wsEndpoint = "wss://" + host + "v1/ws/{visitorToken}"; // just a placeholder, will be replaced by auth response
this.uploadEndpoint = "https://" + host + "v1/upload";
this.touchpoint = touchpoint;
this.deviceToken = deviceToken;
Expand Down Expand Up @@ -113,11 +114,12 @@ private NetworkManager(@NonNull String host,
}

public static void init(@NonNull String host,
@NonNull String authEndpoint,
@NonNull String touchpoint,
String deviceToken,
String deviceType,
boolean isNetworkLoggingEnabled) {
instance = new NetworkManager(host, touchpoint, deviceToken, deviceType, isNetworkLoggingEnabled);
instance = new NetworkManager(host, authEndpoint, touchpoint, deviceToken, deviceType, isNetworkLoggingEnabled);
}

public static NetworkManager getInstance() {
Expand Down Expand Up @@ -279,7 +281,7 @@ private String auth(@NonNull String touchpoint,
@Nullable String deviceToken,
@Nullable String deviceType,
@Nullable String customVisitorToken) throws IOException {
HttpUrl.Builder urlBuilder = HttpUrl.parse(authHost + "auth")
HttpUrl.Builder urlBuilder = HttpUrl.parse(authEndpoint)
.newBuilder()
.addQueryParameter("touchPoint", touchpoint);

Expand Down

0 comments on commit 116a5b5

Please sign in to comment.