<commit_before>const rp = require('request-promise-native');
const { BadRequest } = require('@feathersjs/errors');
const { Configuration } = require('@schul-cloud/commons');

const logger = require('../../../logger/');

/**
 * Is created and designed as singleton.
 * Options only hold as global envirements, or in config file.
 */
class Nexboard {
	constructor() {
		this.apiKey = Configuration.get('NEXBOARD_API_KEY');
		this.user = Configuration.get('NEXBOARD_USER_ID');
		this.url = Configuration.get('NEXBOARD_URL') + Configuration.get('NEXBOARD_URI');

		logger.info('Nextboard is set to=', this.url);

		this.err = {
			getProject: 'Could not retrieve this Project.',
			projectIds: 'Could not retrieve ProjectIds.',
			createProject: 'Could not create new Project',
			retrieveBoardsFromProject: 'Could not retrieve Boards from Project.',
			retrieveBoards: 'Could not retrieve Board.',
			createBoard: 'Could not create a new Board.',
		};
	}

	createSettings({
		method = 'GET',
		endpoint,
		qs = {
			token: this.apiKey,
			userId: this.user,
		},
		body,
	}) {
		return {
			method,
			uri: `${this.url}${endpoint}`,
			qs,
			body,
			json: true,
		};
	}

	getProject(projectId) {
		return rp(this.createSettings({ endpoint: `projects/${projectId}` }))
			.catch((err) => {
				throw new BadRequest(this.err.getProject, err);
			});
	}

	getProjectsIds() {
		return rp(this.createSettings({ endpoint: 'projects' }))
			.then((res) => res.map((e) => e.id))
			.catch((err) => {
				throw new BadRequest(this.err.projectIds, err);
			});
	}

	createProject(title, description) {
		return rp(this.createSettings({
			method: 'POST',
			endpoint: 'projects',
			body: {
				title,
				description,
			},
		}))
			.catch((err) => {
				throw new BadRequest(this.err.createProject, err);
			});
	}

	getBoardsByProject(project) {
		return rp(this.createSettings({
			endpoint: `projects/${project}/boards`,
		}))
			.catch((err) => {
				throw new BadRequest(this.err.retrieveBoardsFromProject, err);
			});
	}

	getBoard(boardId) {
		return rp(this.createSettings({
			endpoint: `boards/${boardId}`,
		}))
			.catch((err) => {
				throw new BadRequest(this.err.retrieveBoards, err);
			});
	}

	createBoard(title, description, project, email = 'schulcloud') {
		return rp(this.createSettings({
			method: 'POST',
			endpoint: 'boards',
			qs: {
				token: this.apiKey,
			},
			body: {
				title,
				description,
				email,
				projectId: project,
			},
		}))
			.catch((err) => {
				throw new BadRequest(this.err.createBoard, err);<commit_msg>use configuration values at execution time<commit_after>const rp = require('request-promise-native');
const { BadRequest } = require('@feathersjs/errors');
const { Configuration } = require('@schul-cloud/commons');

const logger = require('../../../logger/');

/**
 * Is created and designed as singleton.
 * Options only hold as global envirements, or in config file.
 */
class Nexboard {
	constructor() {
		this.apiKey = () => Configuration.get('NEXBOARD_API_KEY');
		this.user = () => Configuration.get('NEXBOARD_USER_ID');
		this.url = () => Configuration.get('NEXBOARD_URL') + Configuration.get('NEXBOARD_URI');

		logger.info('Nextboard is set to=', this.url());

		this.err = {
			getProject: 'Could not retrieve this Project.',
			projectIds: 'Could not retrieve ProjectIds.',
			createProject: 'Could not create new Project',
			retrieveBoardsFromProject: 'Could not retrieve Boards from Project.',
			retrieveBoards: 'Could not retrieve Board.',
			createBoard: 'Could not create a new Board.',
		};
	}

	createSettings({
		method = 'GET',
		endpoint,
		qs = {
			token: this.apiKey(),
			userId: this.user(),
		},
		body,
	}) {
		return {
			method,
			uri: `${this.url()}${endpoint}`,
			qs,
			body,
			json: true,
		};
	}

	getProject(projectId) {
		return rp(this.createSettings({ endpoint: `projects/${projectId}` }))
			.catch((err) => {
				throw new BadRequest(this.err.getProject, err);
			});
	}

	getProjectsIds() {
		return rp(this.createSettings({ endpoint: 'projects' }))
			.then((res) => res.map((e) => e.id))
			.catch((err) => {
				throw new BadRequest(this.err.projectIds, err);
			});
	}

	createProject(title, description) {
		return rp(this.createSettings({
			method: 'POST',
			endpoint: 'projects',
			body: {
				title,
				description,
			},
		}))
			.catch((err) => {
				throw new BadRequest(this.err.createProject, err);
			});
	}

	getBoardsByProject(project) {
		return rp(this.createSettings({
			endpoint: `projects/${project}/boards`,
		}))
			.catch((err) => {
				throw new BadRequest(this.err.retrieveBoardsFromProject, err);
			});
	}

	getBoard(boardId) {
		return rp(this.createSettings({
			endpoint: `boards/${boardId}`,
		}))
			.catch((err) => {
				throw new BadRequest(this.err.retrieveBoards, err);
			});
	}

	createBoard(title, description, project, email = 'schulcloud') {
		return rp(this.createSettings({
			method: 'POST',
			endpoint: 'boards',
			qs: {
				token: this.apiKey(),
			},
			body: {
				title,
				description,
				email,
				projectId: project,
			},
		}))
			.catch((err) => {
				throw new BadRequest(this.err.createBoard, err);
{'fraction_non_alphanumeric': 0.1676020901877298, 'fraction_numerical': 0.0, 'mean_word_length': 3.5413005272407734, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '5923173', 'n_tokens_mistral': 2005, 'n_tokens_neox': 1724, 'n_words': 368}