Skip to content

Commit 416295b

Browse files
sakshamg1304rohitesh-wingify
authored andcommitted
feat: support for webhook
1 parent fbcbaac commit 416295b

File tree

5 files changed

+63
-75
lines changed

5 files changed

+63
-75
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ 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.4.0] - 2024-03-04
9+
10+
### Added
11+
- added new method `updateSettings` to update settings on the vwo client instance.
812

913
## [1.3.0] - 2024-11-11
1014

VWOFmeSdk/Constants/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static class Constants
3838

3939
public const string HOST_NAME = "dev.visualwebsiteoptimizer.com";
4040
public const string SETTINGS_ENDPOINT = "/server-side/v2-settings";
41+
public const string WEBHOOK_SETTINGS_ENDPOINT = "/server-side/v2-pull";
4142

4243
public const string VWO_FS_ENVIRONMENT = "vwo_fs_environment";
4344
public const string HTTPS_PROTOCOL = "https";

VWOFmeSdk/Packages/Logger/Messages/info-messages.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"ON_INIT_ALREADY_RESOLVED": "[INFO]: VWO-SDK {date} {apiName} already resolved",
33
"ON_INIT_SETTINGS_FAILED": "[INFO]: VWO-SDK {date} VWO settings could not be fetched",
4+
"SETTINGS_UPDATED_SUCCESSFULLY" : "Settings File Updated Successfully",
45

56
"POLLING_SET_SETTINGS": "There's a change in settings from the last settings fetched. Hence, instantiating a new VWO client internally",
67
"POLLING_NO_CHANGE_IN_SETTINGS": "No change in settings with the last settings fetched. Hence, not instantiating new VWO client",

VWOFmeSdk/Services/SettingsManager.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private string FetchSettingsAndCacheInStorage()
132132
* Fetches settings from the server
133133
* @return settings
134134
*/
135-
private string FetchSettings()
135+
public string FetchSettings(bool isViaWebhook = false)
136136
{
137137
if (sdkKey == null || accountId == null)
138138
{
@@ -148,9 +148,13 @@ private string FetchSettings()
148148
options.Add("s", "prod");
149149
}
150150

151+
string path = isViaWebhook
152+
? ConstantsNamespace.Constants.WEBHOOK_SETTINGS_ENDPOINT
153+
: ConstantsNamespace.Constants.SETTINGS_ENDPOINT;
154+
151155
try
152156
{
153-
RequestModel request = new RequestModel(hostname, "GET", ConstantsNamespace.Constants.SETTINGS_ENDPOINT, options, null, null, this.protocol, port);
157+
RequestModel request = new RequestModel(hostname, "GET", path, options, null, null, this.protocol, port);
154158
request.SetTimeout(networkTimeout);
155159

156160
ResponseModel response = networkInstance.Get(request);

VWOFmeSdk/VWOClient.cs

Lines changed: 51 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace VWOFmeSdk
3232
{
3333
public class VWOClient
3434
{
35+
private static VWOClient vwoClientInstance;
3536
private Settings processedSettings;
3637
public string Settings { get; private set; }
3738
private VWOInitOptions options;
@@ -40,8 +41,6 @@ public class VWOClient
4041
MissingMemberHandling = MissingMemberHandling.Ignore
4142
};
4243

43-
public static ObjectMapper ObjectMapper { get; } = new ObjectMapper();
44-
4544
/// <summary>
4645
/// Constructor to initialize the VWOClient
4746
/// </summary>
@@ -64,6 +63,8 @@ public VWOClient(string settings, VWOInitOptions options)
6463
UrlService.Init(this.processedSettings.CollectionPrefix);
6564
// Init SDKMetaUtil and set SDK version
6665
LoggerService.Log(LogLevelEnum.INFO, "CLIENT_INITIALIZED", null);
66+
67+
vwoClientInstance = this;
6768
}
6869
catch (Exception exception)
6970
{
@@ -82,13 +83,59 @@ public void UpdateSettings(string newSettings)
8283
{
8384
this.processedSettings = JsonConvert.DeserializeObject<Settings>(newSettings, JsonSerializerSettings);
8485
SettingsUtil.ProcessSettings(this.processedSettings);
86+
LoggerService.Log(LogLevelEnum.INFO, "SETTINGS_UPDATED_SUCCESSFULLY", null);
8587
}
8688
catch (Exception exception)
8789
{
8890
LoggerService.Log(LogLevelEnum.ERROR, "Exception occurred while updating settings: " + exception.Message);
8991
}
9092
}
9193

94+
/// <summary>
95+
/// This method is used to update the settings by fetching from server (overloaded method with no parameters).
96+
/// </summary>
97+
/// <returns>Updated settings in JSON string format</returns>
98+
public string UpdateSettings()
99+
{
100+
return UpdateSettings(true);
101+
}
102+
103+
/// <summary>
104+
/// This method is used to update the settings.
105+
/// </summary>
106+
/// <param name="isViaWebhook">Boolean value to indicate if the settings are being fetched via webhook</param>
107+
/// <returns>Updated settings in JSON string format</returns>
108+
public string UpdateSettings(bool isViaWebhook)
109+
{
110+
string apiName = "updateSettings";
111+
try
112+
{
113+
// Fetch the new settings from the server based on the webhook flag
114+
this.Settings = SettingsManager.GetInstance().FetchSettings(isViaWebhook);
115+
UpdateSettings(this.Settings); // Call the method that takes a settings string
116+
return this.Settings;
117+
}
118+
catch (Exception exception)
119+
{
120+
LoggerService.Log(LogLevelEnum.ERROR, "SETTINGS_FETCH_FAILED", new Dictionary<string, string>
121+
{
122+
{ "apiName", apiName },
123+
{ "isViaWebhook", isViaWebhook.ToString() },
124+
{ "err", exception.ToString() }
125+
});
126+
return null;
127+
}
128+
}
129+
130+
/// <summary>
131+
/// Static method to get the VWOClient instance
132+
/// </summary>
133+
/// <returns>VWOClient instance</returns>
134+
public static VWOClient GetInstance()
135+
{
136+
return vwoClientInstance;
137+
}
138+
92139
/// <summary>
93140
/// This method is used to get the flag value for the given feature key
94141
/// </summary>
@@ -205,8 +252,7 @@ public Dictionary<string, bool> TrackEvent(string eventName, VWOContext context,
205252
}
206253

207254
/// <summary>
208-
/// Overloaded function for no event properties
209-
/// calls track method to track the event
255+
/// Overloaded function when no event properties are required
210256
/// </summary>
211257
/// <param name="eventName">Event name to be tracked</param>
212258
/// <param name="context">User context</param>
@@ -215,73 +261,5 @@ public Dictionary<string, bool> TrackEvent(string eventName, VWOContext context)
215261
{
216262
return Track(eventName, context, new Dictionary<string, object>());
217263
}
218-
219-
/// <summary>
220-
/// Sets an attribute for a user in the context provided.
221-
/// This method validates the types of the inputs before proceeding with the API call.
222-
/// </summary>
223-
/// <param name="attributeKey">The key of the attribute to set</param>
224-
/// <param name="attributeValue">The value of the attribute to set</param>
225-
/// <param name="context">User context</param>
226-
public void SetAttribute(string attributeKey, string attributeValue, VWOContext context)
227-
{
228-
string apiName = "setAttribute";
229-
try
230-
{
231-
LoggerService.Log(LogLevelEnum.DEBUG, "API_CALLED", new Dictionary<string, string> { { "apiName", apiName } });
232-
if (!DataTypeUtil.IsString(attributeKey))
233-
{
234-
LoggerService.Log(LogLevelEnum.ERROR, "API_INVALID_PARAM", new Dictionary<string, string>
235-
{
236-
{ "apiName", apiName },
237-
{ "key", "attributeKey" },
238-
{ "type", DataTypeUtil.GetType(attributeKey) },
239-
{ "correctType", "String" }
240-
});
241-
throw new ArgumentException("TypeError: attributeKey should be a string");
242-
}
243-
244-
if (!DataTypeUtil.IsString(attributeValue))
245-
{
246-
LoggerService.Log(LogLevelEnum.ERROR, "API_INVALID_PARAM", new Dictionary<string, string>
247-
{
248-
{ "apiName", apiName },
249-
{ "key", "attributeValue" },
250-
{ "type", DataTypeUtil.GetType(attributeValue) },
251-
{ "correctType", "String" }
252-
});
253-
throw new ArgumentException("TypeError: attributeValue should be a string");
254-
}
255-
256-
if (context == null || string.IsNullOrEmpty(context.Id))
257-
{
258-
throw new ArgumentException("User ID is required");
259-
}
260-
261-
if (this.processedSettings == null || !new SettingsSchema().IsSettingsValid(this.processedSettings))
262-
{
263-
LoggerService.Log(LogLevelEnum.ERROR, "SETTINGS_SCHEMA_INVALID", null);
264-
return;
265-
}
266-
267-
SetAttributeAPI.SetAttribute(this.processedSettings, attributeKey, attributeValue, context);
268-
}
269-
catch (Exception exception)
270-
{
271-
LoggerService.Log(LogLevelEnum.ERROR, "API_THROW_ERROR", new Dictionary<string, string>
272-
{
273-
{ "apiName", apiName },
274-
{ "err", exception.ToString() }
275-
});
276-
}
277-
}
278-
}
279-
280-
public class ObjectMapper
281-
{
282-
public Dictionary<string, object> ConvertValue<T>(T value)
283-
{
284-
return JsonConvert.DeserializeObject<Dictionary<string, object>>(JsonConvert.SerializeObject(value));
285-
}
286264
}
287-
}
265+
}

0 commit comments

Comments
 (0)