4

In Paw app, if we create a request manually, we can invoke the context menu and pick a environment variable.

In this case the URL looks likes this:

And it would be updated should I change the environment or the variable itself.

I'm trying to improving an existing plugin (API Blueprint Importer) and I only figured out that how to read from environment variable.
I can only do something like this:
http, localhost, and 8000 were read from environment variables.

Trying to achieve something like the in the first picture, but to no avail.
Is there an API available in the Paw App, or it's not available right now?

esfy
  • 673
  • 1
  • 6
  • 13

1 Answers1

3

Creating references to environment variables dynamically is definitely feasible in Paw. First you will need to create the environment variable, and then create a Dynamic Value that references to it.

Setting up the environment domain dynamically

Since environment variables are stored in domains, also called groups in the app, you should first create an environment domain with the desired name using context.getEnvironmentDomainByName and context.createEnvironmentDomain. You can read up more about these two methods on 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
}

Once the environment domain is created, you need to add an environment in which to store the variable. The process is very similar to the creation of an environment domain. You can find more information on the methods used here 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
}

The next step is to create the variable, if it doesn't exist, or to return it if it does.

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

Setting up the reference

To create a reference to an environment variable, you need to create an Environment Variable Dynamic Value. its identifier is com.luckymarmot.EnvironmentVariableDynamicValue, and it takes only one parameter environmentVariable, which is the id of the variable it refers to.

...
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 */
...
Jonathan Montane
  • 386
  • 3
  • 10