代码之家  ›  专栏  ›  技术社区  ›  Simon Mullaney

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

  •  0
  • Simon Mullaney  · 技术社区  · 8 年前

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

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

    var data = {};

    但是,当我运行此命令时,会出现以下错误:

    错误:序列化程序。toJSON只接受资源的实例。

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

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

    是否可以从事务处理器功能执行此操作?

    1 回复  |  直到 8 年前
        1
  •  1
  •   dtylam    8 年前

    这个 post(url, data) 仅在以下情况下使用 data 是建模中的资源(概念、交易、资产或参与者)。cto文件。

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

    裁判: https://hyperledger.github.io/composer/integrating/call-out.html

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

    您还可以考虑将服务器置于中间的方法,将HTTP post请求转换为open sky的get请求。