Skip to content

Commit fa98fa4

Browse files
Merge pull request #24 from optimizely/yasir/get-userid-attributes
feat: Adds getUserId and getAttributes api in userContext.
2 parents ed6d738 + 6112ea0 commit fa98fa4

File tree

10 files changed

+171
-1
lines changed

10 files changed

+171
-1
lines changed

android/src/main/java/com/optimizely/optimizely_flutter_sdk/OptimizelyFlutterClient.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,24 @@ protected void trackEvent(ArgumentsParser argumentsParser, @NonNull Result resul
371371
}
372372
}
373373

374+
protected void getUserId(ArgumentsParser argumentsParser, @NonNull Result result) {
375+
OptimizelyUserContext userContext = getUserContext(argumentsParser);
376+
if (userContext == null) {
377+
result.success(createResponse(false, ErrorMessage.USER_CONTEXT_NOT_FOUND));
378+
return;
379+
}
380+
result.success(createResponse(true, Collections.singletonMap(RequestParameterKey.USER_ID, userContext.getUserId()), ""));
381+
}
382+
383+
protected void getAttributes(ArgumentsParser argumentsParser, @NonNull Result result) {
384+
OptimizelyUserContext userContext = getUserContext(argumentsParser);
385+
if (userContext == null) {
386+
result.success(createResponse(false, ErrorMessage.USER_CONTEXT_NOT_FOUND));
387+
return;
388+
}
389+
result.success(createResponse(true, Collections.singletonMap(RequestParameterKey.ATTRIBUTES, userContext.getAttributes()), ""));
390+
}
391+
374392
protected void setAttribute(ArgumentsParser argumentsParser, @NonNull Result result) {
375393
String sdkKey = argumentsParser.getSdkKey();
376394
if (sdkKey == null) {

android/src/main/java/com/optimizely/optimizely_flutter_sdk/OptimizelyFlutterSdkPlugin.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
6262
createUserContext(argumentsParser, result);
6363
break;
6464
}
65+
case APIs.GET_USER_ID: {
66+
getUserId(argumentsParser, result);
67+
break;
68+
}
69+
case APIs.GET_ATTRIBUTES: {
70+
getAttributes(argumentsParser, result);
71+
break;
72+
}
6573
case APIs.SET_ATTRIBUTES: {
6674
setAttribute(argumentsParser, result);
6775
break;

android/src/main/java/com/optimizely/optimizely_flutter_sdk/helper_classes/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public static class APIs {
2121
public static final String INITIALIZE = "initialize";
2222
public static final String GET_OPTIMIZELY_CONFIG = "getOptimizelyConfig";
2323
public static final String CREATE_USER_CONTEXT = "createUserContext";
24+
public static final String GET_USER_ID = "getUserId";
25+
public static final String GET_ATTRIBUTES = "getAttributes";
2426
public static final String SET_ATTRIBUTES="setAttributes";
2527
public static final String GET_FORCED_DECISION = "getForcedDecision";
2628
public static final String REMOVE_FORCED_DECISION = "removeForcedDecision";

ios/Classes/HelperClasses/Constants.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ struct API {
2020
static let initialize = "initialize"
2121
static let getOptimizelyConfig = "getOptimizelyConfig"
2222
static let createUserContext = "createUserContext"
23+
static let getUserId = "getUserId"
24+
static let getAttributes = "getAttributes"
2325
static let setAttributes = "setAttributes"
2426
static let trackEvent = "trackEvent"
2527
static let decide = "decide"

ios/Classes/SwiftOptimizelyFlutterSdkPlugin.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public class SwiftOptimizelyFlutterSdkPlugin: NSObject, FlutterPlugin {
5252
case API.removeNotificationListener: removeNotificationListener(call, result: result)
5353
case API.getOptimizelyConfig: getOptimizelyConfig(call, result: result)
5454
case API.createUserContext: createUserContext(call, result: result)
55+
case API.getUserId: getUserId(call, result: result)
56+
case API.getAttributes: getAttributes(call, result: result)
5557
case API.setAttributes: setAttributes(call, result: result)
5658
case API.trackEvent: trackEvent(call, result: result)
5759
case API.decide: decide(call, result: result)
@@ -207,7 +209,25 @@ public class SwiftOptimizelyFlutterSdkPlugin: NSObject, FlutterPlugin {
207209
} else {
208210
userContextsTracker[sdkKey] = [userContextId: userContext]
209211
}
210-
result(self.createResponse(success: true,result: [RequestParameterKey.userContextId: userContextId], reason: SuccessMessage.userContextCreated))
212+
result(self.createResponse(success: true, result: [RequestParameterKey.userContextId: userContextId], reason: SuccessMessage.userContextCreated))
213+
}
214+
215+
/// Returns userId for the user context.
216+
func getUserId(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
217+
guard let usrContext = getUserContext(arguments: call.arguments) else {
218+
result(self.createResponse(success: false, reason: ErrorMessage.userContextNotFound))
219+
return
220+
}
221+
result(createResponse(success: true, result: [RequestParameterKey.userId: usrContext.userId]))
222+
}
223+
224+
/// Returns attributes for the user context.
225+
func getAttributes(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
226+
guard let usrContext = getUserContext(arguments: call.arguments) else {
227+
result(self.createResponse(success: false, reason: ErrorMessage.userContextNotFound))
228+
return
229+
}
230+
result(createResponse(success: true, result: [RequestParameterKey.attributes: usrContext.attributes]))
211231
}
212232

213233
/// Sets attributes for the user context.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// **************************************************************************
2+
/// Copyright 2022, Optimizely, Inc. and contributors *
3+
/// *
4+
/// Licensed under the Apache License, Version 2.0 (the "License"); *
5+
/// you may not use this file except in compliance with the License. *
6+
/// You may obtain a copy of the License at *
7+
/// *
8+
/// http://www.apache.org/licenses/LICENSE-2.0 *
9+
/// *
10+
/// Unless required by applicable law or agreed to in writing, software *
11+
/// distributed under the License is distributed on an "AS IS" BASIS, *
12+
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
/// See the License for the specific language governing permissions and *
14+
/// limitations under the License. *
15+
///**************************************************************************/
16+
17+
import 'package:optimizely_flutter_sdk/src/data_objects/base_response.dart';
18+
import 'package:optimizely_flutter_sdk/src/utils/constants.dart';
19+
20+
class GetAttributesResponse extends BaseResponse {
21+
Map<String, dynamic> attributes = {};
22+
23+
GetAttributesResponse(Map<String, dynamic> json) : super(json) {
24+
if (json[Constants.responseResult] is Map<dynamic, dynamic>) {
25+
var response = Map<String, dynamic>.from(json[Constants.responseResult]);
26+
if (response[Constants.attributes] is Map<dynamic, dynamic>) {
27+
attributes = Map<String, dynamic>.from(response[Constants.attributes]);
28+
}
29+
}
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// **************************************************************************
2+
/// Copyright 2022, Optimizely, Inc. and contributors *
3+
/// *
4+
/// Licensed under the Apache License, Version 2.0 (the "License"); *
5+
/// you may not use this file except in compliance with the License. *
6+
/// You may obtain a copy of the License at *
7+
/// *
8+
/// http://www.apache.org/licenses/LICENSE-2.0 *
9+
/// *
10+
/// Unless required by applicable law or agreed to in writing, software *
11+
/// distributed under the License is distributed on an "AS IS" BASIS, *
12+
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
/// See the License for the specific language governing permissions and *
14+
/// limitations under the License. *
15+
///**************************************************************************/
16+
17+
import 'package:optimizely_flutter_sdk/src/data_objects/base_response.dart';
18+
import 'package:optimizely_flutter_sdk/src/utils/constants.dart';
19+
20+
class GetUserIdResponse extends BaseResponse {
21+
String userId = "";
22+
23+
GetUserIdResponse(Map<String, dynamic> json) : super(json) {
24+
if (json[Constants.responseResult] is Map<dynamic, dynamic>) {
25+
var response = Map<String, dynamic>.from(json[Constants.responseResult]);
26+
if (response[Constants.userID] is String) {
27+
userId = response[Constants.userID];
28+
}
29+
}
30+
}
31+
}

lib/src/user_context/optimizely_user_context.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import 'package:flutter/services.dart';
1818
import 'package:optimizely_flutter_sdk/optimizely_flutter_sdk.dart';
1919
import 'package:optimizely_flutter_sdk/src/data_objects/base_response.dart';
2020
import 'package:optimizely_flutter_sdk/src/data_objects/decide_response.dart';
21+
import 'package:optimizely_flutter_sdk/src/data_objects/get_attributes_response.dart';
2122
import 'package:optimizely_flutter_sdk/src/data_objects/get_forced_decision_response.dart';
23+
import 'package:optimizely_flutter_sdk/src/data_objects/get_user_id_response.dart';
2224
import 'package:optimizely_flutter_sdk/src/utils/constants.dart';
2325
import 'package:optimizely_flutter_sdk/src/utils/utils.dart';
2426

@@ -50,6 +52,26 @@ class OptimizelyUserContext {
5052

5153
OptimizelyUserContext(this._sdkKey, this._userContextId, this._channel);
5254

55+
/// Returns userId for the user context.
56+
Future<GetUserIdResponse> getUserId() async {
57+
final result = Map<String, dynamic>.from(
58+
await _channel.invokeMethod(Constants.getUserIdMethod, {
59+
Constants.sdkKey: _sdkKey,
60+
Constants.userContextId: _userContextId,
61+
}));
62+
return GetUserIdResponse(result);
63+
}
64+
65+
/// Returns attributes for the user context.
66+
Future<GetAttributesResponse> getAttributes() async {
67+
final result = Map<String, dynamic>.from(
68+
await _channel.invokeMethod(Constants.getAttributesMethod, {
69+
Constants.sdkKey: _sdkKey,
70+
Constants.userContextId: _userContextId,
71+
}));
72+
return GetAttributesResponse(result);
73+
}
74+
5375
/// Sets attributes for the user context.
5476
Future<BaseResponse> setAttributes(Map<String, dynamic> attributes) async {
5577
final result = Map<String, dynamic>.from(

lib/src/utils/constants.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class Constants {
2626
static const String close = "close";
2727
static const String getOptimizelyConfigMethod = "getOptimizelyConfig";
2828
static const String createUserContextMethod = "createUserContext";
29+
static const String getUserIdMethod = "getUserId";
2930
static const String setAttributesMethod = "setAttributes";
31+
static const String getAttributesMethod = "getAttributes";
3032
static const String trackEventMethod = "trackEvent";
3133
static const String decideMethod = "decide";
3234
static const String setForcedDecision = "setForcedDecision";

test/optimizely_flutter_sdk_test.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ void main() {
9494
Constants.responseReason: Constants.userContextCreated,
9595
Constants.responseResult: {Constants.userContextId: "123"},
9696
};
97+
case Constants.getUserIdMethod:
98+
return {
99+
Constants.responseSuccess: true,
100+
Constants.responseResult: {
101+
Constants.userID: userId,
102+
},
103+
};
104+
case Constants.getAttributesMethod:
105+
return {
106+
Constants.responseSuccess: true,
107+
Constants.responseResult: {
108+
Constants.attributes: {"abc": 123}
109+
},
110+
};
97111
case Constants.setAttributesMethod:
98112
return {
99113
Constants.responseSuccess: true,
@@ -336,6 +350,26 @@ void main() {
336350
expect(userContext, isNotNull);
337351
});
338352
});
353+
group("getUserId()", () {
354+
test("should succeed", () async {
355+
var sdk = OptimizelyFlutterSdk(testSDKKey);
356+
var userContext = await sdk.createUserContext(userId);
357+
var response = await userContext!.getUserId();
358+
359+
expect(response.success, equals(true));
360+
expect(response.userId, equals(userId));
361+
});
362+
});
363+
group("getAttributes()", () {
364+
test("should succeed", () async {
365+
var sdk = OptimizelyFlutterSdk(testSDKKey);
366+
var userContext = await sdk.createUserContext(userId);
367+
var response = await userContext!.getAttributes();
368+
369+
expect(response.success, equals(true));
370+
expect(response.attributes, equals({"abc": 123}));
371+
});
372+
});
339373
group("setAttributes()", () {
340374
test("should succeed", () async {
341375
var sdk = OptimizelyFlutterSdk(testSDKKey);

0 commit comments

Comments
 (0)