Search code examples
javascriptajaxibm-cloudswaggerpresenceinsights

How can I send data to Presence Insights connectors API via Swagger or Ajax?


UPDATE 19 Feb 2016 - see below

I'm building an hybrid mobile app for proximity marketing (i.e. a mobile app which will interact with beacons), and I'd like to use the Bluemix Presence Insights Service to collect data. The problem is I cannot connect through SDK since it is an hybrid app therefore I need to use the connectors API. I've made some attempts and I still get the 401 response.

I've tried an Ajax call setting the Basic Autentication Headers:

beforeSend: function (xhr) {
    xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
},

or

beforeSend: function (xhr) {
    xhr.setRequestHeader ("Authorization", "Basic <my encrypted token>");
},

or

headers: {
    "Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD)
  },

(also with the encrypted token, I'm not rewriting it).

Then I've made a Node.js server and installed swagger-client (i.e. I've changed strategy):

router.post('/', function (req, res, next) {
    var json = req.body.json;
    var client = new Swagger({
        url: 'https://presenceinsights.ibmcloud.com/pi-swagger/the-connector-api',
        success: function () {
            console.log("success");
            console.log(JSON.stringify(client));
        },
        authorizations : {
            easyapi_basic: new Swagger.PasswordAuthorization('username', 'password')
        }

    });

    res.end();
});

This time I've made progress:

I can successfully connect but I'm not able to send the JSON with the data. Having not access to swagger.json how do I declare the body of the post request? Is there any way to get access to swagger.json of presence insights (this thing will solve all my problems)?

UPDATE After an exchange of information with Presence Insights support I can say it is not viable to call the Swagger client. The only way to use the service with an hybrid app, it is building a proxy Blumix runtime which it will forward the datas from the hybrid application to the Presence Insights service. Any other attempt, either with an external server or by calling the API within the application is not permitted, as CORS policy doesn't allow it. This is true for connectors AND management.

I'm writing this for future reference.


Solution

  • the endpoint URL that you're putting needs to be the location of the swagger definition, or swagger specification. Once that's loaded, you can make calls to the API. Note, the swagger client that you're initializing will dynamically create functions based on that definition.

    Now, poking around a tiny bit, I did see that the swagger definition is actually available for this service:

    https://presenceinsights.ibmcloud.com/pi-swagger/swagger.json

    Once you put that in the client, then you can technically make calls against the server.

    There is, however, an issue with this service. The swagger definition doesn't look valid, and because of that it won't be possible to use the javascript client. For example:

    https://online.swagger.io/validator/debug?url=https://presenceinsights.ibmcloud.com/pi-swagger/swagger.json

    Shows many errors. I may be using the swagger.json from this service incorrectly (I did sniff it out) but what I pointed out--using the definition when constructing the client--is how this library works.