<commit_before>const EasyXml = require('easyxml')
const ws = require('ws.js-buffer-fix')
const Parser = require('xml2js-parser')
const parser = new Parser({trim: true, explicitArray: false})
const serializer = new EasyXml({
  rootElement: 'soap:Envelope',
  indent: 0,
  unwrapArrays: true,
  filterNulls: true
})

module.exports = async options => {
  const { query, config, action } = options
  const dokumenter = options._dokumenter ? options._dokumenter : false
  const envelope = createEnvelope(query, action)
  const xml = jsToXml(envelope)
  try {
    const ctx = await sendRequest(xml, config.url, dokumenter)
    return xmlToJs(ctx)
  } catch (error) {
    if (error.response && error.statusCode) {
      throw checkError(error)
    }
  }
}

function textInsideTag (tag, html) {
  const stripTag = new RegExp(`<${tag}>|</${tag}>`, 'g')
  const insideTag = new RegExp(`<${tag}>(.*?)</${tag}>`, 'g')
  const textInside = html.match(insideTag)
  if (!textInside) return
  return textInside.join('').replace(stripTag, '')
}

function checkError (error) {
  if (error.response && error.statusCode) {
    const tag = error.response.includes('title') ? 'title' : error.response.includes('faultstring') ? 'faultstring' : false
    return {
      response: tag ? textInsideTag(tag, error.response) : error.response,
      status: error.statusCode
    }
  } else {
    return error
  }
}

function createEnvelope (query, action) {
  return {
    '_xmlns:soap': 'http://schemas.xmlsoap.org/soap/envelope/',
    'soap:Body': {
      [`ns2:${action}`]: {
        '_xmlns:ns2': 'http://www.ks.no/svarut/servicesV7',
        ...query
      }
    }
  }
}

function jsToXml (obj) {
  let xml = serializer.render(obj)
  xml = xml.replace(/\n/g, '')
  return xml
}

function sendRequest (xml, url, dokumenter) {
  function addFiles (wsRequest, dokumenter) {
    dokumenter.map((dokument, i) => {
      const xpath = `//data[${++i}]`
      ws.addAttachment(wsRequest, 'request', xpath, dokument.data, dokument.mimetype)<commit_msg>Simplifies<commit_after>const EasyXml = require('easyxml')
const ws = require('ws.js-buffer-fix')
const Parser = require('xml2js-parser')
const parser = new Parser({trim: true, explicitArray: false})
const serializer = new EasyXml({
  rootElement: 'soap:Envelope',
  indent: 0,
  unwrapArrays: true,
  filterNulls: true
})

module.exports = async options => {
  const { query, config, action } = options
  const dokumenter = options._dokumenter ? options._dokumenter : false
  const envelope = createEnvelope(query, action)
  const xml = jsToXml(envelope)
  try {
    const ctx = await sendRequest(xml, config.url, dokumenter)
    return xmlToJs(ctx)
  } catch (error) {
    throw checkError(error)
  }
}

function textInsideTag (tag, html) {
  const stripTag = new RegExp(`<${tag}>|</${tag}>`, 'g')
  const insideTag = new RegExp(`<${tag}>(.*?)</${tag}>`, 'g')
  const textInside = html.match(insideTag)
  return textInside ? textInside.join('').replace(stripTag, '') : ''
}

function checkError (error) {
  if (error.response && error.statusCode) {
    const tag = error.response.includes('title') ? 'title' : error.response.includes('faultstring') ? 'faultstring' : false
    return {
      message: tag ? textInsideTag(tag, error.response) : error.response,
      status: error.statusCode
    }
  } else {
    return error
  }
}

function createEnvelope (query, action) {
  return {
    '_xmlns:soap': 'http://schemas.xmlsoap.org/soap/envelope/',
    'soap:Body': {
      [`ns2:${action}`]: {
        '_xmlns:ns2': 'http://www.ks.no/svarut/servicesV7',
        ...query
      }
    }
  }
}

function jsToXml (obj) {
  let xml = serializer.render(obj)
  xml = xml.replace(/\n/g, '')
  return xml
}

function sendRequest (xml, url, dokumenter) {
  function addFiles (wsRequest, dokumenter) {
    dokumenter.map((dokument, i) => {
      const xpath = `//data[${++i}]`
      ws.addAttachment(wsRequest, 'request', xpath, dokument.data, dokument.mimetype)
{'fraction_non_alphanumeric': 0.15978862606945143, 'fraction_numerical': 0.0025163563160543532, 'mean_word_length': 3.7209026128266034, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 26}, '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': '48398', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1361, 'n_words': 296}