<commit_before>cookie = require '../utils/cookies'
EventBus = require '../utils/eventbus'
parseOpts = require './parse-opts'
channel = require './channel'

class Api
  constructor : (config, channel, currentUser)->
    @config = config
    @remoteConfig = channel.remoteConfig
    @services = @remoteConfig.settings
    @channel = channel
    @currentUser = currentUser
    EventBus.on 'hull.settings.update', (settings)-> @services = settings

    authScope = if channel.remoteConfig.data.headers?['Hull-Auth-Scope'] then channel.remoteConfig.data.headers['Hull-Auth-Scope'].split(":")[0] else ''


    @_message = (params, userSuccessCallback, userErrorCallback)=>

  ###
  # Sends the message described by @params to xdm
  # @param {Object} contains the provider, uri and parameters for the message
  # @param {Function} optional a success callback
  # @param {Function} optional an error callback
  # @return {Promise}
  ###
  message: ()=>
    return console.error("Hull Api is not initialized yet. You should run your app from the callback of Hull.ready()") unless @channel.rpc
    {opts, callback, errback} = parseOpts(Array.prototype.slice.call(arguments))
    new Promise (resolve, reject)=>

      onSuccess = (res={})=>
        @updateCurrentUserCookies(res.headers, res.provider)
        callback(res.body)
        resolve(res.body)

      onError = (response, error)=>
        errback(response.message, error)
        reject(response.message, error)

      @channel.rpc.message(opts, onSuccess, onError)

  clearToken    : (args...)=>
    @channel.rpc.clearUserToken(args...) # No need to be exposed, IMO

  refreshUser   : ()=>
    new Promise (resolve, reject)=><commit_msg>catch api error responses.<commit_after>cookie = require '../utils/cookies'
EventBus = require '../utils/eventbus'
parseOpts = require './parse-opts'
channel = require './channel'

class Api
  constructor : (config, channel, currentUser)->
    @config = config
    @remoteConfig = channel.remoteConfig
    @services = @remoteConfig.settings
    @channel = channel
    @currentUser = currentUser
    EventBus.on 'hull.settings.update', (settings)-> @services = settings

    authScope = if channel.remoteConfig.data.headers?['Hull-Auth-Scope'] then channel.remoteConfig.data.headers['Hull-Auth-Scope'].split(":")[0] else ''


    @_message = (params, userSuccessCallback, userErrorCallback)=>

  ###
  # Sends the message described by @params to xdm
  # @param {Object} contains the provider, uri and parameters for the message
  # @param {Function} optional a success callback
  # @param {Function} optional an error callback
  # @return {Promise}
  ###
  message: ()=>
    return console.error("Hull Api is not initialized yet. You should run your app from the callback of Hull.ready()") unless @channel.rpc
    {opts, callback, errback} = parseOpts(Array.prototype.slice.call(arguments))
    p = new Promise (resolve, reject)=>

      onSuccess = (res={})=>
        @updateCurrentUserCookies(res.headers, res.provider)
        callback(res.body)
        resolve(res.body)

      onError = (response, error)=>
        errback(response.message, error)
        reject(response.message, error)

      @channel.rpc.message(opts, onSuccess, onError)

    p.catch (err)->
      console.warn err, err.stack

  clearToken    : (args...)=>
    @channel.rpc.clearUserToken(args...) # No need to be exposed, IMO

  refreshUser   : ()=>
    new Promise (resolve, reject)=>
{'fraction_non_alphanumeric': 0.13333333333333333, 'fraction_numerical': 0.0005797101449275362, 'mean_word_length': 3.5830013280212483, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 24, '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': '621347', 'n_tokens_mistral': 996, 'n_tokens_neox': 986, 'n_words': 276}