<commit_before>import { HTTP } from 'meteor/http';
import { Meteor } from 'meteor/meteor';

class TrelloApi {
  constructor(key, token) {
    this.endpoint = 'https://api.trello.com';
    this.version = '1';
    this.key = key;
    this.token = token;
  }

  makeRequest(method, url, params) {
    const fullUrl = `${this.endpoint}/${this.version}${url}`;
    const extendParam = params;
    extendParam.key = this.key;
    extendParam.token = this.token;
    try {
      return HTTP.call(method, fullUrl, { params });
    } catch (e) {
      return e;
    }
  }

  getBoards(params = {}) {
    const url = '/member/me/boards';
    const res = this.makeRequest('GET', url, params);
    if (res instanceof Error) {
      throw new Meteor.Error(res.response.statusCode, res.response.content);
    }
    return res.data;
  }

  getMember(memberId, params = {}) {
    const url = `/member/${memberId}`;
    const res = this.makeRequest('GET', url, params);
    if (res instanceof Error) {
      throw new Meteor.Error(res.response.statusCode, res.response.content);
    }
    return res.data;
  }

  getAdminBoards(userId, params = {}) {
    const boards = this.getBoards(params);
    const adminBoards = [];

    boards.forEach((board) => {
      if (!board.memberships) return;
      board.memberships.forEach((member) => {
        if (member.idMember === userId && member.memberType === 'admin') {
          adminBoards.push(board);
        }
      });
    });

    return adminBoards;
  }

  getAdminBoardsMembers(userId, boards) {
    const members = [];
    boards.forEach(board => {
      if (!board.memberships) return;
      board.memberships.forEach((member) => {
        if (member.idMember !== userId) {
          const memberProfile = this.getMember(member.idMember);
          members.push(memberProfile);
        }
      });
    });
    return members;
  }

  addBoardMember(boardId, memberId, params = {}) {
    const url = `/boards/${boardId}/members/${memberId}`;
    const res = this.makeRequest('PUT', url, params);
    if (res instanceof Error) {
      throw new Meteor.Error(res.response.statusCode, res.response.content);
    }
    return res.data;
  }

  removeBoardMember(boardId, memberId, params = {}) {
    const url = `/boards/${boardId}/members/${memberId}`;
    const res = this.makeRequest('DELETE', url, params);
    if (res instanceof Error) {
      throw new Meteor.Error(res.response.statusCode, res.response.content);
    }
    return res.data;
  }
}

export default TrelloApi;<commit_msg>update trello api error handler<commit_after>import { HTTP } from 'meteor/http';
import { Meteor } from 'meteor/meteor';
import { _ } from 'meteor/underscore';

class TrelloApi {
  constructor(key, token) {
    this.endpoint = 'https://api.trello.com';
    this.version = '1';
    this.key = key;
    this.token = token;
  }

  handleError(err) {
    let errStatus;
    let errMessage;

    if (err.response) {
      errStatus = err.response.statusCode;
      const message = err.response.content || '';
      const queryStart = message.indexOf('?');
      errMessage = message.substr(0, queryStart === - 1 ? message.length : queryStart);
    } else {
      errStatus = '404';
      errMessage = 'Unknown Error';
    }

    throw new Meteor.Error(errStatus, errMessage);
  }

  makeRequest(method, url, params) {
    const fullUrl = `${this.endpoint}/${this.version}${url}`;
    const extendParam = params;
    extendParam.key = this.key;
    extendParam.token = this.token;
    try {
      return HTTP.call(method, fullUrl, { params });
    } catch (err) {
      return err;
    }
  }

  getBoards(params = {}) {
    const url = '/member/me/boards';
    const res = this.makeRequest('GET', url, params);
    if (res instanceof Error) {
      this.handleError(res);
    }
    return res.data;
  }

  getMember(memberId, params = {}) {
    const url = `/member/${memberId}`;
    const res = this.makeRequest('GET', url, params);
    if (res instanceof Error) {
      this.handleError(res);
    }
    return res.data;
  }

  getAdminBoards(userId, params = {}) {
    const boards = this.getBoards(params);
    const adminBoards = [];

    boards.forEach((board) => {
      if (!board.memberships) return;
      board.memberships.forEach((member) => {
        if (member.idMember === userId && member.memberType === 'admin') {
          adminBoards.push(board);
        }
      });
    });

    return adminBoards;
  }

  getAdminBoardsMembers(userId, boards) {
    const members = [];
    const membersId = [];

    boards.forEach(board => {
      if (!board.memberships) return;
      board.memberships.forEach((member) => {
        if (member.idMember !== userId) {
          membersId.push(member.idMember);
        }
      });
    });

    _.uniq(membersId).forEach(memberId => {
      const memberProfile = this.getMember(memberId);
      members.push(memberProfile);
    });

    return members;
  }

  addBoardMember(boardId, memberId, params = {}) {
    const url = `/boards/${boardId}/members/${memberId}`;
    const res = this.makeRequest('PUT', url, params);
    if (res instanceof Error) {
      this.handleError(res);
    }
    return res.data;
  }

  removeBoardMember(boardId, memberId, params = {}) {
    const url = `/boards/${boardId}/members/${memberId}`;
    const res = this.makeRequest('DELETE', url, params);
    if (res instanceof Error) {
      this.handleError(res);
    }
    return res.data;
  }
}

export default TrelloApi;
{'fraction_non_alphanumeric': 0.15058479532163743, 'fraction_numerical': 0.0012792397660818713, 'mean_word_length': 2.7358361774744027, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 2, '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': '4854040', 'n_tokens_mistral': 1753, 'n_tokens_neox': 1732, 'n_words': 357}