Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Conform variable names to standardized vocabulary (pass#1) #47

Merged
merged 6 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/builder/builde_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ const template_builder = require("./template_query_builder")
const debug = require("debug")("bte:call-apis:query");


const builder_factory = (edge) => {
if ('tags' in edge && edge.tags.includes('bte-trapi')) {
const builder_factory = (APIEdge) => {
if ('tags' in APIEdge && APIEdge.tags.includes('bte-trapi')) {
debug(`using trapi builder now`)
return new trapi_builder(edge);
} else if (edge.query_operation.useTemplating) {
return new trapi_builder(APIEdge);
} else if (APIEdge.query_operation.useTemplating) {
debug("using template builder");
return new template_builder(edge);
return new template_builder(APIEdge);
}
debug('using default builder')
return new default_builder(edge);
return new default_builder(APIEdge);
}

module.exports = builder_factory;
64 changes: 32 additions & 32 deletions src/builder/query_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ nunjucksConfig(env);
module.exports = class QueryBuilder {
/**
* Constructor for Query Builder
* @param {object} edge - BTE Edge object with input field provided
* @param {object} APIEdge - BTE Edge object with input field provided
*/
constructor(edge) {
constructor(APIEdge) {
this.start = 0;
this.hasNext = false;
this.edge = edge;
this.APIEdge = APIEdge;
}

getUrl() {
return this.edge.query_operation.server + this.edge.query_operation.path;
return this.APIEdge.query_operation.server + this.APIEdge.query_operation.path;
}

_getUrl(edge, input) {
let server = edge.query_operation.server;
_getUrl(APIEdge, input) {
let server = APIEdge.query_operation.server;
if (server.endsWith("/")) {
server = server.substring(0, server.length - 1);
}
let path = edge.query_operation.path;
if (Array.isArray(edge.query_operation.path_params)) {
edge.query_operation.path_params.map(param => {
const val = edge.query_operation.params[param];
let path = APIEdge.query_operation.path;
if (Array.isArray(APIEdge.query_operation.path_params)) {
APIEdge.query_operation.path_params.map(param => {
const val = APIEdge.query_operation.params[param];
path = path.replace("{" + param + "}", val).replace("{inputs[0]}", input);
});
}
Expand All @@ -38,28 +38,28 @@ module.exports = class QueryBuilder {
/**
* Construct input based on method and inputSeparator
*/
_getInput(edge) {
if (edge.query_operation.supportBatch === true) {
if (Array.isArray(edge.input)) {
return edge.input.join(edge.query_operation.inputSeparator);
_getInput(APIEdge) {
if (APIEdge.query_operation.supportBatch === true) {
if (Array.isArray(APIEdge.input)) {
return APIEdge.input.join(APIEdge.query_operation.inputSeparator);
}
}
return edge.input;
return APIEdge.input;
}

/**
* Construct parameters for API calls
*/
_getParams(edge, input) {
_getParams(APIEdge, input) {
const params = {};
Object.keys(edge.query_operation.params).map(param => {
if (Array.isArray(edge.query_operation.path_params) && edge.query_operation.path_params.includes(param)) {
Object.keys(APIEdge.query_operation.params).map(param => {
if (Array.isArray(APIEdge.query_operation.path_params) && APIEdge.query_operation.path_params.includes(param)) {
return;
}
if (typeof edge.query_operation.params[param] === "string") {
params[param] = edge.query_operation.params[param].replace("{inputs[0]}", input);
if (typeof APIEdge.query_operation.params[param] === "string") {
params[param] = APIEdge.query_operation.params[param].replace("{inputs[0]}", input);
} else {
params[param] = edge.query_operation.params[param];
params[param] = APIEdge.query_operation.params[param];
}
});
return params;
Expand All @@ -68,9 +68,9 @@ module.exports = class QueryBuilder {
/**
* Construct request body for API calls
*/
_getRequestBody(edge, input) {
if (edge.query_operation.request_body !== undefined && "body" in edge.query_operation.request_body) {
let body = edge.query_operation.request_body.body;
_getRequestBody(APIEdge, input) {
if (APIEdge.query_operation.request_body !== undefined && "body" in APIEdge.query_operation.request_body) {
let body = APIEdge.query_operation.request_body.body;
let data;
data = Object.keys(body).reduce(
(accumulator, key) => accumulator + key + "=" + body[key].toString().replace("{inputs[0]}", input) + "&",
Expand All @@ -84,20 +84,20 @@ module.exports = class QueryBuilder {
* Construct the request config for Axios reqeust.
*/
constructAxiosRequestConfig() {
const input = this._getInput(this.edge);
const input = this._getInput(this.APIEdge);
const config = {
url: this._getUrl(this.edge, input),
params: this._getParams(this.edge, input),
data: this._getRequestBody(this.edge, input),
method: this.edge.query_operation.method,
url: this._getUrl(this.APIEdge, input),
params: this._getParams(this.APIEdge, input),
data: this._getRequestBody(this.APIEdge, input),
method: this.APIEdge.query_operation.method,
timeout: 50000,
};
this.config = config;
return config;
}

needPagination(apiResponse) {
if (this.edge.query_operation.method === "get" && this.edge.tags.includes("biothings")) {
if (this.APIEdge.query_operation.method === "get" && this.APIEdge.tags.includes("biothings")) {
if (apiResponse.total > this.start + apiResponse.hits.length) {
if (this.start + apiResponse.hits.length < 10000) {
this.hasNext = true;
Expand All @@ -111,7 +111,7 @@ module.exports = class QueryBuilder {

getNext() {
this.start = Math.min(this.start + 1000, 9999);
const config = this.constructAxiosRequestConfig(this.edge);
const config = this.constructAxiosRequestConfig(this.APIEdge);
config.params.from = this.start;
if (config.params.size + this.start > 10000) {
config.params.size = 10000 - this.start;
Expand All @@ -122,7 +122,7 @@ module.exports = class QueryBuilder {

getConfig() {
if (this.hasNext === false) {
return this.constructAxiosRequestConfig(this.edge);
return this.constructAxiosRequestConfig();
}
return this.getNext();
}
Expand Down
60 changes: 30 additions & 30 deletions src/builder/template_query_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ nunjucksConfig(env);
module.exports = class TemplateQueryBuilder {
/**
* Constructor for Query Builder
* @param {object} edge - BTE Edge object with input field provided
* @param {object} APIEdge - BTE Edge object with input field provided
*/
constructor(edge) {
constructor(APIEdge) {
this.start = 0;
this.hasNext = false;
this.edge = edge;
this.APIEdge = APIEdge;
}

getUrl() {
return this.edge.query_operation.server + this.edge.query_operation.path;
return this.APIEdge.query_operation.server + this.APIEdge.query_operation.path;
}

_getUrl(edge, input) {
let server = edge.query_operation.server;
_getUrl(APIEdge, input) {
let server = APIEdge.query_operation.server;
if (server.endsWith("/")) {
server = server.substring(0, server.length - 1);
}
let path = edge.query_operation.path;
if (Array.isArray(edge.query_operation.path_params)) {
edge.query_operation.path_params.map(param => {
const val = edge.query_operation.params[param];
let path = APIEdge.query_operation.path;
if (Array.isArray(APIEdge.query_operation.path_params)) {
APIEdge.query_operation.path_params.map(param => {
const val = APIEdge.query_operation.params[param];
path = nunjucks.renderString(path.replace("{" + param + "}", val), input);
});
}
Expand All @@ -38,23 +38,23 @@ module.exports = class TemplateQueryBuilder {
/**
* Construct input based on method and inputSeparator
*/
_getInput(edge) {
return edge.input;
_getInput(APIEdge) {
return APIEdge.input;
}

/**
* Construct parameters for API calls
*/
_getParams(edge, input) {
_getParams(APIEdge, input) {
const params = {};
Object.keys(edge.query_operation.params).map(param => {
if (Array.isArray(edge.query_operation.path_params) && edge.query_operation.path_params.includes(param)) {
Object.keys(APIEdge.query_operation.params).map(param => {
if (Array.isArray(APIEdge.query_operation.path_params) && APIEdge.query_operation.path_params.includes(param)) {
return;
}
if (typeof edge.query_operation.params[param] === "string") {
params[param] = nunjucks.renderString(edge.query_operation.params[param], input);
if (typeof APIEdge.query_operation.params[param] === "string") {
params[param] = nunjucks.renderString(APIEdge.query_operation.params[param], input);
} else {
params[param] = edge.query_operation.params[param];
params[param] = APIEdge.query_operation.params[param];
}
});
return params;
Expand All @@ -63,11 +63,11 @@ module.exports = class TemplateQueryBuilder {
/**
* Construct request body for API calls
*/
_getRequestBody(edge, input) {
if (edge.query_operation.request_body !== undefined && "body" in edge.query_operation.request_body) {
let body = edge.query_operation.request_body.body;
_getRequestBody(APIEdge, input) {
if (APIEdge.query_operation.request_body !== undefined && "body" in APIEdge.query_operation.request_body) {
let body = APIEdge.query_operation.request_body.body;
let data;
if (edge.query_operation.requestBodyType === "object") {
if (APIEdge.query_operation.requestBodyType === "object") {
data = JSON.parse(nunjucks.renderString(body, input));
} else {
data = Object.keys(body).reduce((accumulator, key) => {
Expand All @@ -83,20 +83,20 @@ module.exports = class TemplateQueryBuilder {
* Construct the request config for Axios reqeust.
*/
constructAxiosRequestConfig() {
const input = this._getInput(this.edge);
const input = this._getInput(this.APIEdge);
const config = {
url: this._getUrl(this.edge, input),
params: this._getParams(this.edge, input),
data: this._getRequestBody(this.edge, input),
method: this.edge.query_operation.method,
url: this._getUrl(this.APIEdge, input),
params: this._getParams(this.APIEdge, input),
data: this._getRequestBody(this.APIEdge, input),
method: this.APIEdge.query_operation.method,
timeout: 50000,
};
this.config = config;
return config;
}

needPagination(apiResponse) {
if (this.edge.query_operation.method === "get" && this.edge.tags.includes("biothings")) {
if (this.APIEdge.query_operation.method === "get" && this.APIEdge.tags.includes("biothings")) {
if (apiResponse.total > this.start + apiResponse.hits.length) {
if (this.start + apiResponse.hits.length < 10000) {
this.hasNext = true;
Expand All @@ -110,7 +110,7 @@ module.exports = class TemplateQueryBuilder {

getNext() {
this.start = Math.min(this.start + 1000, 9999);
const config = this.constructAxiosRequestConfig(this.edge);
const config = this.constructAxiosRequestConfig(this.APIEdge);
config.params.from = this.start;
if (config.params.size + this.start > 10000) {
config.params.size = 10000 - this.start;
Expand All @@ -121,7 +121,7 @@ module.exports = class TemplateQueryBuilder {

getConfig() {
if (this.hasNext === false) {
return this.constructAxiosRequestConfig(this.edge);
return this.constructAxiosRequestConfig(this.APIEdge);
}
return this.getNext();
}
Expand Down
44 changes: 22 additions & 22 deletions src/builder/trapi_query_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ nunjucksConfig(env);
module.exports = class TRAPIQueryBuilder {
/**
* Constructor for Query Builder
* @param {object} edge - BTE Edge object with input field provided
* @param {object} APIEdge - BTE Edge object with input field provided
*/
constructor(edge) {
constructor(APIEdge) {
this.start = 0
this.hasNext = false
this.edge = edge;
this.APIEdge = APIEdge;
}

getUrl() {
return this.edge.query_operation.server + this.edge.query_operation.path;
return this.APIEdge.query_operation.server + this.APIEdge.query_operation.path;
}

_getUrl(edge, input) {
let server = edge.query_operation.server;
_getUrl(APIEdge, input) {
let server = APIEdge.query_operation.server;
if (server.endsWith('/')) {
server = server.substring(0, server.length - 1)
};
let path = edge.query_operation.path;
if (Array.isArray(edge.query_operation.path_params)) {
edge.query_operation.path_params.map(param => {
const val = edge.query_operation.params[param];
let path = APIEdge.query_operation.path;
if (Array.isArray(APIEdge.query_operation.path_params)) {
APIEdge.query_operation.path_params.map(param => {
const val = APIEdge.query_operation.params[param];
path = path.replace("{" + param + "}", val).replace("{inputs[0]}", input);
});
}
Expand All @@ -38,31 +38,31 @@ module.exports = class TRAPIQueryBuilder {
/**
* Construct input based on method and inputSeparator
*/
_getInput(edge) {
return edge.input;
_getInput(APIEdge) {
return APIEdge.input;
}

/**
* Construct TRAPI request body
*/
_getRequestBody(edge, input) {
_getRequestBody(APIEdge, input) {
const qg = {
"message": {
"query_graph": {
"nodes": {
"n0": {
"ids": Array.isArray(input) ? input : [input],
"categories": ["biolink:" + edge.association.input_type]
"categories": ["biolink:" + APIEdge.association.input_type]
},
"n1": {
"categories": ["biolink:" + edge.association.output_type]
"categories": ["biolink:" + APIEdge.association.output_type]
}
},
"edges": {
"e01": {
"subject": "n0",
"object": "n1",
"predicates": ["biolink:" + edge.association.predicate]
"predicates": ["biolink:" + APIEdge.association.predicate]
}
}
}
Expand All @@ -76,11 +76,11 @@ module.exports = class TRAPIQueryBuilder {
* Construct the request config for Axios reqeust.
*/
constructAxiosRequestConfig() {
const input = this._getInput(this.edge);
const input = this._getInput(this.APIEdge);
const config = {
url: this._getUrl(this.edge, input),
data: this._getRequestBody(this.edge, input),
method: this.edge.query_operation.method,
url: this._getUrl(this.APIEdge, input),
data: this._getRequestBody(this.APIEdge, input),
method: this.APIEdge.query_operation.method,
timeout: 50000,
headers: {
'Content-Type': 'application/json'
Expand All @@ -96,13 +96,13 @@ module.exports = class TRAPIQueryBuilder {
}

getNext() {
const config = this.constructAxiosRequestConfig(this.edge);
const config = this.constructAxiosRequestConfig(this.APIEdge);
return config;
}

getConfig() {
if (this.hasNext === false) {
return this.constructAxiosRequestConfig(this.edge);
return this.constructAxiosRequestConfig(this.APIEdge);
}
return this.getNext();
}
Expand Down
Loading