Skip to content

Commit 6aec4f4

Browse files
Abhi591rohitesh-wingify
authored andcommitted
feat: update settings on vwo client with or without webhook
1 parent c7bce3a commit 6aec4f4

10 files changed

Lines changed: 108 additions & 181 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
[1.8.0] - 2025-05-20
9+
10+
### Added
11+
- added new method `updateSettings` to update settings on the vwo client instance.
12+
813
## [1.7.0] - 2025-05-12
914

1015
### Added
@@ -198,4 +203,4 @@ Send event properties as third param in trackEvent() call`
198203
vwInitOptions.setSdkKey("sdk-key");
199204
vwInitOptions.setAccountId(1234);
200205
vwInitOptions.setPollInterval(60);
201-
```
206+
```

src/main/java/com/vwo/VWOClient.java

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.vwo.utils.SDKMetaUtil;
3434
import com.vwo.utils.SettingsUtil;
3535
import com.vwo.services.BatchEventQueue;
36+
import com.vwo.services.SettingsManager;
3637

3738
import java.util.HashMap;
3839
import java.util.Map;
@@ -76,19 +77,6 @@ public void setBatchEventQueue(BatchEventQueue batchEventQueue) {
7677
this.batchEventQueue = batchEventQueue;
7778
}
7879

79-
/**
80-
* This method is used to update the settings
81-
* @param newSettings New settings to be updated
82-
*/
83-
public void updateSettings(String newSettings) {
84-
try {
85-
this.processedSettings = objectMapper.readValue(newSettings, Settings.class);
86-
SettingsUtil.processSettings(this.processedSettings);
87-
} catch (Exception exception) {
88-
LoggerService.log(LogLevelEnum.ERROR, "Exception occurred while updating settings " + exception.getMessage());
89-
}
90-
}
91-
9280
/**
9381
* This method is used to get the flag value for the given feature key
9482
* @param featureKey Feature key for which the flag value is to be fetched
@@ -287,4 +275,87 @@ public boolean flushEvents() {
287275
return false;
288276
}
289277
}
278+
279+
/**
280+
* This method is used to update the settings on the VWOClient instance
281+
* It validates the new settings and updates the processedSettings
282+
* @param newSettings New settings to be updated
283+
*/
284+
private void updateSettingsOnVWOClient(String newSettings) {
285+
try {
286+
if (newSettings == null || newSettings.isEmpty()) {
287+
throw new IllegalArgumentException("Settings cannot be empty");
288+
}
289+
// Read the new settings and update the processedSettings
290+
this.processedSettings = objectMapper.readValue(newSettings, Settings.class);
291+
292+
// Check if the new settings are valid
293+
boolean settingsValid = new SettingsSchema().isSettingsValid(this.processedSettings);
294+
if (settingsValid) {
295+
// Process the new settings and update the client instance
296+
SettingsUtil.processSettings(this.processedSettings);
297+
} else {
298+
throw new IllegalStateException("Settings schema is invalid");
299+
}
300+
} catch (Exception exception) {
301+
throw new IllegalStateException(exception.getMessage());
302+
}
303+
}
304+
305+
/**
306+
* This method is used to update the settings by fetching from server
307+
*/
308+
public String updateSettings() {
309+
return this.updateSettings(true);
310+
}
311+
312+
/**
313+
* This method is used to update the settings with provided settings string
314+
* @param settings New settings to be updated
315+
*/
316+
public String updateSettings(String settings) {
317+
String apiName = "updateSettings";
318+
try {
319+
LoggerService.log(LogLevelEnum.DEBUG, "API_CALLED", new HashMap<String, String>() {{
320+
put("apiName", apiName);
321+
}});
322+
323+
String settingsToUpdate = settings;
324+
if (settings == null || settings.isEmpty()) {
325+
settingsToUpdate = this.updateSettings(true);
326+
}
327+
// Update the settings on the VWOClient instance
328+
this.updateSettingsOnVWOClient(settingsToUpdate);
329+
LoggerService.log(LogLevelEnum.INFO, "SETTINGS_UPDATED", new HashMap<String, String>() {{
330+
put("apiName", apiName);
331+
}});
332+
return settingsToUpdate;
333+
} catch (Exception exception) {
334+
LoggerService.log(LogLevelEnum.ERROR, "SETTINGS_FETCH_FAILED", new HashMap<String, String>() {{
335+
put("apiName", apiName);
336+
put("err", exception.toString());
337+
}});
338+
return null;
339+
}
340+
}
341+
342+
/**
343+
* This method is used to update the settings
344+
* @param isViaWebhook Boolean value to indicate if the settings are being fetched via webhook
345+
*/
346+
public String updateSettings(Boolean isViaWebhook) {
347+
String apiName = "updateSettings";
348+
try {
349+
// Fetch the new settings from the server
350+
this.settings = SettingsManager.getInstance().fetchSettings(isViaWebhook);
351+
return this.updateSettings(this.settings);
352+
} catch (Exception exception) {
353+
LoggerService.log(LogLevelEnum.ERROR, "SETTINGS_FETCH_FAILED", new HashMap<String, String>() {{
354+
put("apiName", apiName);
355+
put("isViaWebhook", isViaWebhook.toString());
356+
put("err", exception.toString());
357+
}});
358+
return null;
359+
}
360+
}
290361
}

src/main/java/com/vwo/constants/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class Constants {
3333

3434
public static final String HOST_NAME = "dev.visualwebsiteoptimizer.com";
3535
public static final String SETTINGS_ENDPOINT = "/server-side/v2-settings";
36+
public static final String WEBHOOK_SETTINGS_ENDPOINT = "/server-side/v2-pull";
3637

3738
public static final String VWO_FS_ENVIRONMENT = "vwo_fs_environment";
3839
public static final String HTTPS_PROTOCOL = "https";

src/main/java/com/vwo/models/BatchEventData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2024 Wingify Software Pvt. Ltd.
2+
* Copyright 2024-2025 Wingify Software Pvt. Ltd.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/main/java/com/vwo/models/FlushInterface.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2024 Wingify Software Pvt. Ltd.
2+
* Copyright 2024-2025 Wingify Software Pvt. Ltd.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/main/java/com/vwo/services/BatchEventQueue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2024 Wingify Software Pvt. Ltd.
2+
* Copyright 2024-2025 Wingify Software Pvt. Ltd.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/main/java/com/vwo/services/SettingsManager.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static SettingsManager getInstance() {
9090
*/
9191
private String fetchSettingsAndCacheInStorage() {
9292
try {
93-
return fetchSettings();
93+
return fetchSettings(false);
9494
} catch (Exception e) {
9595
LoggerService.log(LogLevelEnum.ERROR, "SETTINGS_FETCH_ERROR", new HashMap<String, String>() {
9696
{
@@ -105,7 +105,7 @@ private String fetchSettingsAndCacheInStorage() {
105105
* Fetches settings from the server
106106
* @return settings
107107
*/
108-
private String fetchSettings() {
108+
public String fetchSettings(Boolean isViaWebhook) {
109109
if (sdkKey == null || accountId == null) {
110110
throw new IllegalArgumentException("SDK Key and Account ID are required to fetch settings. Aborting!");
111111
}
@@ -118,8 +118,13 @@ private String fetchSettings() {
118118
options.put("s", "prod");
119119
}
120120

121+
String endpoint = Constants.SETTINGS_ENDPOINT;
122+
if (isViaWebhook) {
123+
endpoint = Constants.WEBHOOK_SETTINGS_ENDPOINT;
124+
}
125+
121126
try {
122-
RequestModel request = new RequestModel(hostname, "GET", Constants.SETTINGS_ENDPOINT, options, null, null, this.protocol, port);
127+
RequestModel request = new RequestModel(hostname, "GET", endpoint, options, null, null, this.protocol, port);
123128
request.setTimeout(networkTimeout);
124129

125130
ResponseModel response = networkInstance.get(request);

src/main/resources/error-messages.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323

2424
"GATEWAY_URL_ERROR": "Please provide a valid URL for VWO Gateway Service",
2525

26-
"NETWORK_CALL_FAILED": "Error occurred while sending {method} request. Error: {err}"
27-
}
26+
"NETWORK_CALL_FAILED": "Error occurred while sending {method} request. Error: {err}",
27+
"SETTINGS_FETCH_FAILED": "Failed to fetch settings and hence VWO client instance couldn't be updated when API: {apiName} got called having isViaWebhook param as {isViaWebhook}. Error: {err}"
28+
}

src/main/resources/info-messages.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@
2727
"MEG_SKIP_ROLLOUT_EVALUATE_EXPERIMENTS": "No rollout rule found for feature:{featureKey}. Hence, evaluating experiments",
2828
"MEG_CAMPAIGN_FOUND_IN_STORAGE": "Campaign {campaignKey} found in storage for user ID:{userId}",
2929
"MEG_CAMPAIGN_ELIGIBLE": "Campaign {campaignKey} is eligible for user ID:{userId}",
30-
"MEG_WINNER_CAMPAIGN": "MEG: Campaign {campaignKey} is the winner for group {groupId} for user ID:{userId} {algo}"
31-
}
30+
"MEG_WINNER_CAMPAIGN": "MEG: Campaign {campaignKey} is the winner for group {groupId} for user ID:{userId} {algo}",
31+
"SETTINGS_UPDATED": "Settings fetched and updated successfully on the current VWO client instance when API: {apiName} got called having isViaWebhook param as {isViaWebhook}"
32+
}

src/test/java/Main.java

Lines changed: 0 additions & 157 deletions
This file was deleted.

0 commit comments

Comments
 (0)