Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from mboudet/dev_uri
Add a route to avoid 404 in URIs
- Loading branch information
Showing
13 changed files
with
558 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"""Api routes""" | ||
import sys | ||
import traceback | ||
|
||
from askomics.libaskomics.SparqlQuery import SparqlQuery | ||
from askomics.libaskomics.SparqlQueryLauncher import SparqlQueryLauncher | ||
|
||
from flask import (Blueprint, current_app, jsonify, session) | ||
|
||
|
||
data_bp = Blueprint('data', __name__, url_prefix='/') | ||
|
||
|
||
@data_bp.route('/api/data/<string:uri>', methods=['GET']) | ||
def get_data(uri): | ||
"""Get information about uri | ||
Returns | ||
------- | ||
json | ||
error: True if error, else False | ||
errorMessage: the error message of error, else an empty string | ||
""" | ||
|
||
try: | ||
query = SparqlQuery(current_app, session) | ||
graphs, endpoints = query.get_graphs_and_endpoints(all_selected=True) | ||
|
||
endpoints = [val['uri'] for val in endpoints.values()] | ||
|
||
data = [] | ||
|
||
# If the user do not have access to any endpoint (no viewable graph), skip | ||
if endpoints: | ||
|
||
base_uri = current_app.iniconfig.get('triplestore', 'namespace_data') | ||
full_uri = "<%s%s>" % (base_uri, uri) | ||
|
||
raw_query = "SELECT DISTINCT ?predicat ?object\nWHERE {\n%s ?predicat ?object\n}" % (full_uri) | ||
federated = query.is_federated() | ||
replace_froms = query.replace_froms() | ||
|
||
sparql = query.format_query(raw_query, replace_froms=replace_froms, federated=federated) | ||
|
||
query_launcher = SparqlQueryLauncher(current_app, session, get_result_query=True, federated=federated, endpoints=endpoints) | ||
header, data = query_launcher.process_query(sparql) | ||
|
||
except Exception as e: | ||
current_app.logger.error(str(e).replace('\\n', '\n')) | ||
traceback.print_exc(file=sys.stdout) | ||
return jsonify({ | ||
'error': True, | ||
'errorMessage': str(e).replace('\\n', '\n'), | ||
'data': [] | ||
}), 500 | ||
|
||
return jsonify({ | ||
'data': data, | ||
'error': False, | ||
'errorMessage': "" | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import React, { Component } from 'react' | ||
import axios from 'axios' | ||
import { Button, Form, FormGroup, Label, Input, Alert, Row, Col, CustomInput } from 'reactstrap' | ||
import BootstrapTable from 'react-bootstrap-table-next' | ||
import paginationFactory from 'react-bootstrap-table2-paginator' | ||
import cellEditFactory from 'react-bootstrap-table2-editor' | ||
import update from 'react-addons-update' | ||
import { withRouter } from "react-router-dom"; | ||
import PropTypes from 'prop-types' | ||
import Utils from '../../classes/utils' | ||
import { Redirect } from 'react-router-dom' | ||
import WaitingDiv from '../../components/waiting' | ||
import ErrorDiv from '../error/error' | ||
|
||
class Data extends Component { | ||
constructor (props) { | ||
super(props) | ||
this.utils = new Utils() | ||
this.state = { | ||
isLoading: true, | ||
error: false, | ||
errorMessage: '', | ||
data: [], | ||
} | ||
} | ||
|
||
componentDidMount () { | ||
if (!this.props.waitForStart) { | ||
this.loadData() | ||
} | ||
} | ||
|
||
loadData() { | ||
let uri = this.props.match.params.uri; | ||
let requestUrl = '/api/data/' + uri | ||
axios.get(requestUrl, { baseURL: this.props.config.proxyPath, cancelToken: new axios.CancelToken((c) => { this.cancelRequest = c }) }) | ||
.then(response => { | ||
console.log(requestUrl, response.data) | ||
this.setState({ | ||
isLoading: false, | ||
data: response.data.data, | ||
}) | ||
}) | ||
.catch(error => { | ||
console.log(error, error.response.data.errorMessage) | ||
this.setState({ | ||
error: true, | ||
errorMessage: error.response.data.errorMessage, | ||
success: !error.response.data.error | ||
}) | ||
}) | ||
} | ||
|
||
componentWillUnmount () { | ||
if (!this.props.waitForStart) { | ||
this.cancelRequest() | ||
} | ||
} | ||
|
||
render () { | ||
let uri = this.props.match.params.uri; | ||
|
||
let columns = [{ | ||
dataField: 'predicat', | ||
text: 'Property', | ||
sort: true, | ||
formatter: (cell, row) => { | ||
if (this.utils.isUrl(cell)) { | ||
return this.utils.splitUrl(cell) | ||
} | ||
return cell | ||
} | ||
},{ | ||
dataField: 'object', | ||
text: 'Value', | ||
sort: true, | ||
formatter: (cell, row) => { | ||
if (this.utils.isUrl(cell)) { | ||
if (cell.startsWith(this.props.config.namespaceInternal)){ | ||
return this.utils.splitUrl(cell) | ||
} else { | ||
return <a href={cell}>{this.utils.splitUrl(cell)}</a> | ||
} | ||
} | ||
return cell | ||
} | ||
}] | ||
|
||
|
||
return ( | ||
<div className="container"> | ||
<h2>Information about uri {uri}</h2> | ||
<br /> | ||
<div className="asko-table-height-div"> | ||
<BootstrapTable | ||
classes="asko-table" | ||
wrapperClasses="asko-table-wrapper" | ||
tabIndexCell | ||
bootstrap4 | ||
keyField='id' | ||
data={this.state.data} | ||
columns={columns} | ||
pagination={paginationFactory()} | ||
noDataIndication={'No results for this URI. You may not have access to any graph including it.'} | ||
/> | ||
</div> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
Data.propTypes = { | ||
waitForStart: PropTypes.bool, | ||
config: PropTypes.object, | ||
match: PropTypes.object | ||
} | ||
|
||
export default withRouter(Data) |
Oops, something went wrong.