从事务处理器函数调用 REST API

Calling a REST API from Transaction Processor Functions

我正在尝试通过事务处理函数从开放空间 api 获取数据。我一直在看文档:https://hyperledger.github.io/composer/integrating/call-out

因为我正在对此 API 执行 GET 请求,所以我不需要随请求发送任何数据。这就是我将参数数据设置为空对象的原因:

var data = {};

但是当我 运行 这样做时,我得到以下错误:

Error: Serializer.toJSON only accepts instances of Resource.

此错误是因为 toJSON(resource, options) 函数需要资源。这可以在下面看到 link:

https://hyperledger.github.io/composer/jsdoc/module-composer-common.Serializer.html#toJSON__anchor

我还查看了 post() 函数的代码,我将其复制在下面:第 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);
            });
    };

但我可以找到任何解决方法..

我的代码:

    /**
 * 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);
      });
    }
}

是否可以从事务处理函数中执行此操作?

仅当 data 是建模 .cto 文件中的资源(概念、交易、资产或参与者)时,post(url, data) 用法才有效。

你试过用空概念吗?也许这会奏效(只是猜测)

参考:https://hyperledger.github.io/composer/integrating/call-out.html

我认为您需要考虑您尝试提取的数据是否可以在客户端应用程序级别完成,然后更新到区块链上,这可能会容易得多。

您还可以考虑将服务器置于中间以将 HTTP post 请求转换为开放天空的 get 请求的方法。