Skip to content

Commit

Permalink
feat: OutcomeService method to prepare request
Browse files Browse the repository at this point in the history
OutcomeService gains methods to prepare requests without sending them. `ims-lti` would define and sign the request but and user can sent it by some other way.

Adds:

- OutcomeService.prototype.replace_result_request(score) -> {body, options}
- OutcomeService.prototype.replace_result_with_text_request(score) -> {body, options}
- OutcomeService.prototype.replace_result_with_url_request(score) -> {body, options}
- OutcomeService.prototype.read_result_request(score) -> {body, options}
- OutcomeService.prototype.delete_result_request(score) -> {body, options}
  • Loading branch information
dinoboff committed Aug 17, 2017
1 parent 4df2936 commit 379f9fa
Showing 1 changed file with 60 additions and 31 deletions.
91 changes: 60 additions & 31 deletions src/extensions/outcomes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -97,91 +97,120 @@ class OutcomeService
parts = @service_url_parts = url.parse @service_url, true
@service_url_oauth = parts.protocol + '//' + parts.host + parts.pathname


send_replace_result: (score, callback) ->
replace_result_request: (score) ->
doc = new OutcomeDocument @REQUEST_REPLACE, @source_did, @
doc.add_score score, @language
@_request doc

send_replace_result: (score, callback) ->
try
doc.add_score score, @language
@_send_request doc, callback
req = @replace_result_request(score)
@_send(req.body, req.options, callback)
catch err
callback err, false


send_replace_result_with_text: (score, text, callback) ->
replace_result_with_text_request: (score, text) ->
doc = new OutcomeDocument @REQUEST_REPLACE, @source_did, @
doc.add_score score, @language,
doc.add_text text
@_request doc

send_replace_result_with_text: (score, text, callback) ->
try
doc.add_score score, @language,
doc.add_text text
@_send_request doc, callback
req = @replace_result_with_text_request score, text
@_send(req.body, req.options, callback)
catch err
callback err, false


send_replace_result_with_url: (score, url, callback) ->
replace_result_with_url_request: (score, url) ->
doc = new OutcomeDocument @REQUEST_REPLACE, @source_did, @
doc.add_score score, @language,
doc.add_url url
@_request doc

send_replace_result_with_url: (score, url, callback) ->
try
doc.add_score score, @language,
doc.add_url url
@_send_request doc, callback
req = @replace_result_with_url_request(score, url)
@_send req.body, req.options, callback
catch err
callback err, false


send_read_result: (callback) ->
read_result_request: ->
doc = new OutcomeDocument @REQUEST_READ, @source_did, @
@_send_request doc, (err, result, xml) =>
return callback(err, result) if err
@_request(doc)

score = parseFloat navigateXml(xml, 'imsx_POXBody.readResultResponse.result.resultScore.textString'), 10
send_read_result: (callback) ->
try
req = @read_result_request()
@_send req.body, req.options, (err, result, xml) =>
return callback(err, result) if err

if (isNaN(score))
callback new errors.OutcomeResponseError('Invalid score response'), false
else
callback null, score
score = parseFloat navigateXml(xml, 'imsx_POXBody.readResultResponse.result.resultScore.textString'), 10

if (isNaN(score))
callback new errors.OutcomeResponseError('Invalid score response'), false
else
callback null, score
catch err
callback err, null

send_delete_result: (callback) ->

delete_result_request: ->
doc = new OutcomeDocument @REQUEST_DELETE, @source_did, @
@_send_request doc, callback
@_request doc

send_delete_result: (callback) ->
try
req = @delete_result_request()
@_send req.body, req.options, callback
catch err
callback err, false

supports_result_data: (type) ->
return @result_data_types.length and (!type or @result_data_types.indexOf(type) != -1)


_send_request: (doc, callback) ->
# deprecated Since version 3.1.
req = @_request doc
@_send req.body, req.options, callback

_request: (doc) ->
xml = doc.finalize()
body = ''
is_ssl = @service_url_parts.protocol == 'https:'

options =
protocol: @service_url_parts.protocol
hostname: @service_url_parts.hostname
path: @service_url_parts.path
method: 'POST'
headers: @_build_headers xml

return body: xml, options: options

_send: (body, options, callback) ->
result = ''
client = if options.protocol == 'https:' then https else http

if @cert_authority and is_ssl
options.ca = @cert_authority
else
options.agent = if is_ssl then https.globalAgent else http.globalAgent
options.agent = client.globalAgent

if @service_url_parts.port
options.port = @service_url_parts.port

# Make the request to the TC, verifying that the status code is valid and fetching the entire response body.
req = (if is_ssl then https else http).request options, (res) =>
req = client.request options, (res) =>
res.setEncoding 'utf8'
res.on 'data', (chunk) => body += chunk
res.on 'data', (chunk) => result += chunk
res.on 'end', () =>
@_process_response body, callback
@_process_response result, callback

req.on 'error', (err) =>
callback err, false

req.write xml
req.write body
req.end()


Expand Down

0 comments on commit 379f9fa

Please sign in to comment.