<commit_before>'use strict';

var _ = require('lodash');

var validate = require('./validate');
var grameneSwaggerClient = require('./grameneSwaggerClient');

// hard code these here so that we can guarantee module.export functions are
// available immediately.
var collections = {
  genes: "MongoGenesResponse",
  genetrees: "MongoGenetreesResponse",
  maps: "MongoMapsResponse",
  domains: "InterProResponse",
  taxonomy: "TaxonomyResponse",
  GO: "OntologyResponse",
  PO: "OntologyResponse",
  pathways: "ReactomeEntityResponse"
};

module.exports = _.mapValues(collections, callPromiseFactory);

function callPromiseFactory(schemaName, methodName) {
  function getCallFunction(gramene) {
		var operationId = gramene.spec.paths['/'+methodName]['get']['operationId']
    return gramene['apis']['Data access'][operationId];
  }

  function getSchemaName(gramene) {
    var $ref = _.get(gramene, ['spec','definitions', schemaName,'items','$$ref']);
		var a = $ref.split('#/definitions/');
    if ($ref && a[1]) {
      return a[1];
    }
  }

  function checkSchemaAgainst(gramene) {
    var scheamNameFromAPI = getSchemaName(gramene);
    if(scheamNameFromAPI !== schemaName) {
      throw new Error("Schema name mismatch. Expected " + schemaName + ", got " + scheamNameFromAPI);
    }
  }

  function makeCall(gramene, query) {
    var ids, params, apiMethodToInvoke;

    // checkSchemaAgainst(gramene);

    ids = getIdListString(query);
    params = {
      idList: ids
    };
    apiMethodToInvoke = getCallFunction(gramene);

    return apiMethodToInvoke(params).then(function addApiToResponseAndResolvePromise(res) {
      res.client = gramene;
      return res;
    });
  }

  return function promise(queryString) {
    return grameneSwaggerClient
      .then(function makeCallFactory(client) {
        return makeCall(client, queryString);
      })
      .then(validate(schemaName))<commit_msg>revert to swagger v2 calls<commit_after>'use strict';

var _ = require('lodash');

var validate = require('./validate');
var grameneSwaggerClient = require('./grameneSwaggerClient');

// hard code these here so that we can guarantee module.export functions are
// available immediately.
var collections = {
  genes: "MongoGenesResponse",
  genetrees: "MongoGenetreesResponse",
  maps: "MongoMapsResponse",
  domains: "InterProResponse",
  taxonomy: "TaxonomyResponse",
  GO: "OntologyResponse",
  PO: "OntologyResponse",
  pathways: "ReactomeEntityResponse"
};

module.exports = _.mapValues(collections, callPromiseFactory);

function callPromiseFactory(schemaName, methodName) {
  function getCallFunction(gramene) {
    return gramene['Data access'][methodName];
  }

  function getSchemaName(gramene) {
    const $ref = _.get(gramene, ['Data access', 'apis', methodName, 'type', 'schema', '$ref']);
    if ($ref && $ref.indexOf('#/definitions/') === 0) {
      return $ref.substring(14);
    }
  }

  function checkSchemaAgainst(gramene) {
    var scheamNameFromAPI = getSchemaName(gramene);
    if(scheamNameFromAPI !== schemaName) {
      throw new Error("Schema name mismatch. Expected " + schemaName + ", got " + scheamNameFromAPI);
    }
  }

  function makeCall(gramene, query) {
    var ids, params, apiMethodToInvoke;

    checkSchemaAgainst(gramene);

    ids = getIdListString(query);
    params = {
      idList: ids
    };
    apiMethodToInvoke = getCallFunction(gramene);

    return apiMethodToInvoke(params).then(function addApiToResponseAndResolvePromise(res) {
      res.client = gramene;
      return res;
    });
  }

  return function promise(queryString) {
    return grameneSwaggerClient
      .then(function makeCallFactory(client) {
        return makeCall(client, queryString);
      })
      .then(validate(schemaName))
{'fraction_non_alphanumeric': 0.12683186783906208, 'fraction_numerical': 0.0015987210231814548, 'mean_word_length': 4.221140472878998, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4903942', 'n_tokens_mistral': 1144, 'n_tokens_neox': 1123, 'n_words': 243}