Skip to content
Snippets Groups Projects
Commit ed017b7d authored by esikkala's avatar esikkala
Browse files

Add support for csv result format

parent 73705e16
No related branches found
No related tags found
No related merge requests found
......@@ -41,12 +41,19 @@ app.get(`${apiPath}/:resultClass/paginated`, async (req, res, next) => {
try {
const data = await getPaginatedResults({
resultClass: req.params.resultClass,
page: parseInt(req.query.page) || null,
page: req.query.page == null ? null : req.query.page,
pagesize: parseInt(req.query.pagesize) || null,
sortBy: req.query.sortBy || null,
sortDirection: req.query.sortDirection || null,
constraints: req.query.constraints == null ? null : JSON.parse(req.query.constraints),
resultFormat: req.query.resultFormat == null ? 'json' : req.query.resultFormat
});
// test csv result format
// res.writeHead(200, {
// 'Content-Type': 'text/csv',
// 'Content-Disposition': 'attachment; filename=results.csv'
// });
// res.end(data);
res.json(data);
} catch(error) {
next(error);
......@@ -60,6 +67,7 @@ app.get(`${apiPath}/:resultClass/all`, async (req, res, next) => {
facetClass: req.query.facetClass || null,
constraints: req.query.constraints == null ? null : JSON.parse(req.query.constraints),
variant: req.query.variant || null,
resultFormat: req.query.resultFormat == null ? 'json' : req.query.resultFormat
});
res.json({
results: data
......@@ -74,6 +82,7 @@ app.get(`${apiPath}/:resultClass/count`, async (req, res, next) => {
const count = await getResultCount({
resultClass: req.params.resultClass,
constraints: req.query.constraints == null ? null : JSON.parse(req.query.constraints),
resultFormat: req.query.resultFormat == null ? 'json' : req.query.resultFormat
});
res.json({ count });
} catch(error) {
......@@ -88,7 +97,8 @@ app.get(`${apiPath}/:resultClass/instance/:uri`, async (req, res, next) => {
facetClass: req.query.facetClass || null,
constraints: req.query.constraints == null ? null : JSON.parse(req.query.constraints),
variant: req.query.variant || null,
uri: req.params.uri
uri: req.params.uri,
resultFormat: req.query.resultFormat == null ? 'json' : req.query.resultFormat
});
// there is always one object in the 'data' array
res.json(data[0]);
......@@ -105,6 +115,7 @@ app.get(`${apiPath}/:facetClass/facet/:id`, async (req, res, next) => {
sortBy: req.query.sortBy || null,
sortDirection: req.query.sortDirection || null,
constraints: req.query.constraints == null ? null : JSON.parse(req.query.constraints),
resultFormat: req.query.resultFormat == null ? 'json' : req.query.resultFormat
});
res.json(data);
} catch(error) {
......@@ -134,6 +145,7 @@ app.get(`${apiPath}/search`, async (req, res, next) => {
longMin: longMin,
latMax: latMax,
longMax: longMax,
resultFormat: req.query.resultFormat == null ? 'json' : req.query.resultFormat
});
res.json(data);
} catch(error) {
......
......@@ -24,7 +24,8 @@ export const getPaginatedResults = async ({
pagesize,
constraints,
sortBy,
sortDirection
sortDirection,
resultFormat
}) => {
const data = await getPaginatedData({
resultClass,
......@@ -32,20 +33,26 @@ export const getPaginatedResults = async ({
pagesize,
constraints,
sortBy,
sortDirection
sortDirection,
resultFormat
});
return {
pagesize: pagesize,
page: page,
results: data
};
if (resultFormat === 'json') {
return {
pagesize: pagesize,
page: page,
results: data
};
} else {
return data;
}
};
export const getAllResults = ({
// resultClass, // TODO: handle other classes than manuscripts
facetClass,
constraints,
variant
variant,
resultFormat
}) => {
let q = '';
let filterTarget = '';
......@@ -80,12 +87,13 @@ export const getAllResults = ({
// if (variant == 'productionPlaces') {
// console.log(prefixes + q)
// }
return runSelectQuery(prefixes + q, endpoint, makeObjectList);
return runSelectQuery(prefixes + q, endpoint, makeObjectList, resultFormat);
};
export const getResultCount = ({
resultClass,
constraints
constraints,
resultFormat
}) => {
let q = countQuery;
q = q.replace('<FACET_CLASS>', facetConfigs[resultClass].facetClass);
......@@ -100,7 +108,7 @@ export const getResultCount = ({
facetID: null
}));
}
return runSelectQuery(prefixes + q, endpoint, mapCount);
return runSelectQuery(prefixes + q, endpoint, mapCount, resultFormat);
};
const getPaginatedData = ({
......@@ -109,7 +117,8 @@ const getPaginatedData = ({
pagesize,
constraints,
sortBy,
sortDirection
sortDirection,
resultFormat
}) => {
let q = facetResultSetQuery;
const facetConfig = facetConfigs[resultClass];
......@@ -163,8 +172,8 @@ const getPaginatedData = ({
resultSetProperties = '';
}
q = q.replace('<RESULT_SET_PROPERTIES>', resultSetProperties);
// console.log(prefixes + q)
return runSelectQuery(prefixes + q, endpoint, makeObjectList);
console.log(prefixes + q);
return runSelectQuery(prefixes + q, endpoint, makeObjectList, resultFormat);
};
export const getByURI = ({
......@@ -172,7 +181,8 @@ export const getByURI = ({
facetClass,
constraints,
//variant,
uri
uri,
resultFormat
}) => {
let q;
switch (resultClass) {
......@@ -194,5 +204,5 @@ export const getByURI = ({
// if (variant === 'productionPlaces') {
// console.log(prefixes + q)
// }
return runSelectQuery(prefixes + q, endpoint, makeObjectList);
return runSelectQuery(prefixes + q, endpoint, makeObjectList, resultFormat);
};
......@@ -25,6 +25,7 @@ export const getFacet = ({
sortBy,
sortDirection,
constraints,
resultFormat
}) => {
const facetConfig = facetConfigs[facetClass][facetID];
// choose query template and result mapper:
......@@ -104,7 +105,7 @@ export const getFacet = ({
// if (facetID == 'productionPlace') {
// console.log(prefixes + q)
// }
return runSelectQuery(prefixes + q, endpoint, mapper);
return runSelectQuery(prefixes + q, endpoint, mapper, resultFormat);
};
const generateSelectedBlock = ({
......
import axios from 'axios';
import querystring from 'querystring';
const defaultSelectHeaders = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/sparql-results+json; charset=utf-8'
};
// const defaultConstructHeaders = {
// 'Content-Type': 'application/x-www-form-urlencoded',
// 'Accept': 'text/turtle'
// };
export const runSelectQuery = async (query, endpoint, resultMapper) => {
export const runSelectQuery = async (query, endpoint, resultMapper, resultFormat) => {
let MIMEtype = resultFormat === 'json'
? 'application/sparql-results+json; charset=utf-8'
: 'text/csv; charset=utf-8';
const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': MIMEtype
};
try {
const response = await axios({
method: 'post',
headers: defaultSelectHeaders,
headers: headers,
url: endpoint,
data: querystring.stringify({ query }),
});
return resultMapper(response.data.results.bindings);
if (resultFormat === 'json') {
return resultMapper(response.data.results.bindings);
} else {
return response.data;
}
} catch(error) {
if (error.response) {
// The request was made and the server responded with a status code
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment