Skip to content

Commit

Permalink
Merge pull request #2 from Learnosity/feature/add-events-api-support
Browse files Browse the repository at this point in the history
[FEATURE] Add events api support
  • Loading branch information
jack-vo committed Jun 5, 2015
2 parents c32bcd1 + 8b2c76f commit 0f1e065
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
45 changes: 45 additions & 0 deletions LearnositySDK/Examples/Events.cs
Original file line number Diff line number Diff line change
@@ -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;
}

}
}
29 changes: 28 additions & 1 deletion LearnositySDK/Request/Init.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion LearnositySDK/Utils/JsonObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public List<string> getValuesList(bool includeInts = false, bool includeBools =

foreach (KeyValuePair<string, string> pair in this.ds)
{
stringsList.Add(pair.Key);
stringsList.Add(pair.Value);
}

if (includeInts)
Expand Down

0 comments on commit 0f1e065

Please sign in to comment.