<commit_before>request = require 'request'

class Freshdesk
  constructor: (@url, @apikey, headers = {}) ->
    authKey = new Buffer("#{@apikey}:X").toString('base64')
    defaultHeaders = {
      'Authorization': "Basic #{authKey}"
      'Content-Type': "application/json"
    }

    # Add any other headers supplied by you to the requests
    for own header of headers
      defaultHeaders[header] = headers[header]

    # This request object will be used in the base methods
    @r = request.defaults({
      headers: defaultHeaders
    })

  get: (path, cb) =>
    @r.get({
      url: "#{@url}#{path}"
    }, cb)

  post: (path, data, cb) =>
    @r.post({
      url: "#{@url}#{path}"
      json: data
    }, cb)

  put: (path, data, cb) =>
    @r.put({
      url: "#{@url}#{path}"
      json: data
    }, cb)

  delete: (path, cb) =>
    @r.del({
      url: "#{@url}#{path}"
    }, cb)

  # Ticket Methods
  createTicket: (data, cb) =>
    @post '/helpdesk/tickets.json', data, cb

  listTickets: (cb) =>
    @get '/helpdesk/tickets.json', cb

  allTickets: (cb) =>
    @get '/helpdesk/tickets/filter/all_tickets?format=json', cb

  getTicket: (id, cb) =>
    @get "/helpdesk/tickets/#{id}.json", cb

  updateTicket: (id, data, cb) =>
    @put "/helpdesk/tickets/#{id}.json", data, cb

  pickTicket: (id, cb) =>
    @put "/helpdesk/tickets/#{id}/pick_tickets.json", {}, cb

  deleteTicket: (id, cb) =>
    @delete "/helpdesk/tickets/#{id}.json", cb

  restoreTicket: (id, cb) =>
    @put "/helpdesk/tickets/#{id}/restore.json", cb

  assignTicket: (id, user_id, cb) =>
    @put "/helpdesk/tickets/#{id}/assign.json?responder_id=#{user_id}", cb

  ticketFields: (cb) =>
    @get "/ticket_fields.json", cb

  addNoteToTicket: (id, note, is_private, callback) =>
    data =
      helpdesk_note:
        body: note,
        private: is_private
    @post "/helpdesk/tickets/#{id}/conversations/note.json", data, cb


  # User Methods
  createUser: (data, cb) =>
    @post '/contacts.json', contact, cb

  listUsers: (cb) =>
    @get '/contacts.json?state=all', cb<commit_msg>support HTML body in ticket, fix invalid identifier (closes #5)<commit_after>request = require 'request'

class Freshdesk
  constructor: (@url, @apikey, headers = {}) ->
    authKey = new Buffer("#{@apikey}:X").toString('base64')
    defaultHeaders = {
      'Authorization': "Basic #{authKey}"
      'Content-Type': "application/json"
    }

    # Add any other headers supplied by you to the requests
    for own header of headers
      defaultHeaders[header] = headers[header]

    # This request object will be used in the base methods
    @r = request.defaults({
      headers: defaultHeaders
    })

  get: (path, cb) =>
    @r.get({
      url: "#{@url}#{path}"
    }, cb)

  post: (path, data, cb) =>
    @r.post({
      url: "#{@url}#{path}"
      json: data
    }, cb)

  put: (path, data, cb) =>
    @r.put({
      url: "#{@url}#{path}"
      json: data
    }, cb)

  delete: (path, cb) =>
    @r.del({
      url: "#{@url}#{path}"
    }, cb)

  # Ticket Methods
  createTicket: (data, cb) =>
    @post '/helpdesk/tickets.json', data, cb

  listTickets: (cb) =>
    @get '/helpdesk/tickets.json', cb

  allTickets: (cb) =>
    @get '/helpdesk/tickets/filter/all_tickets?format=json', cb

  getTicket: (id, cb) =>
    @get "/helpdesk/tickets/#{id}.json", cb

  updateTicket: (id, data, cb) =>
    @put "/helpdesk/tickets/#{id}.json", data, cb

  pickTicket: (id, cb) =>
    @put "/helpdesk/tickets/#{id}/pick_tickets.json", {}, cb

  deleteTicket: (id, cb) =>
    @delete "/helpdesk/tickets/#{id}.json", cb

  restoreTicket: (id, cb) =>
    @put "/helpdesk/tickets/#{id}/restore.json", cb

  assignTicket: (id, user_id, cb) =>
    @put "/helpdesk/tickets/#{id}/assign.json?responder_id=#{user_id}", cb

  ticketFields: (cb) =>
    @get "/ticket_fields.json", cb

  addNoteToTicket: (id, note, is_private, cb) =>
    data =
      helpdesk_note:
        body_html: note,
        private: is_private
    @post "/helpdesk/tickets/#{id}/conversations/note.json", data, cb


  # User Methods
  createUser: (data, cb) =>
    @post '/contacts.json', contact, cb

  listUsers: (cb) =>
    @get '/contacts.json?state=all', cb
{'fraction_non_alphanumeric': 0.18441744102930666, 'fraction_numerical': 0.0011913271384322134, 'mean_word_length': 2.9014869888475836, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, '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': '6233775', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1521, 'n_words': 358}