Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
DebashishDas
Active Participant
501

In Previous Part-1 we configured the destination through which we will access feature flags. Now we will develop an application. We could use a Simple NodeJs application to access BTP instance, but again thought of developing using sap cloud sdk as described by SAP.

How to develop a simple nodeJS application, you can follow one of my existing blog posts or any other out there in internet.

Run NodeJs in SAP BTP and Locally, Part - 1

(** Note: The blog was written earlier, you may have to adjust the npm packages and little modifications in the code.)

 Let’s Start with some command to install packages –

Setting Up the Project

1. Install NestJS: (documentation on NestJS - link )

 

npm i -g @nestjs/cli

 

2Scaffold an Application:

 

nest new featureflagsapcloudsdk
cd featureflagsapcloudsdk

 

3. Install OpenAPI Generator:

 

npm install -D -cloud-sdk/openapi-generator

 

4. Download and Set Up FeatureFlagsAPI.yaml:

                       DebashishDas_1-1738757949656.png

5. Create a file options-per-service.json and store in the same folder ‘resources/service-specs’.

 

{
  "resources/service-specs/FeatureFlagsAPI.yaml": {}
}

 

6. Install SAP Cloud SDK OpenAPI Package:

 

npm i -cloud-sdk/openapi

 

7. Generate Typed Client:

 

npx openapi-generator --input resources/service-specs --outputDir src/generated

 

This will generate the below like folders and files –

                               DebashishDas_2-1738757949657.png

All the calls or routing will be navigating from app.controller.ts file. These files will be generated by-default.

                           DebashishDas_3-1738757949658.png

8. Implementing the Application

Modify app.controller.ts and app.service.ts to include methods for accessing the feature flag.

These applications are based on typescript which generated by sap cloud sdk, but for the demo purposes we are avoiding the types. Mostly we are providing <any> as assigning and return type.

app.controller.ts:

DebashishDas_4-1738757949659.png

app.service.ts:

Add “import { EvaluateV2Api } from './generated/FeatureFlagsAPI';”

DebashishDas_5-1738757949660.png

9. Deploying the Application

Now let’s create a manifest file to deploy and bind with other resources –

Destination Featureflagdest already

** I am not very much worried about the authentication here now. So only using the Destination service.

manifest.yaml will look like below –

DebashishDas_6-1738757949661.png

Before deploying use the below Command, which is must each time you make any changes to any files.

 

nest build

 

Now deploy manifest file to BTP Cloud foundry using Command

 

cf push

 

DebashishDas_7-1738757949662.png

10. Run the Application

In BTP, Application is in running state and started.

DebashishDas_8-1738757949664.png

Execute the module with the path /feature-flag

Here is the output when flag is set to true

DebashishDas_9-1738757949665.png

DebashishDas_10-1738757949666.png

When the flag is false

DebashishDas_11-1738757949666.png

DebashishDas_12-1738757949667.png

Code Snippet –

app.controller.ts

 

import { Controller, Get, HttpException } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
  constructor(private readonly appService: AppService) { }
  @Get('/feature-flag')
  getFeatureFlag(): any {    
    return this.appService.getFlag()
    .then(response => {
      return response;
    })
    .catch(error => {
      throw new HttpException(
        `Failed to get Feature Flags - ${error.message}`,
        500
      )
    })
  }
}

 

app.service.ts

 

import { Injectable, Get ,HttpException } from '@nestjs/common';
import { EvaluateV2Api } from './generated/FeatureFlagsAPI';
@Injectable()
export class AppService {
  async getFlag(): Promise<any> {
    return EvaluateV2Api
          .getV2EvaluateById('<feature_flag_name>', { identifier: '<tenant_id>'})
          .setBasePath('https://feature-flags.cfapps.<region>.hana.ondemand.com/api')
          .execute({ destinationName: '<destination_name>' });
  }
}

 

manifest.yaml

 

applications:
  - name: featureflagsdk  
    path: .
    buildpacks:
      - nodejs_buildpack
    memory: 256M
    command: npm run start:prod
    random-route: true
    services:
      - featureflagdest

 

Conclusion: 

By following this posts, you will get the guidance of how to access the BTP Feature flags and use in your different scenarios.

If you have any questions or faced challenges, feel free to drop a comment below.

 

References –

  1. https://help.sap.com/docs/feature-flags-service/sap-feature-flags-service/what-is-sap-feature-flags-...
  2. https://api.sap.com/api/FeatureFlagsAPI/overview
  3. https://sap.github.io/cloud-sdk/
Labels in this area