<commit_before>restify = require "restify"
util = require "util"
yaml = require 'js-yaml'
Login = require "./Login"
DolanDB = require "./DolanDB"
q = require "q"
fs = require "fs"
URL = require "url"
http = require 'http'
open = require "open"
paths = require "./paths"
env = require("yeoman-generator")()
Help = require "./Help"
chalk = require "chalk"

data_definition_path = 'config/dolandb.yaml'
raml_path            = 'www/local.raml'
cloud_json_path      = 'config/cloud.json'

db_browser_url       = 'http://sandboxdb.testgyver.com/browser/projects/'
configapi_url        = 'http://config-api.testgyver.com'

#configapi_url        = 'http://config-api.local.testgyver.com:3000'
#db_browser_url       = 'http://sandboxdb.local.testgyver.com:3000/browser/projects/'

class Providers
  constructor: (@options={}) ->
    @config_api = restify.createJsonClient
      url: configapi_url
    @config_api.headers["Authorization"] = Login.currentAccessToken()
    @db_browser = restify.createJsonClient
      url: db_browser_url

  listProviders: () =>
    console.log "Fetching all providers...\n"
    @config_api.get('/available_service_providers.json', (err, req, res, obj) =>
      if err?
        Help.error()
        console.log(
          """
          Could not get list of available providers. Please check your
          Internet connection.

          In case of a service outage, more information is available at

            #{chalk.underline('http://status.appgyver.com')}

          """
          )
      else
        console.log "Available providers:\n"
        obj.forEach (provider) ->
          console.log "  #{provider.human_name}"
        console.log ""
      @config_api.close()
    )

  addProvider: (provider_name) =>
    if provider_name? and provider_name != 'appgyver_sandbox'
      console.log "Only supported provider 'appgyver_sandbox'"
      process.exit 1

    @getProviderByName(provider_name).then(
      (provider) =>
        if provider?
          console.log "Provider '#{provider_name}' is already defined"
          process.exit 1

        data =
          providerTypeId: 6    # appgyver_sandbox id specified in config api
          name: provider_name

        console.log "Adding provider '#{provider_name}' to your app"

        @config_api.post("/app/#{@getAppId()}/service_providers.json", data, (err, req, res, obj) =>

          if obj['uid']
            console.log 'done'
          else
            console.log err

          @config_api.close()
        )
      (error) =>
        errorObject = JSON.parse(error)
        Help.error()
        console.log "\nCould not add provider: #{errorObject.error}"<commit_msg>more improvements to output<commit_after>restify = require "restify"
util = require "util"
yaml = require 'js-yaml'
Login = require "./Login"
DolanDB = require "./DolanDB"
q = require "q"
fs = require "fs"
URL = require "url"
http = require 'http'
open = require "open"
paths = require "./paths"
env = require("yeoman-generator")()
Help = require "./Help"
chalk = require "chalk"

data_definition_path = 'config/dolandb.yaml'
raml_path            = 'www/local.raml'
cloud_json_path      = 'config/cloud.json'

db_browser_url       = 'http://sandboxdb.testgyver.com/browser/projects/'
configapi_url        = 'http://config-api.testgyver.com'

#configapi_url        = 'http://config-api.local.testgyver.com:3000'
#db_browser_url       = 'http://sandboxdb.local.testgyver.com:3000/browser/projects/'

class Providers
  constructor: (@options={}) ->
    @config_api = restify.createJsonClient
      url: configapi_url
    @config_api.headers["Authorization"] = Login.currentAccessToken()
    @db_browser = restify.createJsonClient
      url: db_browser_url

  listProviders: () =>
    console.log "Fetching all providers...\n"
    @config_api.get('/available_service_providers.json', (err, req, res, obj) =>
      if err?
        Help.error()
        console.log(
          """
          Could not get list of available providers. Please check your
          Internet connection.

          In case of a service outage, more information is available at

            #{chalk.underline('http://status.appgyver.com')}

          """
          )
      else
        console.log "Available providers:\n"
        obj.forEach (provider) ->
          console.log "  #{provider.human_name}"
        console.log ""
      @config_api.close()
    )

  addProvider: (provider_name) =>
    if provider_name? and provider_name != 'appgyver_sandbox'
      Help.error()
      console.log "Only supported provider is 'appgyver_sandbox'"
      process.exit 1

    @getProviderByName(provider_name).then(
      (provider) =>
        if provider?
          Help.error()
          console.log "Provider '#{provider_name}' is already defined"
          process.exit 1

        data =
          providerTypeId: 6    # appgyver_sandbox id specified in config api
          name: provider_name

        console.log "Adding provider '#{provider_name}' to your app..."

        @config_api.post("/app/#{@getAppId()}/service_providers.json", data, (err, req, res, obj) =>

          if obj['uid']
            Help.success()
            console.log "Provider successfully added!"
          else
            Help.error()
            console.log err

          @config_api.close()
        )
      (error) =>
        errorObject = JSON.parse(error)
        Help.error()
        console.log "\nCould not add provider: #{errorObject.error}"
{'fraction_non_alphanumeric': 0.12449652142072501, 'fraction_numerical': 0.004027828634199927, 'mean_word_length': 2.4974391805377723, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1659151', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1620, 'n_words': 379}