Skip to content

Commit

Permalink
Merge pull request #4 from vittalpai/master
Browse files Browse the repository at this point in the history
Make SDK compatible with Liveupdate Microservice
  • Loading branch information
vittalpai authored Mar 17, 2020
2 parents 45f80ab + 884fb26 commit ea6ef46
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@

PUBLISH_GROUP_ID = com.ibm.mobile.foundation
PUBLISH_ARTIFACT_ID = ibmmobilefirstplatformfoundationliveupdate
PUBLISH_VERSION = 8.0.0
PUBLISH_VERSION = 8.0.202003051505
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ android {
minSdkVersion 16
targetSdkVersion 23
versionCode 8
versionName "8.0.0"
versionName "8.0.202003051505"
}

buildTypes {
Expand All @@ -51,7 +51,7 @@ dependencies {

compile group: 'com.ibm.mobile.foundation',
name: 'ibmmobilefirstplatformfoundation',
version: '8.0.+',
version: '8.0.202003051505',
ext: 'aar',
transitive: true
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
*/
package com.worklight.ibmmobilefirstplatformfoundationliveupdate;

import android.content.Context;
import android.webkit.URLUtil;

import com.worklight.common.Logger;
import com.worklight.ibmmobilefirstplatformfoundationliveupdate.api.Configuration;
import com.worklight.ibmmobilefirstplatformfoundationliveupdate.api.ConfigurationListener;
import com.worklight.ibmmobilefirstplatformfoundationliveupdate.cache.LocalCache;
import com.worklight.wlclient.api.WLClient;
import com.worklight.wlclient.api.WLFailResponse;
import com.worklight.wlclient.api.WLResourceRequest;
import com.worklight.wlclient.api.WLResponse;
Expand All @@ -27,6 +31,7 @@
import org.json.JSONObject;

import java.net.URI;
import java.net.URL;
import java.util.Map;

/**
Expand All @@ -40,20 +45,80 @@

public class LiveUpdateManager {

private static LiveUpdateManager instance = new LiveUpdateManager();
private final static String CONFIGURATION_SCOPE = "configuration-user-login";
private final String SERVICE_URL = "adapters/liveUpdateAdapter/configuration";
private static LiveUpdateManager instance = null;
private final static String LIVEUPDATE_CLIENT_SCOPE = "liveupdate.mobileclient";
private String SERVICE_URL;


private static final Logger logger = Logger.getInstance(LiveUpdateManager.class.getName());
/**
* getInstance
*
* @param context Android {@link Context}
* @return LiveUpdateManager singleton instance
*/
public static LiveUpdateManager getInstance() {
public static synchronized LiveUpdateManager getInstance(Context context) {
if (instance == null) {
instance = new LiveUpdateManager(context);
}
return instance;
}

private LiveUpdateManager() {
private LiveUpdateManager(Context context) {
try {
WLClient client = null;
// Get the applicationId and backend route from core
try {
client = WLClient.getInstance();
} catch (Exception e) {
client = WLClient.createInstance(context);
}
URL url = client.getServerUrl();
StringBuilder routeBuilder = new StringBuilder();
routeBuilder.append(url.getProtocol());
routeBuilder.append("://");
routeBuilder.append(url.getHost());
int port = url.getPort();
if(port != -1) {
routeBuilder.append(":");
routeBuilder.append(port);
}
String urlPath = url.getPath();
String[] split = urlPath.split("/api");
String contextRoute = urlPath.substring(0, split[0].lastIndexOf( "/" ));
routeBuilder.append(contextRoute);

String applicationRoute = routeBuilder.toString();
String packageName = context.getPackageName();
SERVICE_URL = applicationRoute + "/mfpliveupdate/v1/" + packageName + "/configuration";
if (!URLUtil.isValidUrl(SERVICE_URL)) {
throw new RuntimeException("LiveUpdateManager:initialize() - An error occured while initializing Liveupdate service. Reason : Invalid HOST URL");
}
} catch (Exception e) {
logger.error("LiveUpdateManager:initialize() - An error occured while initializing Liveupdate service.");
throw new RuntimeException(e);
}
}

/**
* obtainConfiguration - obtains a configuration from server/cache
* @param configurationListener - the configuration listener for receiving the configuration
*/
public void obtainConfiguration (ConfigurationListener configurationListener) {
this.obtainConfiguration(true, configurationListener);
}

/**
* obtainConfiguration - obtains a configuration from server / cache
* @param useCache - true to use cache, false to always obtains from server
* @param configurationListener - the configuration listener for receiving the configuration
*/
public void obtainConfiguration (boolean useCache, ConfigurationListener configurationListener) {
URI url = URI.create(SERVICE_URL + "/all");


logger.debug("obtainConfiguration: useCache = " + useCache + ", url = " + url);
this.obtainConfiguration("all", url, null, useCache, configurationListener);
}

/**
Expand All @@ -64,7 +129,7 @@ private LiveUpdateManager() {
* @param segmentId - the segment id
* @param configurationListener - the configuration listener for receiving the configuration
*/
public void obtainConfiguration (String segmentId, ConfigurationListener configurationListener) {
private void obtainConfiguration (String segmentId, ConfigurationListener configurationListener) {
this.obtainConfiguration(segmentId, true, configurationListener);
}

Expand All @@ -76,7 +141,7 @@ public void obtainConfiguration (String segmentId, ConfigurationListener configu
* @param params - the params used by the server to return a configuration.
* @param configurationListener - the configuration listener for receiving the configuration
*/
public void obtainConfiguration (Map<String,String> params, ConfigurationListener configurationListener) {
private void obtainConfiguration (Map<String,String> params, ConfigurationListener configurationListener) {
this.obtainConfiguration(params, true, configurationListener);
}

Expand All @@ -86,7 +151,7 @@ public void obtainConfiguration (Map<String,String> params, ConfigurationListene
* @param useCache - true to use cache, false to always obtains from server
* @param configurationListener - the configuration listener for receiving the configuration
*/
public void obtainConfiguration (String segmentId, boolean useCache, ConfigurationListener configurationListener) {
private void obtainConfiguration (String segmentId, boolean useCache, ConfigurationListener configurationListener) {
URI url = URI.create(SERVICE_URL + "/" + segmentId);


Expand All @@ -100,7 +165,7 @@ public void obtainConfiguration (String segmentId, boolean useCache, Configurati
* @param useCache - true to use cache, false to always obtain configuration from server
* @param configurationListener - the configuration listener for receiving the configuration
*/
public void obtainConfiguration (Map<String,String> params, boolean useCache, ConfigurationListener configurationListener) {
private void obtainConfiguration (Map<String,String> params, boolean useCache, ConfigurationListener configurationListener) {
URI url = URI.create(SERVICE_URL);
String id = buildIDFromParams(params);

Expand All @@ -122,7 +187,7 @@ private void obtainConfiguration (String id, URI url, Map<String,String> params,


private void sendConfigRequest(final String id, URI url, Map<String,String> params, final ConfigurationListener configurationListener) {
WLResourceRequest configurationServiceRequest = new WLResourceRequest(url, WLResourceRequest.GET, CONFIGURATION_SCOPE);
WLResourceRequest configurationServiceRequest = new WLResourceRequest(url, WLResourceRequest.GET, LIVEUPDATE_CLIENT_SCOPE);

logger.trace("sendConfigRequest: id = " + id + ", url = " + url + "params = " + params);

Expand Down
36 changes: 8 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ MobileFirst Foundation - LiveUpdate Android SDK

###Contents
LiveUpdate Android SDK lets you query runtime configuration properties and features which you set in the LiveUpdate Settings screen in the MobileFirst Operations Console.
With LiveUpdate integrated in your application you can implement feature toggling, A/B testing, feature segmentation and more.
With LiveUpdate integrated in your application you can implement feature toggling and more.

To learn more on how to use the Live Update SDK see following the [tutorial](https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/using-the-mfpf-sdk/live-update/).
To learn more on how to use the Live Update SDK see following the [tutorial](https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/application-development/live-update-service/).

###Installation

Expand All @@ -22,41 +22,21 @@ dependencies {
compile group: 'com.ibm.mobile.foundation',
name: 'ibmmobilefirstplatformfoundationliveupdate',
version: '8.0.0',
version: '8.0.+',
ext: 'aar',
transitive: true
}
```

### Configuration In MobileFirst Operation Console
In your application under Scope-Elements Mapping in security tab you must map the scope 'configuration-user-login' to security check, you can map it to empty string if you want to use the default protection. More info about [scope mapping](https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/authentication-and-security/authorization-concepts/#scope-mapping)
In your application under Scope-Elements Mapping in security tab you must map the scope 'liveupdate.mobileclient' to security check, you can map it to empty string if you want to use the default protection. More info about [scope mapping](https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/authentication-and-security/authorization-concepts/#scope-mapping)

### Sample Usages Of The API

#### Obtain Configuration By Segment :
#### Obtain Configuration:

```Java
LiveUpdateManager.getInstance().obtainConfiguration("segment1", new ConfigurationListener() {

@Override
public void onSuccess(final Configuration configuration) {
Log.i("LiveUpdateSample", configuration.getProperty("property1"));
Log.i("LiveUpdateSample", configuration.isFeatureEnabled("feature1").toString());
}

@Override
public void onFailure(WLFailResponse wlFailResponse) {
Log.e("LiveUpdateSample", wlFailResponse.getErrorMsg());
}
});
```

#### Obtain Configuration By Params :

```Java
LiveUpdateManager.getInstance().obtainConfiguration(new HashMap<String, String>() {{
put("paramKey","paramValue");
}}, new ConfigurationListener() {
LiveUpdateManager.getInstance().obtainConfiguration( new ConfigurationListener() {

@Override
public void onSuccess(final Configuration configuration) {
Expand All @@ -75,7 +55,7 @@ LiveUpdateManager.getInstance().obtainConfiguration(new HashMap<String, String>(
#### Disable cache (by default the cache is enabled):

```Java
LiveUpdateManager.getInstance().obtainConfiguration("segment1", false, new ConfigurationListener() {
LiveUpdateManager.getInstance().obtainConfiguration(false, new ConfigurationListener() {

@Override
public void onSuccess(final Configuration configuration) {
Expand All @@ -93,7 +73,7 @@ LiveUpdateManager.getInstance().obtainConfiguration("segment1", false, new Confi
###Supported Levels
- API Level: 16 Android 4.1 and above

Copyright 2016 IBM Corp.
Copyright 2020 IBM Corp.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down

0 comments on commit ea6ef46

Please sign in to comment.