Skip to content

Instantly share code, notes, and snippets.

@soukron
Last active April 29, 2021 07:34
Show Gist options
  • Save soukron/766db7f7a90f4213bdde to your computer and use it in GitHub Desktop.
Save soukron/766db7f7a90f4213bdde to your computer and use it in GitHub Desktop.
Simple plugin for Baasbox to act as an API gateway for Foursquare venues API
/* script: venues.fs.search */
/* This script endpoint: /plugin/venues.fs.search */
on('install',function(installEvent){
var success = Box.DB.createCollection('venues-cache-queries');
var success = Box.DB.createCollection('venues-cache-answers');
return true;
});
http().get(function(req){
// parse parameter in format "lat,long" to array
var ll = req.queryString.ll.toString();
var llarray = ll.split(',');
// very basic check: we need two coordinates
if (llarray.length == 2) {
// query in cache collection for previous queries with 100m of distance
var queries = Box.Documents.find('venues-cache-queries', {
where: 'endpoint = "search" and distance(lat, long, ' +
llarray[0] + ', ' + llarray[1] + ') < 0.1',
orderBy: '_creation_date desc'
});
// if there's a previous query, let's answer its content
if (queries.length > 0) {
var answer = Box.Documents.find('venues-cache-answers', {
where: 'id = "'+queries[0]['answer-id'].toString() + '"'
});
return {status: answer[0].meta.code, content: answer[0]};
}
// otherwise, call to FS api endpoint
else {
var result = Box.WS.get('https://api.foursquare.com/v2/venues/search/',
{
params:{
ll: ll,
intent: "checkin",
radius: 250,
limit: 20,
locale: "es",
v: "20150827",
m: "swarm",
client_id: "YOURCLIENTID",
client_secret: "YOURCLIENTSECRET"
}
}
);
// create document with query information
var answer=Box.Documents.save('venues-cache-answers', result.body);
// create document with answer from FS
var query=Box.Documents.save('venues-cache-queries', {
"endpoint": "search",
"lat": llarray[0],
"long": llarray[1],
"answer-id": answer.id
});
// set permissions for both previous documents so all registered
// users can read its content
Box.Documents.grant('venues-cache-answers',answer.id,
{
roles: {
read:["registered"],
},
});
Box.Documents.grant('venues-cache-queries',query.id,
{
roles: {
read:["registered"],
},
});
// return FS information
return {status: result.body.meta.code, content: result.body};
}
}
// otherwise return not found
else {
return {status: 404};
}
});
@giastfader
Copy link

Hi,
great job!
Just few suggestions:
https://gist.github.com/soukron/766db7f7a90f4213bdde#file-venues-fs-search-js-L5
instead of Box.DB.createCollection() that returns false in case of error (collection already exists), use Box.DB.ensureCollection(). This is useful when the plugin is updated.

https://gist.github.com/soukron/766db7f7a90f4213bdde#file-venues-fs-search-js-L54
https://gist.github.com/soukron/766db7f7a90f4213bdde#file-venues-fs-search-js-L69

You can save and set the ACL in one statement (baasbox/baasbox#344 (comment)):

var query=Box.Documents.save('venues-cache-queries', {
                "endpoint": "search",
                "lat": llarray[0],
                "long": llarray[1],
                "answer-id": answer.id,
                "_allowRead": [{"name":"registered","isrole":true}]
            });

As far as the documents belonging to the venues-cache-answers collection this is not yet possible due baasbox/baasbox#832

Finally I suggest to avoid the concatenation of values coming from the querystring into the query statements, use the positional parameters instead https://github.com/baasbox/baasbox/wiki/PluginApi#query-1
(the example is for Links but the concept works with Documents as well).

@soukron
Copy link
Author

soukron commented Apr 29, 2021

Oh my god. I never answered to your suggestions. Thanks so much. I had a good time with Baasbox.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment