Reputation: 447
I'm trying to get data from the open sky api through a transaction processor function. I have been looking at the documentation: https://hyperledger.github.io/composer/integrating/call-out
Since I'm doing a GET request to this API I don't need to send any data with the request. This is why I have set the parameter data to an empty object:
var data = {};
But when I run this I get the following error:
Error: Serializer.toJSON only accepts instances of Resource.
This error is because the toJSON(resource, options)
function is expecting a resource. This can be seen in the following link:
https://hyperledger.github.io/composer/jsdoc/module-composer-common.Serializer.html#toJSON__anchor
I also looked at the code for the post() function, which I will copy below: On line 224: https://hyperledger.github.io/composer/jsdoc/composer-runtime_lib_api.js.html
/**
* Post a typed instance to a HTTP URL
* @method module:composer-runtime#post
* @param {string} url The URL to post the data to
* @param {Typed} typed The typed instance to be posted. The instance will be serialized to JSON.
* @param {object} options The options that are passed to Serializer.toJSON
* @return {Promise} A promise. The promise is resolved with a HttpResponse
* that represents the result of the HTTP POST.
* @public
*/
this.post = function post(url, typed, options) {
const method = 'post';
LOG.entry(method, url, typed);
const data = serializer.toJSON(typed, options);
LOG.debug(method, typed.getFullyQualifiedType(), data);
return httpService.post(url, data)
.then((response) => {
LOG.exit(method);
return Promise.resolve(response);
});
};
But I could see any way around it..
My code:
/**
* Transaction to allow parties to Monitor planes
* @param {org.****.MonitorPlane} monitorPlane
* @transaction
*/
function monitorPlane(monitorPlane){
var NS = 'org.****';
plane = monitorPlane.plane
var location
return location = getLocation()
.then(function(){
plane.lat = lat
plane.long = long
if(plane.monitorPlane){
plane.monitorPlane.push(monitorPlane)
}else{
plane.monitorPlane = [monitorPlane]
}
}).then(function(){
return getAssetRegistry(NS + '.Plane')
}).then(function(planeRegistry){
return planeRegistry.update(plane);
});
function getLocation(){
var url = 'https://opensky-network.org/api/states/all?time=1458564121&icao24=3c6444';
var data = {};
return post(url,data)
.then(function (resp) {
console.log(resp);
return resp;
})
.then(function(data) {
console.log(data);
lat = data.states[0][5]
long = data.states[0][6]
lat = lat.toString()
long = long.toString()
location = [lat,long]
return location
})
.catch((err) => {
console.log(err);
});
}
}
Is it possible to do this from a transaction processor function?
Upvotes: 0
Views: 633
Reputation: 128
The post(url, data)
usage only works if data
is a resource (concept, transaction, asset or participant) in your modelling .cto file.
Have you tried using an empty concept? Maybe that will work (just a guess)
Ref: https://hyperledger.github.io/composer/integrating/call-out.html
I think you need to consider if the data you are trying to pull can be done at the client application level and then update onto the blockchain, which could be much easier.
You may also consider ways you can put a server in the middle to transform your HTTP post request to a get request for open sky.
Upvotes: 1