Skip to content

Commit

Permalink
Merge pull request #84 from irisEd/callback_errors
Browse files Browse the repository at this point in the history
Added support error-first callbacks through strictCallbacks option
  • Loading branch information
ljwolford authored Mar 29, 2017
2 parents 8477dcf + db51416 commit 9b1b21c
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 35 deletions.
95 changes: 94 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ var Config = function()
// conf["registration"] = ruuid();
// conf["grouping"] = {"id":"ctxact:default/grouping"};
// conf["activity_platform"] = "default platform";

// Behavior defaults
// conf["strictCallbacks"] = false; // Strict error-first callbacks
return conf
}();
```
Expand Down Expand Up @@ -407,6 +410,25 @@ ADL.XAPIWrapper.sendStatement(stmt, function(resp, obj){
>> [4edfe763-8b84-41f1-a355-78b7601a6fe8]: 200 - OK
```

###### Send Statement with strict error-first callback

Requires the config option `strictCallbacks` to be `true`.

```JavaScript
var stmt = {"actor" : {"mbox" : "mailto:[email protected]"},
"verb" : {"id" : "http://adlnet.gov/expapi/verbs/answered",
"display" : {"en-US" : "answered"}},
"object" : {"id" : "http://adlnet.gov/expapi/activities/question"}};
ADL.XAPIWrapper.sendStatement(stmt, function(err, res, body) {
if (err) {
// Handle error case
return;
}

ADL.XAPIWrapper.log("[" + body.id + "]: " + res.status + " - " + res.statusText);});
>> [4edfe763-8b84-41f1-a355-78b7601a6fe8]: 200 - OK
```

###### Send Statement with Attachments
The wrapper can construct a `multipart/mixed` POST for a single statement that includes attachments. Attachments should be
supplied as an array in the 3rd parameter to `sendStatement`. Attachments are optional. The attachments array should consist of
Expand Down Expand Up @@ -538,6 +560,29 @@ ADL.XAPIWrapper.sendStatements(stmts, function(r){ADL.XAPIWrapper.log(JSON.parse
>> ["2d819ea4-1a1e-11e3-a888-08002787eb49", "409c27de-1a1e-11e3-a888-08002787eb49"]
```

###### Send Statements with strict error-first callback

Requires the config option `strictCallbacks` to be `true`.

```JavaScript
var stmt = {"actor" : {"mbox" : "mailto:[email protected]"},
"verb" : ADL.verbs.answered,
"object" : {"id" : "http://adlnet.gov/expapi/activities/question/1"}};
var stmt2 = {"actor" : {"mbox" : "mailto:[email protected]"},
"verb" : ADL.verbs.answered,
"object" : {"id" : "http://adlnet.gov/expapi/activities/question/2"}};
var stmts = [stmt, stmt2];
ADL.XAPIWrapper.sendStatements(stmts, function(err, res, body) {
if (err) {
// Handle error case
return;
}

ADL.XAPIWrapper.log(body);
});
>> ["2d819ea4-1a1e-11e3-a888-08002787eb49", "409c27de-1a1e-11e3-a888-08002787eb49"]
```

##### Get Statements
`function getStatements(searchParams, more, callback)`
Get a single or collection of Statements based on
Expand All @@ -557,7 +602,6 @@ if (ret)
```

###### Get all Statements with callback

```JavaScript
ADL.XAPIWrapper.getStatements(null, null,
function(r){ADL.XAPIWrapper.log(JSON.parse(r.response).statements);});
Expand Down Expand Up @@ -594,6 +638,22 @@ ADL.XAPIWrapper.getStatements(null, null,
...
```

###### Get all Statements with with strict error-first callback

Requires the config option `strictCallbacks` to be `true`.

```JavaScript
ADL.XAPIWrapper.getStatements(null, null, function(err, res, body) {
if (err) {
// Handle error case
return;
}

ADL.XAPIWrapper.log(body.statements);
});
>> <Array of statements>
```

###### Get Statements based on search parameters
The Experience API provides search parameters to narrow down
the result of a Statement request. See the [Experience API Spec](https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#723-getstatements)
Expand Down Expand Up @@ -638,6 +698,23 @@ ADL.XAPIWrapper.getActivities("http://adlnet.gov/expapi/activities/question",
>> <Activity object>
```

###### Get Activity with strict error-first callback

Requires the config option `strictCallbacks` to be `true`.

```JavaScript
ADL.XAPIWrapper.getActivities("http://adlnet.gov/expapi/activities/question", function(err, res, body) {
if (err) {
// Handle error case
return;
}

ADL.XAPIWrapper.log(body);
});
>> <Activity object>
```


##### Activity State
`function sendState(activityid, agent, stateid, registration, statevalue, matchHash, noneMatchHash, callback)`
`function getState(activityid, agent, stateid, registration, since, callback)`
Expand Down Expand Up @@ -841,6 +918,22 @@ ADL.XAPIWrapper.getAgents({"mbox":"mailto:[email protected]"},
>> <Person object>
```

###### Get Agent with strict error-first callbacks

Requires the config option `strictCallbacks` to be `true`.

```JavaScript
ADL.XAPIWrapper.getAgents({"mbox":"mailto:[email protected]"}, function(err, res, body) {
if (err) {
// Handle error case
return;
}

ADL.XAPIWrapper.log(body);
});
>> <Person object>
```

##### Agent Profile
`function sendAgentProfile(agent, profileid, profilevalue, matchHash, noneMatchHash, callback)`
`function getAgentProfile(agent, profileid, since, callback)`
Expand Down
6 changes: 3 additions & 3 deletions dist/xapiwrapper.min.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/xapi-launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function setupCourseLinks(_nodes)
};
}

function xAPILaunch(cb, terminate_on_unload)
function xAPILaunch(cb, terminate_on_unload, strict_callbacks)
{
cb = cb_wrap(cb);
try
Expand Down Expand Up @@ -145,10 +145,13 @@ function xAPILaunch(cb, terminate_on_unload)
}
var body = JSON.parse(xhr.responseText);
var launchData = body;

var conf = {};
conf['endpoint'] = launchData.endpoint;
conf["actor"] = launchData.actor;
conf.withCredentials = true;
conf.strictCallbacks = strict_callbacks || false;

window.onunload = function()
{
if (!terminate_on_unload)
Expand Down
Loading

0 comments on commit 9b1b21c

Please sign in to comment.