使用Paw中的环境变量创建请求

时间:2016-09-18 08:32:58

标签: paw-app

在Paw app中,如果我们手动创建请求,我们可以调用上下文菜单并选择一个环境变量。

在这种情况下,URL看起来像这样:

如果我改变环境或变量本身,它会更新。

我正在尝试改进现有的插件(API Blueprint Importer),我只知道如何从环境变量中读取。
我只能这样做:
从环境变量中读取了httplocalhost8000

尝试实现第一张图片中的内容,但无济于事 Paw应用程序中是否有可用的API,或者它现在不可用?

1 个答案:

答案 0 :(得分:3)

动态创建对环境变量的引用在Paw中绝对可行。首先,您需要创建环境变量,然后创建一个引用它的动态值。

动态设置环境域

由于环境变量存储在域中,也称为应用程序中的组,因此您应首先使用context.getEnvironmentDomainByNamecontext.createEnvironmentDomain创建具有所需名称的环境域。您可以在the documentation page for the context object上阅读有关这两种方法的更多信息。

getOrCreateEnvironmentDomain(name) {
    let env = this.context.getEnvironmentDomainByName(name)
    if (typeof env === 'undefined') {
        env = context.createEnvironmentDomain(name)
    }
    return env
}

创建环境域后,您需要添加用于存储变量的环境。该过程与创建环境域非常相似。您可以在此处找到有关此处使用的方法的更多信息on the documentation page for the EnvironmentDomain

getOrCreateEnvironment(domain, name) {
    let env = domain.getEnvironmentByName(name)
    if (typeof env === 'undefined') {
        env = domain.createEnvironment(name)
    }
    return env
}

下一步是创建变量(如果它不存在),或者如果变量存在则返回它。

/* 
    uses:
        @getOrCreateEnvironmentDomain
        @getOrCreateEnvironment
*/
updateOrCreateEnvironmentVariable(domainName, envName, name, value) {
    let domain = this.getOrCreateEnvironmentDomain(domainName)
    let env = this.getOrCreateEnvironment(domain, envName)
    let varDict = {}

    varDict[name] = typeof value !== 'undefined' ? value: ''
    env.setVariablesValues(varDict)
    return domain.getVariableByName(name)
}

设置参考

要创建对环境变量的引用,您需要创建环境变量动态值。它的标识符是com.luckymarmot.EnvironmentVariableDynamicValue,它只需要一个参数environmentVariable,它是它引用的变量的id。

...
let envVariable = this.updateOrCreateEnvironmentVariable('Server', 'api-blueprint', 'protocol', 'https')
let dv = new DynamicValue(
    'com.luckymarmot.EnvironmentVariableDynamicValue',
    {
        environmentVariable: envVariable.id
    }
)
/* use the Dynamic Value like any other */
...