From 8b2c76f37f23f850f284705d52e5cc1e91babfc8 Mon Sep 17 00:00:00 2001 From: klauste Date: Fri, 5 Jun 2015 14:54:29 +1000 Subject: [PATCH] [FEATURE] Add events api support --- LearnositySDK/Examples/Events.cs | 45 +++++++++++++++++++++++++++++++ LearnositySDK/Request/Init.cs | 29 +++++++++++++++++++- LearnositySDK/Utils/JsonObject.cs | 2 +- 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 LearnositySDK/Examples/Events.cs diff --git a/LearnositySDK/Examples/Events.cs b/LearnositySDK/Examples/Events.cs new file mode 100644 index 0000000..9ddef31 --- /dev/null +++ b/LearnositySDK/Examples/Events.cs @@ -0,0 +1,45 @@ +using System; +using System.Text; +using LearnositySDK.Request; +using LearnositySDK.Utils; + +namespace LearnositySDK.Examples +{ + public class Events + { + + public static string Simple() + { + string service = "events"; + + JsonObject security = new JsonObject(); + security.set("consumer_key", "yis0TYCu7U9V4o7M"); + security.set("user_id", "demo_student"); + security.set("domain", "localhost"); + + string secret = "74c5fd430cf1242a527f6223aebd42d30464be22"; + + JsonObject request = new JsonObject(); + request.set("eventbus", true); + request.set("skip", true); + request.set("users", Events.users()); + + Init init = new Init(service, security, secret, request); + return init.generate(); + } + + private static JsonObject users() + { + JsonObject users = new JsonObject(true); + + for (int i = 0; i <= 10; i++) + { + string user_id = "userid_" + i; + users.set(user_id); + } + + return users; + } + + } +} diff --git a/LearnositySDK/Request/Init.cs b/LearnositySDK/Request/Init.cs index d742aa4..c13077e 100755 --- a/LearnositySDK/Request/Init.cs +++ b/LearnositySDK/Request/Init.cs @@ -146,7 +146,7 @@ private void Initialize(string service, JsonObject securityPacket, string secret this.signRequestData = true; this.validSecurityKeys = new string[4] { "consumer_key", "domain", "timestamp", "user_id" }; - this.validServices = new string[6] { "assess", "author", "data", "items", "questions", "reports" }; + this.validServices = new string[7] { "assess", "author", "data", "events", "items", "questions", "reports" }; this.algorithm = "sha256"; // We don't catch this Exception, as we can't `die` as in PHP @@ -266,6 +266,12 @@ public string generate() output = this.generateQuestions(output); + break; + case "events": + + output.set("security", this.securityPacket); + output.set("config", this.requestPacket); + break; default: // do nothing @@ -431,6 +437,27 @@ private void setServiceOptions() this.securityPacket.set("user_id", this.requestPacket.getString("user_id")); } + break; + case "events": + + string consumer_key = this.securityPacket.getString("consumer_key"); + JsonObject hashedUsers; + + this.signRequestData = false; + + JsonObject requestPackageUsers = this.requestPacket.getJsonObject("users"); + if (requestPackageUsers != null) { + string[] users = requestPackageUsers.getValuesArray(); + if (users != null && users.Length > 0) { + hashedUsers = new JsonObject(); + for (int i = 0; i < users.Length; i++) { + string user_id = users[i]; + hashedUsers.set(user_id, Tools.hash(this.algorithm, user_id + consumer_key)); + } + this.requestPacket.set("users", hashedUsers); + } + } + break; default: // do nothing diff --git a/LearnositySDK/Utils/JsonObject.cs b/LearnositySDK/Utils/JsonObject.cs index 3998f8b..eb5d691 100755 --- a/LearnositySDK/Utils/JsonObject.cs +++ b/LearnositySDK/Utils/JsonObject.cs @@ -382,7 +382,7 @@ public List getValuesList(bool includeInts = false, bool includeBools = foreach (KeyValuePair pair in this.ds) { - stringsList.Add(pair.Key); + stringsList.Add(pair.Value); } if (includeInts)