<commit_before>NotificationPlugin = require '../../notification-plugin'
xmlBuilder = require './xmlBuilder'
xmlResponseParser = require './xmlparser'

class XmlRpc

  constructor: (@host) ->

  buildRequest: (xmlBody) ->
    NotificationPlugin.request
      .post(@host + '/xmlrpc.cgi')
      .type('text/xml')
      .send(xmlBody)
      .on 'error', (err) ->
        callback(err)

  methodCall: (methodName, params, callback, tokenCallback) ->
    xmlBody = xmlBuilder.buildRequestBody(methodName, params)
    request = @buildRequest(xmlBody)
    request.end (response) =>
      return callback(response.error) if response.error
      xmlResponseParser.parse response, (xmlError, xmljson) =>
        @handleErrors(xmlError, xmljson.methodResponse.fault)
        if tokenCallback
          tokenCallback(@extractToken(xmljson))
        else
          id = xmljson.methodResponse.params.param.value.struct.member.value.int
          callback null,
            id: id,
            url: "#{@host}/show_bug.cgi?id=#{id}"


  handleErrors: (xmlError, fault) ->
    callback(xmlError) if xmlError
    if fault
      errorMsg = fault.value.struct.member[0].value.string
      errorCode = fault.value.struct.member[1].value.int
      callback(new Error("#{errorMsg} (code: #{errorCode})"))

  extractToken: (xmljson) ->
    xmljson.methodResponse.params.param.value.struct.member[1].value.string

module.exports = XmlRpc;<commit_msg>bugzilla/xmlrpc: fix undefined callback<commit_after>NotificationPlugin = require '../../notification-plugin'
xmlBuilder = require './xmlBuilder'
xmlResponseParser = require './xmlparser'

class XmlRpc

  constructor: (@host) ->

  buildRequest: (xmlBody) ->
    NotificationPlugin.request
      .post(@host + '/xmlrpc.cgi')
      .type('text/xml')
      .send(xmlBody)
      .on 'error', (err) ->
        callback(err)

  methodCall: (methodName, params, callback, tokenCallback) ->
    xmlBody = xmlBuilder.buildRequestBody(methodName, params)
    request = @buildRequest(xmlBody)
    request.end (response) =>
      return callback(response.error) if response.error
      xmlResponseParser.parse response, (xmlError, xmljson) =>
        callback(xmlError) if xmlError
        if fault = xmljson.methodResponse.fault
          errorMsg = fault.value.struct.member[0].value.string
          errorCode = fault.value.struct.member[1].value.int
          callback(new Error("#{errorMsg} (code: #{errorCode})"))

        if tokenCallback
          tokenCallback(@extractToken(xmljson))
        else
          id = @extractId(xmljson)
          callback null,
            id: id,
            url: "#{@host}/show_bug.cgi?id=#{id}"

  extractToken: (xmljson) ->
    xmljson.methodResponse.params.param.value.struct.member[1].value.string

  extractId: (xmljson) ->
    xmljson.methodResponse.params.param.value.struct.member.value.int

module.exports = XmlRpc;
{'fraction_non_alphanumeric': 0.13229166666666667, 'fraction_numerical': 0.0020833333333333333, 'mean_word_length': 3.0749646393210748, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 47}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7196353', 'n_tokens_mistral': 854, 'n_tokens_neox': 829, 'n_words': 159}