How to select KVM conditionally based on apikey/api name?

abhinabanag92
Participant III
  • We have 20 odd Apps and 20 respective key-value pairs for that.
  • We are passing different apikeys on request header while calling a common API, from different apps.
  • For a particular app/apikey, corresponding Key-Value pair has to be selected inside this common API using KVM-Operations.

Please suggest how to achieve this.

Solved Solved
1 6 398
2 ACCEPTED SOLUTIONS

babuk0930
Participant III

1. While storing KVM, make sure key name as the app name.

2. Retrieve app name from variables after key verification

3. Then get the kvm value based on the appName as key name is same as appname

View solution in original post

abhinabanag92
Participant III

My requirement was selecting different key-value based on app name.

I am using Javascript policy and inside I'm getting the app name,

and based on that, conditionally I am setting the key name.
Below is the Javascript code:

 var appName = context.getVariable("verifyapikey.Verify-API-Key-1.developer.app.name");
if(appName=="firstapp")
{
    context.setVariable("request.header.myKey", "firstkey");
}
else if(appName=="testapp")
{
    context.setVariable("request.header.myKey", "testkey");
}

Below is the KVM code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="KVM-GetKey" mapIdentifier="somemap">
    <DisplayName>KVM-GetKey</DisplayName>
    <Properties/>
    <Get assignTo="test.key">
        <Key>
            <Parameter ref="request.header.myKey"/>
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>

View solution in original post

6 REPLIES 6

babuk0930
Participant III

1. While storing KVM, make sure key name as the app name.

2. Retrieve app name from variables after key verification

3. Then get the kvm value based on the appName as key name is same as appname

We cannot keep same key name as app name, as per business requirement here. But, it's a good suggestion. Thank you!

Are you asking how to configure the KeyValueMapOperations policy to retrieve a thing based on the apikey? The policy documentation shows you how to configure it.

<KeyValueMapOperations name="KVM-1" mapIdentifier="map1">
   <Scope>environment</Scope>
   <Get assignTo="whatever">
      <Key>
         <Parameter>apikey</Parameter>
         <Parameter ref="client_id"/> 
      </Key>
   </Get>
</KeyValueMapOperations> 
<br>

The "key" is created by concatenating the key parameters with double underscore. So if your client_id is ABDCDEFG, then the resulting key for the above policy is apikey__ABCDEFG .

This example assumes the "client_id" variable will have been populated by a VerifyApiKey that precedes this KVM policy.

abhinabanag92
Participant III

My requirement was selecting different key-value based on app name.

I am using Javascript policy and inside I'm getting the app name,

and based on that, conditionally I am setting the key name.
Below is the Javascript code:

 var appName = context.getVariable("verifyapikey.Verify-API-Key-1.developer.app.name");
if(appName=="firstapp")
{
    context.setVariable("request.header.myKey", "firstkey");
}
else if(appName=="testapp")
{
    context.setVariable("request.header.myKey", "testkey");
}

Below is the KVM code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="KVM-GetKey" mapIdentifier="somemap">
    <DisplayName>KVM-GetKey</DisplayName>
    <Properties/>
    <Get assignTo="test.key">
        <Key>
            <Parameter ref="request.header.myKey"/>
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>

Here you need to hardcode 20 app names in ur code?

Why can’t make it dynamic based on app name

That's a good suggestion, but, we cannot keep same key name as app name, as per business requirement here.