Skip to content

Commit

Permalink
Add support for EU tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Serrano committed Aug 4, 2021
1 parent 3118305 commit c6e6049
Show file tree
Hide file tree
Showing 24 changed files with 315 additions and 48 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## 2.21.0 (2021-08-04)

- Add support for EU tokens

## 2.20.0 (2021-06-29)

- Add setSurveyedDefault method
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -28,14 +28,14 @@ If you use Maven, you can include this library as a dependency:
<dependency>
<groupId>com.wootric</groupId>
<artifactId>wootric-sdk-android</artifactId>
<version>2.20.0</version>
<version>2.21.0</version>
</dependency>
```

### Using Gradle

```xml
implementation 'com.wootric:wootric-sdk-android:2.20.0'
implementation 'com.wootric:wootric-sdk-android:2.21.0'
```

## Initializing Wootric
Expand Down
4 changes: 2 additions & 2 deletions androidsdk/src/main/java/com/wootric/androidsdk/Wootric.java
Expand Up @@ -479,7 +479,7 @@ public void survey(String eventName) {
}

OfflineDataHandler offlineDataHandler = new OfflineDataHandler(preferencesUtils);
WootricRemoteClient wootricRemoteClient = new WootricRemoteClient(offlineDataHandler);
WootricRemoteClient wootricRemoteClient = new WootricRemoteClient(offlineDataHandler, user.getAccountToken());
SurveyValidator surveyValidator = buildSurveyValidator();

if (weakFragmentActivity != null) {
Expand All @@ -505,7 +505,7 @@ public void showSurveyInActivity(Activity activity, String eventName) {
}

OfflineDataHandler offlineDataHandler = new OfflineDataHandler(preferencesUtils);
WootricRemoteClient wootricRemoteClient = new WootricRemoteClient(offlineDataHandler);
WootricRemoteClient wootricRemoteClient = new WootricRemoteClient(offlineDataHandler, user.getAccountToken());
SurveyValidator surveyValidator = buildSurveyValidator();

if (weakFragmentActivity != null) {
Expand Down
Expand Up @@ -41,37 +41,39 @@
*/
public class WootricRemoteClient {
private final OfflineDataHandler offlineDataHandler;
private final String accountToken;

public WootricRemoteClient(OfflineDataHandler offlineDataHandler) {
public WootricRemoteClient(OfflineDataHandler offlineDataHandler, String accountToken) {
this.offlineDataHandler = offlineDataHandler;
this.accountToken = accountToken;
}

public void checkEligibility(User user, EndUser endUser, Settings settings, PreferencesUtils preferencesUtils, final CheckEligibilityTask.Callback surveyCallback) {
new CheckEligibilityTask(user, endUser, settings, preferencesUtils, surveyCallback).execute();
}

public void authenticate(User user, final WootricApiCallback wootricApiCallback) {
new GetAccessTokenTask(user.getClientId(), wootricApiCallback).execute();
new GetAccessTokenTask(user.getClientId(), this.accountToken, wootricApiCallback).execute();
}

public void getEndUserByEmail(String email, String accessToken, final WootricApiCallback wootricApiCallback) {
new GetEndUserByEmailTask(email, accessToken, wootricApiCallback).execute();
new GetEndUserByEmailTask(email, accessToken, this.accountToken, wootricApiCallback).execute();
}

public void createEndUser(EndUser endUser, String accessToken, final WootricApiCallback wootricApiCallback) {
new CreateEndUserTask(endUser, accessToken, wootricApiCallback).execute();
new CreateEndUserTask(endUser, accessToken, this.accountToken, wootricApiCallback).execute();
}

public void updateEndUser(EndUser endUser, String accessToken, final WootricApiCallback wootricApiCallback) {
new UpdateEndUserTask(endUser, accessToken, wootricApiCallback).execute();
new UpdateEndUserTask(endUser, accessToken, this.accountToken, wootricApiCallback).execute();
}

public void createDecline(long endUserId, long userId, long accountId, int priority, String accessToken, String originUrl, String uniqueLink) {
new CreateDeclineTask(endUserId, userId, accountId, priority, originUrl, accessToken, offlineDataHandler, uniqueLink).execute();
new CreateDeclineTask(endUserId, userId, accountId, priority, originUrl, accessToken, this.accountToken, offlineDataHandler, uniqueLink).execute();
}

public void createResponse(long endUserId, long userId, long accountId, String accessToken, String originUrl, int score, int priority, String text, String uniqueLink) {
new CreateResponseTask(endUserId, userId, accountId, originUrl, score, priority, text, accessToken, offlineDataHandler, uniqueLink).execute();
new CreateResponseTask(endUserId, userId, accountId, originUrl, score, priority, text, accessToken, this.accountToken, offlineDataHandler, uniqueLink).execute();
}

public void getRegisteredEvents(User user, final GetRegisteredEventsTask.Callback surveyCallback) {
Expand Down
Expand Up @@ -50,7 +50,7 @@ public class CheckEligibilityTask extends WootricRemoteRequestTask {
private final Callback surveyCallback;

public CheckEligibilityTask(User user, EndUser endUser, Settings settings, PreferencesUtils preferencesUtils, Callback surveyCallback) {
super(REQUEST_TYPE_GET, null, null);
super(REQUEST_TYPE_GET, null, user.getAccountToken(), null);

this.user = user;
this.endUser = endUser;
Expand Down Expand Up @@ -102,7 +102,7 @@ protected void buildParams() {

@Override
protected String requestUrl() {
return ELIGIBLE_URL;
return getSurveyEndpoint() + ELIGIBLE_PATH;
}

@Override
Expand Down
Expand Up @@ -59,8 +59,8 @@ public class CreateDeclineTask extends WootricRemoteRequestTask {
private final OfflineDataHandler offlineDataHandler;
private final String uniqueLink;

public CreateDeclineTask(long endUserId, long userId, long accountId, int priority, String originUrl, String accessToken, OfflineDataHandler offlineDataHandler, String uniqueLink) {
super(REQUEST_TYPE_POST, accessToken, null);
public CreateDeclineTask(long endUserId, long userId, long accountId, int priority, String originUrl, String accessToken, String accountToken, OfflineDataHandler offlineDataHandler, String uniqueLink) {
super(REQUEST_TYPE_POST, accessToken, accountToken, null);

this.originUrl = originUrl;
this.endUserId = endUserId;
Expand All @@ -86,7 +86,7 @@ protected void buildParams() {

@Override
protected String requestUrl() {
return END_USERS_URL + "/" + String.valueOf(endUserId) + "/declines";
return getApiEndpoint() + END_USERS_PATH + "/" + endUserId + "/declines";
}

@Override
Expand Down
Expand Up @@ -36,14 +36,14 @@
public class CreateEndUserTask extends WootricRemoteRequestTask {
private final EndUser endUser;

public CreateEndUserTask(EndUser endUser, String accessToken, WootricApiCallback wootricApiCallback) {
super(REQUEST_TYPE_POST, accessToken, wootricApiCallback);
public CreateEndUserTask(EndUser endUser, String accessToken, String accountToken, WootricApiCallback wootricApiCallback) {
super(REQUEST_TYPE_POST, accessToken, accountToken, wootricApiCallback);
this.endUser = endUser;
}

@Override
protected String requestUrl() {
return END_USERS_URL;
return getApiEndpoint() + END_USERS_PATH;
}

@Override
Expand Down
Expand Up @@ -41,8 +41,8 @@ public class CreateResponseTask extends WootricRemoteRequestTask {

private final OfflineDataHandler offlineDataHandler;

public CreateResponseTask(long endUserId, long userId, long accountId, String originUrl, int score, int priority, String text, String accessToken, OfflineDataHandler offlineDataHandler, String uniqueLink) {
super(REQUEST_TYPE_POST, accessToken, null);
public CreateResponseTask(long endUserId, long userId, long accountId, String originUrl, int score, int priority, String text, String accessToken, String accountToken, OfflineDataHandler offlineDataHandler, String uniqueLink) {
super(REQUEST_TYPE_POST, accessToken, accountToken, null);

this.endUserId = endUserId;
this.userId = userId;
Expand All @@ -57,7 +57,7 @@ public CreateResponseTask(long endUserId, long userId, long accountId, String or

@Override
protected String requestUrl() {
return END_USERS_URL + "/" + String.valueOf(endUserId) + "/responses";
return getApiEndpoint() + END_USERS_PATH + "/" + endUserId + "/responses";
}

@Override
Expand Down
Expand Up @@ -34,15 +34,13 @@ public class GetAccessTokenTask extends WootricRemoteRequestTask {

private final String clientId;

public GetAccessTokenTask(String clientId, WootricApiCallback wootricApiCallback) {
super(REQUEST_TYPE_POST, null, wootricApiCallback);
public GetAccessTokenTask(String clientId, String accountToken, WootricApiCallback wootricApiCallback) {
super(REQUEST_TYPE_POST, null, accountToken, wootricApiCallback);
this.clientId = clientId;
}

@Override
protected String requestUrl() {
return OAUTH_URL;
}
protected String requestUrl() { return getApiEndpoint() + OAUTH_PATH; }

@Override
protected void buildParams() {
Expand Down
Expand Up @@ -36,14 +36,14 @@ public class GetEndUserByEmailTask extends WootricRemoteRequestTask {

private final String email;

public GetEndUserByEmailTask(String email, String accessToken, WootricApiCallback wootricApiCallback) {
super(REQUEST_TYPE_GET, accessToken, wootricApiCallback);
public GetEndUserByEmailTask(String email, String accessToken, String accountToken, WootricApiCallback wootricApiCallback) {
super(REQUEST_TYPE_GET, accessToken, accountToken, wootricApiCallback);
this.email = email;
}

@Override
protected String requestUrl() {
return END_USERS_URL;
return getApiEndpoint() + END_USERS_PATH;
}

@Override
Expand Down
Expand Up @@ -38,7 +38,7 @@ public class GetRegisteredEventsTask extends WootricRemoteRequestTask {
private final GetRegisteredEventsTask.Callback surveyCallback;

public GetRegisteredEventsTask(User user, GetRegisteredEventsTask.Callback surveyCallback) {
super(REQUEST_TYPE_GET, null, null);
super(REQUEST_TYPE_GET, null, user.getAccountToken(), null);

this.user = user;
this.surveyCallback = surveyCallback;
Expand All @@ -53,7 +53,7 @@ protected void buildParams() {

@Override
protected String requestUrl() {
return REGISTERED_EVENTS_URL;
return getSurveyEndpoint() + REGISTERED_EVENTS_PATH;
}

@Override
Expand Down
Expand Up @@ -33,14 +33,14 @@
public class UpdateEndUserTask extends WootricRemoteRequestTask {
private final EndUser endUser;

public UpdateEndUserTask(EndUser endUser, String accessToken, WootricApiCallback wootricApiCallback) {
super( REQUEST_TYPE_PUT, accessToken, wootricApiCallback);
public UpdateEndUserTask(EndUser endUser, String accessToken, String accountToken, WootricApiCallback wootricApiCallback) {
super(REQUEST_TYPE_PUT, accessToken, accountToken, wootricApiCallback);
this.endUser = endUser;
}

@Override
protected String requestUrl() {
return END_USERS_URL + "/" + String.valueOf(endUser.getId());
return getApiEndpoint() + END_USERS_PATH + "/" + endUser.getId();
}

@Override
Expand Down
Expand Up @@ -29,6 +29,7 @@

import com.wootric.androidsdk.BuildConfig;
import com.wootric.androidsdk.network.WootricApiCallback;
import com.wootric.androidsdk.utils.Utils;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -50,20 +51,24 @@ public abstract class WootricRemoteRequestTask extends AsyncTask<Void, Void, Str

protected static final String API_ENDPOINT = "https://api.wootric.com";
protected static final String SURVEY_ENDPOINT = "https://survey.wootric.com";
protected static final String END_USERS_URL = API_ENDPOINT + "/v1/end_users";
protected static final String OAUTH_URL = API_ENDPOINT + "/oauth/token";
protected static final String ELIGIBLE_URL = SURVEY_ENDPOINT + "/eligible.json";
protected static final String REGISTERED_EVENTS_URL = SURVEY_ENDPOINT + "/registered_events.json";
protected static final String EU_API_ENDPOINT = "https://app.wootric.eu";
protected static final String EU_SURVEY_ENDPOINT = "https://eligibility.wootric.eu";
protected static final String END_USERS_PATH = "/api/v1/end_users";
protected static final String OAUTH_PATH = "/oauth/token";
protected static final String ELIGIBLE_PATH = "/eligible.json";
protected static final String REGISTERED_EVENTS_PATH = "/registered_events.json";

private final String requestType;
private final String accessToken;
private final String accountToken;
protected final WootricApiCallback wootricApiCallback;
protected final HashMap<String, String> paramsMap;


public WootricRemoteRequestTask(String requestType, String accessToken, WootricApiCallback wootricApiCallback) {
public WootricRemoteRequestTask(String requestType, String accessToken, String accountToken, WootricApiCallback wootricApiCallback) {
this.requestType = requestType;
this.accessToken = accessToken;
this.accountToken = accountToken;
this.wootricApiCallback = wootricApiCallback;
this.paramsMap = new HashMap<>();
}
Expand Down Expand Up @@ -163,6 +168,14 @@ protected void onInvalidResponse(String message) {
}
}

protected String getApiEndpoint() {
return Utils.startsWithEU(this.accountToken) ? EU_API_ENDPOINT : API_ENDPOINT;
}

protected String getSurveyEndpoint() {
return Utils.startsWithEU(this.accountToken) ? EU_SURVEY_ENDPOINT : SURVEY_ENDPOINT;
}

protected abstract String requestUrl();
protected abstract void buildParams();
protected void onSuccess(String response) { }
Expand Down
Expand Up @@ -75,4 +75,8 @@ public static byte getByteValue(Boolean bool) {
return (byte) 0;
}
}

public static boolean startsWithEU(String aString) {
return aString.startsWith("NPS-EU");
}
}
Expand Up @@ -52,6 +52,7 @@
import com.wootric.androidsdk.utils.SHAUtil;
import com.wootric.androidsdk.utils.ScreenUtils;
import com.wootric.androidsdk.utils.SocialHandler;
import com.wootric.androidsdk.utils.Utils;
import com.wootric.androidsdk.views.phone.ThankYouDialogFactory;

import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -131,7 +132,7 @@ public void onCreate(Bundle savedInstanceState) {
mSocialHandler = new SocialHandler(getActivity());
PreferencesUtils prefUtils = new PreferencesUtils(new WeakReference<Context>(this.getActivity()));
OfflineDataHandler offlineDataHandler = new OfflineDataHandler(prefUtils);
mWootricApiClient = new WootricRemoteClient(offlineDataHandler);
mWootricApiClient = new WootricRemoteClient(offlineDataHandler, mUser.getAccountToken());

mIsTablet = getResources().getBoolean(R.bool.isTablet);

Expand Down Expand Up @@ -386,9 +387,10 @@ private void notifySurveyFinished() {
}

private void optOut() {
String optOutUrl = "https://app.wootric.com/opt_out?token=" + mUser.getAccountToken()
+ "&metric_type=" + mSettings.getSurveyType()
+ "&end_user_id=" + Long.toString(mEndUser.getId())
String tld = Utils.startsWithEU(mUser.getAccountToken()) ? "eu" : "com";
String optOutUrl = "https://app.wootric." + tld + "/opt_out?token=" + mUser.getAccountToken()
+ "&metric_type=" + mSettings.getSurveyType()
+ "&end_user_id=" + mEndUser.getId()
+ "&end_user_email=" + mEndUser.getEmail()
+ "&unique_link=" + mUniqueLink
+ "&opt_out_token=" + mAccessToken;
Expand Down
Expand Up @@ -52,6 +52,7 @@
import com.wootric.androidsdk.utils.SHAUtil;
import com.wootric.androidsdk.utils.ScreenUtils;
import com.wootric.androidsdk.utils.SocialHandler;
import com.wootric.androidsdk.utils.Utils;
import com.wootric.androidsdk.views.OnSurveyFinishedListener;
import com.wootric.androidsdk.views.SurveyLayout;
import com.wootric.androidsdk.views.SurveyLayoutListener;
Expand Down Expand Up @@ -132,7 +133,7 @@ public void onCreate(Bundle savedInstanceState) {
mSocialHandler = new SocialHandler(getActivity());
PreferencesUtils prefUtils = new PreferencesUtils(new WeakReference<Context>(this.getActivity()));
OfflineDataHandler offlineDataHandler = new OfflineDataHandler(prefUtils);
mWootricApiClient = new WootricRemoteClient(offlineDataHandler);
mWootricApiClient = new WootricRemoteClient(offlineDataHandler, mUser.getAccountToken());

mIsTablet = getResources().getBoolean(R.bool.isTablet);

Expand Down Expand Up @@ -388,7 +389,8 @@ private void notifySurveyFinished() {
}

private void optOut() {
String optOutUrl = "https://app.wootric.com/opt_out?token=" + mUser.getAccountToken()
String tld = Utils.startsWithEU(mUser.getAccountToken()) ? "eu" : "com";
String optOutUrl = "https://app.wootric." + tld + "/opt_out?token=" + mUser.getAccountToken()
+ "&metric_type=" + mSettings.getSurveyType()
+ "&end_user_id=" + Long.toString(mEndUser.getId())
+ "&end_user_email=" + mEndUser.getEmail()
Expand Down

0 comments on commit c6e6049

Please sign in to comment.