<commit_before>/**
 * @description
 * HTTP code snippet generator for Java using OkHttp.
 *
 * @author
 * @shashiranjan84
 *
 * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
 */

'use strict'

var util = require('util')
var CodeBuilder = require('../../helpers/code-builder')

module.exports = function (source, options) {
  var opts = util._extend({
    indent: '  '
  }, options)

  var code = new CodeBuilder(opts.indent)

  var methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD']

  var methodsWithBody = [ 'POST', 'PUT', 'DELETE', 'PATCH']

  code.push('OkHttpClient client = new OkHttpClient();')
      .blank()

  if (source.postData.text) {
    if (source.postData.boundary) {
      code.push('MediaType mediaType = MediaType.parse("%s; boundary=%s");', source.postData.mimeType, source.postData.boundary)
    }else {
      code.push('MediaType mediaType = MediaType.parse("%s");', source.postData.mimeType)
    }
    code.push('RequestBody body = RequestBody.create(mediaType, %s);', JSON.stringify(source.postData.text))
  }

  console.log(source)

  code.push('Request request = new Request.Builder()')
  code.push(1, '.url("%s")', source.fullUrl)
  if (methods.indexOf(source.method.toUpperCase()) === -1) {
    if (source.postData.text) {
      code.push(1, '.method("%s", body)', source.method.toUpperCase())
    }else {
      code.push(1, '.method("%s", null)', source.method.toUpperCase())
    }
  }else {
    if (source.postData.text) {
      code.push(1, '.%s(body)', source.method.toLowerCase())
    }else {
      console.log(methodsWithBody.indexOf(source.method.toUpperCase()))
      if (methodsWithBody.indexOf(source.method.toUpperCase()) >= 0) {
        code.push(1, '.%s(null)', source.method.toLowerCase())
      }else {
        code.push(1, '.%s()', source.method.toLowerCase())
      }
    }
  }

  // Add headers, including the cookies
  var headers = Object.keys(source.allHeaders)

  // construct headers
  if (headers.length) {
    headers.map(function (key) {
      code.push(1, '.addHeader("%s", "%s")', key, source.allHeaders[key])
    })
  }

  code.push(1, '.build();')
      .blank()
      .push('Response response = client.newCall(request).execute();')

  return code.join()
}

module.exports.info = {
  key: 'okhttp',
  title: 'OkHttp',
  link: 'http://square.github.io/okhttp/',
  description: 'An HTTP Request Client Library'<commit_msg>refactored code<commit_after>/**
 * @description
 * HTTP code snippet generator for Java using OkHttp.
 *
 * @author
 * @shashiranjan84
 *
 * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
 */

'use strict'

var util = require('util')
var CodeBuilder = require('../../helpers/code-builder')

module.exports = function (source, options) {
  var opts = util._extend({
    indent: '  '
  }, options)

  var code = new CodeBuilder(opts.indent)

  var methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD']

  var methodsWithBody = [ 'POST', 'PUT', 'DELETE', 'PATCH']

  code.push('OkHttpClient client = new OkHttpClient();')
      .blank()

  if (source.postData.text) {
    if (source.postData.boundary) {
      code.push('MediaType mediaType = MediaType.parse("%s; boundary=%s");', source.postData.mimeType, source.postData.boundary)
    }else {
      code.push('MediaType mediaType = MediaType.parse("%s");', source.postData.mimeType)
    }
    code.push('RequestBody body = RequestBody.create(mediaType, %s);', JSON.stringify(source.postData.text))
  }

  code.push('Request request = new Request.Builder()')
  code.push(1, '.url("%s")', source.fullUrl)
  if (methods.indexOf(source.method.toUpperCase()) === -1) {
    if (source.postData.text) {
      code.push(1, '.method("%s", body)', source.method.toUpperCase())
    }else {
      code.push(1, '.method("%s", null)', source.method.toUpperCase())
    }
  }else if (methodsWithBody.indexOf(source.method.toUpperCase()) >= 0) {
    if (source.postData.text) {
      code.push(1, '.%s(body)', source.method.toLowerCase())
    }else {
      code.push(1, '.%s(null)', source.method.toLowerCase())
    }
  } else {
    code.push(1, '.%s()', source.method.toLowerCase())
  }

  // Add headers, including the cookies
  var headers = Object.keys(source.allHeaders)

  // construct headers
  if (headers.length) {
    headers.map(function (key) {
      code.push(1, '.addHeader("%s", "%s")', key, source.allHeaders[key])
    })
  }

  code.push(1, '.build();')
      .blank()
      .push('Response response = client.newCall(request).execute();')

  return code.join()
}

module.exports.info = {
  key: 'okhttp',
  title: 'OkHttp',
  link: 'http://square.github.io/okhttp/',
  description: 'An HTTP Request Client Library'
{'fraction_non_alphanumeric': 0.18115336397826995, 'fraction_numerical': 0.005014625992478061, 'mean_word_length': 3.5288552507095554, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1432361', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1555, 'n_words': 336}