{"size": 4530, "ext": "js", "max_stars_repo_path": "src/kong-api.js", "max_stars_repo_name": "frxception/kong-api-client", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/kong-api.js", "max_issues_repo_name": "frxception/kong-api-client", "max_issues_repo_issues_event_min_datetime": "2021-01-05T20:51:01.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-13T06:19:08.000Z", "max_forks_repo_path": "src/kong-api.js", "max_forks_repo_name": "frxception/kong-api-client", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 22.5373134328, "max_line_length": 123, "alphanum_fraction": 0.6664459161}
let BasicApi = require('../domain/basic.class')
let KongError = require('../domain/kong.error')
let Service = require('../libs/service.lib')


var assert = require('assert');
const axios = require('axios');

//Agregation of all the non public funcions
let _KongApi = {
	sync: false,
	service: null
}

class KongApi extends BasicApi {
	constructor(options) {
		//call the cntructor of the basic API
		super(options)

		//validate if the argument have the necessary parameters
		//TODO tranform the errors messagens to a file
		assert.notEqual(options, undefined, 'options not defined')
		assert.notEqual(options.admin_url, undefined, 'admin_url not defined: examples { admin_url:"http://kong_uri:kong_port"}')

		//Add / on kong url
		this.url = options.admin_url
		if (this.url[this.url.length - 1] != '/')
			this.url += '/'

		this.sync = options.sync

		//transforme services in a array
		this.services = []
		//TODO tranform to a function that always return a array (to_array())
		if (options.services) {
			this.services = Array.isArray(options.services)
				? options.services
				: [options.services]
		}

		//TODO use to_array() function
		this.plugins = options.plugins
		//TODO use to_array() function
		this.consumers = options.consumers
	}

	async init() {
		//save all services of options
		if (this.services) {
			let proms = this.services.map(async (ser) => {
				await addService(this.url, this.sync, ser)
			})
			await Promise.all(proms)
		}

		//save all puglins of options
		if (this.plugins) {
			let proms = this.plugins.map(async (e) => {
				try {
					await this.addPlugin(e)
				} catch (error) {
					if (this.sync && error instanceof KongError) {
						//do same thing
					} else
						throw error
				}
			})
			await Promise.all(proms)
		}

		//save all custumers of options
		if (this.consumers) {
			let proms = this.consumers.map(async (e) => {
				try {
					await this.addConsumer(e)
				} catch (error) {
					if (this.sync && error instanceof KongError) {
						//do same thing
					} else
						throw error
				}
			})
			await Promise.all(proms)
		}
	}


	async getServices(limit) {
		//llok for the services
		let services = []
		//convert to classes
		await services.forEach((e) => new Service(e))
	}

	async getService(id) {
		//look for the service
		let service = null
		//convert to class
		return new Service(service)
	}

	async addService(service) {
		//add service
		return await addService(this.url, false, service)
	}

	async updateService(id, service) {
		//update service

		let service = null
		//convert to class
		return new Service(service)
	}

	async deleteService(id) {
		//delete service

	}
}


module.exports = KongApi


_KongApi.addRoutes = async function (service, sync, routes) {
	let proms = routes.map(async (route) => {
		try {
			await service.addRoute(route)
		} catch (error) {
			console.log(error)
			if (!(sync && error instanceof KongError && error.code == 'R401'))
				throw error
		}
	})

	await Promise.all(proms)
}

_KongApi.addPlugins = async function (service, sync, plugins) {
	let proms = plugins.map(async (plugin) => {
		try {
			await service.addPlugin(plugin)
		} catch (error) {
			if (!(sync && error instanceof KongError && error.code == 'P401'))
				throw error
		}
	})
	await Promise.all(proms)
}

_KongApi.addConsumers = async function (service, sync, consumers) {
	let proms = consumers.map(async (consumer) => {
		try {
			await service.addConsumer(consumer)
		} catch (error) {
			if (!(sync && error instanceof KongError && error.code == 'C401'))
				throw error
		}
	})
	await Promise.all(proms)
}

_KongApi.addService = async function (url_entry, sync, input) {
	let url = url_entry + 'services'
	let service = null

	let routes = input.routes
	let consumers = input.consumers
	let plugins = input.plugins

	input.consumers = undefined
	input.routes = undefined
	input.plugins = undefined

	try {
		//cop
		service = await axios.post(url, input)
		service = new Service(service.data, url_entry)

		//adding routes
		await _KongApi.addRoutes(service, sync, routes ? routes : [])

	} catch (error) {
		if (!error.response)
			throw error
		if (!(sync && error.response.status == 409))
			throw error

		//buid the existing service here
		service = await axios.get(url + '/' + input.name)
		service = new Service(service.data, url_entry)
	}

	//adding plugins on service 
	await _KongApi.addPlugins(service, sync, plugins ? plugins : [])


	//adding consumers
	await _KongApi.addConsumers(service, sync, consumers ? consumers : [])
}
