首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型记录管理REST调用和回调

类型记录管理REST调用和回调
EN

Stack Overflow用户
提问于 2018-06-26 05:41:56
回答 1查看 60关注 0票数 0

我正在使用谷歌防火墙功能与类型记录。我有一个关于更好的代码管理的基本问题。目前,我的代码如下所示:

代码语言:javascript
复制
export const on_command_ping = functions.database.ref("commands/ping/{id}").onWrite(async (change, context) => {
    if(instr == '/my-sr'){
        const reoptions = {
            uri: baseUrl + '/serviceRequests',
            headers: {
                'Authorization': "Basic " + btoa(username + ":" + password)
            },
            json:true
        };

        const result = await rp.get(reoptions)
            .then(function(resp){
                console.log("got the response dude:" + JSON.stringify(resp))


                const options = {
                    uri: respUrl, 
                    method: "POST",
                    json: true,
                    body: { "attachments": [{
                                    "fallback": "Sorry failed to get response"}]
                          }
                 }
                 return rp(options);
               }));
     }else  if(instr == '/my-oher-stuff'){
        //another REST call
      }

正如您在上面看到的,这将很难管理单个函数中的所有内容。那么,如何组织这个代码,使每个rest调用是一个独立的函数,从上面调用基于if- each。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-26 05:51:32

您可以将代码放入函数中的IF块中。

例如:

代码语言:javascript
复制
export const on_command_ping = functions.database.ref("commands/ping/{id}").onWrite(async (change, context) => {
    if (instr == '/my-sr') {
        return function1(change, context)
    }
    else if (instr == '/my-oher-stuff') {
        return function2(change, context)
    } 
    else {
        return function3(change, context)
    }

});

function function1(change, context) {
    const reoptions = {
        uri: baseUrl + '/serviceRequests',
        headers: {
            'Authorization': "Basic " + btoa(username + ":" + password)
        },
        json: true
    };

    const result = await
    rp.get(reoptions)
        .then(function (resp) {
            console.log("got the response dude:" + JSON.stringify(resp))


            const options = {
                uri: respUrl,
                method: "POST",
                json: true,
                body: {
                    "attachments": [{
                        "fallback": "Sorry failed to get response"
                    }]
                }
            }
            return rp(options);
        }));
}

function function2(change, context) {
    //Some code here
}

function function3(change, context) {
    //Some code here
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51035592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档