|
| 1 | +import httpContext from 'express-http-context'; |
1 | 2 | import SC2 from 'sparql-client-2'; |
2 | 3 | const { SparqlClient, SPARQL } = SC2; |
3 | 4 |
|
4 | 5 | //==-- logic --==// |
5 | 6 |
|
6 | 7 | // builds a new sparqlClient |
7 | 8 | function newSparqlClient() { |
8 | | - return new SparqlClient( process.env.MU_SPARQL_ENDPOINT ).register({ |
| 9 | + let options = { |
| 10 | + requestDefaults: { |
| 11 | + headers: { |
| 12 | + 'mu-session-id': httpContext.get('request').get('mu-session-id') |
| 13 | + } |
| 14 | + } |
| 15 | + }; |
| 16 | + |
| 17 | + const authorizationGroups = httpContext.get('response').get('mu-authorization-groups'); |
| 18 | + if (authorizationGroups) |
| 19 | + options.requestDefaults.headers['mu-authorization-groups'] = authorizationGroups; |
| 20 | + |
| 21 | + console.log(`Add options on SparqlClient: ${JSON.stringify(options)}`); |
| 22 | + |
| 23 | + return new SparqlClient(process.env.MU_SPARQL_ENDPOINT, options).register({ |
9 | 24 | mu: 'http://mu.semte.ch/vocabularies/', |
10 | 25 | muCore: 'http://mu.semte.ch/vocabularies/core/', |
11 | 26 | muExt: 'http://mu.semte.ch/vocabularies/ext/' |
12 | 27 | }); |
13 | 28 | } |
14 | 29 |
|
15 | 30 | // executes a query (you can use the template syntax) |
16 | | -function query( queryString ){ |
| 31 | +function query( queryString ) { |
17 | 32 | console.log(queryString); |
18 | | - return newSparqlClient().query( queryString ).execute(); |
| 33 | + return newSparqlClient().queryRaw(queryString).execute().then(response => { |
| 34 | + const authorizationGroups = response.headers['mu-authorization-groups']; |
| 35 | + if (authorizationGroups) { |
| 36 | + httpContext.get('response').setHeader('mu-authorization-groups', authorizationGroups); |
| 37 | + console.log(`Set mu-authorization-groups header to ${authorizationGroups}`); |
| 38 | + } else { |
| 39 | + httpContext.get('response').removeHeader('mu-authorization-groups'); |
| 40 | + console.log('Remove mu-authorization-groups header'); |
| 41 | + } |
| 42 | + |
| 43 | + function maybeParseJSON(body) { |
| 44 | + // Catch invalid JSON |
| 45 | + try { |
| 46 | + return JSON.parse(body); |
| 47 | + } catch (ex) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return maybeParseJSON(response.body); |
| 53 | + }); |
19 | 54 | }; |
20 | 55 |
|
21 | 56 | // executes an update query |
|
0 commit comments