The core app provides the ability to define multiple OAuth clients for different urls. You can change the base URL from the Settings Screen inside the app and the relevant OAuth client settings will be picked up.
Click the three-dot menu on the login page and you will be navigated to the Settings Screen, where you can click on OpenSRP base URL and will be presented with the following.
To support switching between multiple environments, extend PropertiesSyncConfiguration
for the implementation of SyncConfiguration
. PropertiesSyncConfiguration
depends on EnvironmentManager
to load different Environment
's.
Environment
is a POJO that defines the OAuth client.secret
, client.id
against a url
and a name
.
The EnvironmentManager
class requires a serialized JSON array of Environment
objects. You can load this JSON Array from your assets folder or generate it using build.gradle
and local.properties
. Once you have a serialized JSON Array initialize EnvironmentManager
using it.
After implementing the PropertiesSyncConfiguration
and initializing the CoreLibrary
with it. The app automatically picks the client.id
and client.secret
for the base URL stored in preference at DRISHTI_BASE_URL
and use it for subsequent calls.
In your Application onCreate
initialize CoreLibary
with the SyncConfiguration
String json = loadEnvironments(); EnvironmentManager envManager = new EnvironmentManager(json); SyncConfiguration syncConfig = new MyPropertiesSyncConfiguration(envManager); CoreLibrary.init(context, syncConfig, timeStamp, new P2POptions(true)); |
MyPropertiesSyncConfiguration.java
class MyPropertiesSyncConfiguration extends PropertiesSyncConfiguration { //Implement abstract methods ... } |
Sample JSON Array defining the environmets
[ { "env": "preview", "client.id": "preview-server", "client.secret": "19aaaf41-eac2-4ef9-a537-4fe62416b522", "url": "https://preview.smartregister.org/opensrp/" }, { "env": "qa", "client.id": "qa-server", "client.secret": "7a89e3fe-99fe-4af8-a699-c213192c634a", "url": "https://qa.smartregister.org/opensrp/" }, { "env": "stage", "client.id": "stage-server", "client.secret": "7d68f02d-7c32-4e3f-9c48-065dc2a6b8d0", "url": "https://stage.smartregister.org/opensrp" } ] |
Once you have this in place the app will automatically pick the client.id
and client.secret
for the base URL stored in preference at DRISHTI_BASE_URL
and all subsequent calls that need OAuth configs will use these.
In your local.properties
file define the environments as:
#Stage oauth.client.stage.id="stage-server" oauth.client.stage.secret="7d68f02d-7c32-4e3f-9c48-065dc2a6b8d0" oauth.client.stage.url="https://stage.smartregister.org/opensrp" #QA oauth.client.qa.id="qa-server" oauth.client.qa.secret="7a89e3fe-99fe-4af8-a699-c213192c634a" oauth.client.qa.url="https://qa.smartregister.org/opensrp/" #Preview oauth.client.preview.id="preview-server" oauth.client.preview.secret="19aaaf41-eac2-4ef9-a537-4fe62416b522" oauth.client.preview.url="https://preview.smartregister.org/opensrp/" |
Add the following to your app level build.gradle
:
Properties properties = new Properties() properties.load(project.rootProject.file("local.properties").newDataInputStream()) if (properties != null) { def envsArray = "[ " properties.toSorted().findAll { return it.key.toString().contains("oauth.client") && it.key.toString().tokenize(".").size() > 3 }.groupBy { return it.key.toString().contains("secret") }[true].each { def env = it.key.toString().tokenize(".")[2] envsArray += """${getJavaMap([ env: "$env", 'client.id': properties["oauth.client." + env + ".id"], 'client.secret': properties["oauth.client.+ env + ".secret"], url: properties["oauth.client." + env + ".url"] ])},""" } envsArray = envsArray.substring(0, envsArray.length() - 1) envsArray += "]" buildConfigField "String", "ENV_ARRAY", "\"${envsArray}\"" } else { buildConfigField "String", "ENV_ARRAY", '"[]"' } |
Define getJavaMap in your gradle file as:
private static String getJavaMap(apiUrlMap) { def hashMap = "{ " apiUrlMap.each { k, v -> hashMap += "\\\"${k}\\\":" + "\\\"${v.toString().replace("\"", '')}\\\"" + "," } hashMap = hashMap.substring(0, hashMap.length() - 1) return hashMap + "}" } |
You can now use BuildConig.ENV_ARRAY
to initialize EnviromentManageer
:
EnvironmentManager envManager = new EnvironmentManager(BuildConig.ENV_ARRAY); |