basilapi / basil

Building Apis SImpLy from sparql endpoints
http://basil.kmi.open.ac.uk
24 stars 5 forks source link

A way to pre-process parameters and post-process results #34

Open nandana opened 9 years ago

nandana commented 9 years ago

This is more of a new feature for the wishlist.

Say for example, if I have a simple query such as

select distinct ?city where { ?city a <http://dbpedia.org/ontology/City&gt; ; <http://dbpedia.org/ontology/country&gt; ?_name_iri . }

I would like to pass just the local name in the API and concatenate it with a prefix on the fly.

e.g. Italy -> <http://dbpedia.org/resource/Italy&gt;

Similarly it would be nice if some post processing results is possible, for example

select distinct ?country where { ?country a <http://dbpedia.org/ontology/City&gt; . }

So in this case, to convert <http://dbpedia.org/resource/Italy&gt; to "Italy".

I was guessing may this what the scripts are for but I couldn't find the information in the documentation.

And one last minor request, will it be possible to preserve the variable names when your generate the Swagger specification?

enridaga commented 9 years ago

For what concern param value pre-processing I couldn't find a use case so far that cannot be covered by SPARQL itself. For example:

select distinct ?city where {
   BIND(iri(concat("http://dbpedia.org/resource/", ?_name)) as ?country) .
   ?city a <http://dbpedia.org/ontology/City> ;
   <http://dbpedia.org/ontology/country> ?country .
}

There might be use cases not covered by SPARQL, and we could think to provide scripts for input preparation. However, I like the idea to keep BASIL as simple as possible, and I could not found such cases so far.

Post-processing can be done with Scripts. At the moment we support the Mustache language (that I recommend for returning HTML) and Javascript.

Documentation of this feature is not yet ready (mea culpa), I am putting few examples taken from the current catalog here, as a basis to start from:

MUSTACHE This is the very simple example, creating a list in HTML:

<ul>
{{#items}}
   <li><a href="{{id}}">{{label}}</a> ({{nationality}})<br/><small>{{id}}</small></li>
{{/items}}
</ul>

See it in action here: http://basil.kmi.open.ac.uk/basil/qhq3k9v61eu9/api.list?year=1966

Javascript: This snippet shows how to use Javascript (implemented via Rhino):

function(){
   while(this.items.hasNext()){
      var i = this.items.next();
      var c = (i.nationality.lastIndexOf('#') != -1) ? '#' : '/' ;
      var n = i.nationality.substring(i.nationality.lastIndexOf(c) + 1);
      this.print(i.label + " was born in " + n + "\n");
   }
   return this;
}

See it in action here: http://basil.kmi.open.ac.uk/basil/qhq3k9v61eu9/api.jsres?year=1966

The important bits are:

Result sets are streamed to the script, so if you follow this pattern you shouldn't get into troubles with memory consumption on large result sets.

About the last question, I am not sure I got what you mean, can you show an example?

Hope this helps.