Skip to content

Commit

Permalink
Merge pull request #16 from flagship-io/feat/SDK-V4
Browse files Browse the repository at this point in the history
Feat/sdk v4
  • Loading branch information
Chadiii authored Oct 15, 2024
2 parents be652c0 + d6c28c5 commit db36ff0
Show file tree
Hide file tree
Showing 18 changed files with 673 additions and 55 deletions.
10 changes: 10 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FS_API="https://api.flagship.io"
FS_AUTH_API="https://auth.flagship.io"
REPOSITORY_BRANCH="master"
DIRECTORY="path/to/directory"
REPOSITORY_URL="https://gitlab.com/org/repo"
NB_CODE_LINES_EDGES=1
FLAGSHIP_CLIENT_ID=FLAGSHIP_MANAGEMENT_API_CLIENT_ID
FLAGSHIP_CLIENT_SECRET=FLAGSHIP_MANAGEMENT_API_CLIENT_SECRET
ACCOUNT_ID=FLAGSHIP_ACCOUNT_ID
ENVIRONMENT_ID=FLAGSHIP_ENVIRONMENT_ID
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
example/src-js
.env
coverage
codebase-analyzer
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export ENVIRONMENT_ID=FLAGSHIP_ENVIRONMENT_ID
export REPOSITORY_URL=https://gitlab.com/org/repo
export REPOSITORY_BRANCH=master
export DIRECTORY=./
./code-analyzer
./codebase-analyzer
```

### With Docker
Expand All @@ -41,6 +41,7 @@ docker run -v $(pwd)/your_repo:/your_repo -e FLAGSHIP_CLIENT_ID=FLAGSHIP_MANAGEM
```

### With Homebrew

```sh
export FLAGSHIP_CLIENT_ID=FLAGSHIP_MANAGEMENT_API_CLIENT_ID
export FLAGSHIP_CLIENT_SECRET=FLAGSHIP_MANAGEMENT_API_CLIENT_SECRET
Expand All @@ -57,6 +58,7 @@ codebase-analyzer
```

### Supported file languages

- .cs .fs
- .dart
- .go
Expand Down Expand Up @@ -140,7 +142,7 @@ analyze_flag_references:
ACCOUNT_ID: YOUR_ACCOUNT_ID
ENVIRONMENT_ID: YOUR_ENVIRONMENT_ID
script:
- /root/code-analyser
- /root/codebase-analyser
only:
- master
```
Expand All @@ -159,13 +161,12 @@ This repository needs go v1.13+ to work
2. Create a .env file to customize your environment variable
3. Run `go run *.go` in the example folder to run the code

### Test
### Test

```
make test
```
## Contributors
- Guillaume Jacquart [@GuillaumeJacquart](https://github.com/guillaumejacquart)
Expand All @@ -178,23 +179,25 @@ make test
[Apache License.](https://github.com/flagship-io/codebase-analyzer/blob/master/LICENSE)
## About Flagship
<img src="https://www.flagship.io/wp-content/uploads/Flagship-horizontal-black-wake-AB.png" alt="drawing" width="150"/>
[Flagship by AB Tasty](https://www.flagship.io/) is a feature flagging platform for modern engineering and product teams. It eliminates the risks of future releases by separating code deployments from these releases :bulb: With Flagship, you have full control over the release process. You can:
- Switch features on or off through remote config.
- Automatically roll-out your features gradually to monitor performance and gather feedback from your most relevant users.
- Roll back any feature should any issues arise while testing in production.
- Segment users by granting access to a feature based on certain user attributes.
- Carry out A/B tests by easily assigning feature variations to groups of users.
<img src="https://www.flagship.io/wp-content/uploads/demo-setup.png" alt="drawing" width="600"/>
Flagship also allows you to choose whatever implementation method works for you from our many available SDKs or directly through a REST API. Additionally, our architecture is based on multi-cloud providers that offer high performance and highly-scalable managed services.
**To learn more:**
<img src="https://www.flagship.io/wp-content/uploads/demo-setup.png" alt="drawing" width="600"/>
Flagship also allows you to choose whatever implementation method works for you from our many available SDKs or directly through a REST API. Additionally, our architecture is based on multi-cloud providers that offer high performance and highly-scalable managed services.
**To learn more:**
- [Solution overview](https://www.flagship.io/#showvideo) - A 5mn video demo :movie_camera:
- [Documentation](https://docs.developers.flagship.io/) - Our dev portal with guides, how tos, API and SDK references
- [Sign up for a free trial](https://www.flagship.io/sign-up/) - Create your free account
Expand Down
File renamed without changes.
18 changes: 18 additions & 0 deletions example/src/flutter/SDK_V4/sample.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:flagship/flagship.dart';

// Step 1 - Start the Flagship sdk with default configuration.
Flagship.start("_ENV_ID_", "_API_KEY_");

// Step 2 - Create visitor with context "isVip" : true
var visitor = Flagship.newVisitor(visitorId: "visitorId", hasConsented: true)
.withContext({"isVip": true}).build();

// Step 3 - Fetch flags
visitor.fetchFlags().whenComplete(() {
// Step 4 - Get Flag key
var flag = v.getFlag("displayVipFeature");
// Step 5 - Read Flag value
var value = flag.value(false);
var value1 = v.getFlag("backgroundColor").value("red");
var value1 = v.getFlag("backgroundSize").value(1);
});
30 changes: 30 additions & 0 deletions example/src/ios/SDK_V4/sample.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Flagship

// Step 1 - Start the Flagship sdk with default configuration.
Flagship.sharedInstance.start(envId: "_ENV_ID_", apiKey: "_API_KEY_")

// Step 2 - Create visitor with context "isVip" : true
let visitor = Flagship.sharedInstance.newVisitor(visitorId: "visitorId", hasConsented: true)
.withContext(context: ["isVip": true])
.build()

// Step 3 - Fetch flags
visitor.fetchFlags {

// Fetch completed

// Step 4 - Get Flag key
let flag = visitor.getFlag(key: "btnColor")

// Step 5 - Read Flag value
let value = flag.value(defaultValue: "red")

let value = visitor.getFlag(key: "displayVipFeature").value(defaultValue: false)

// Step 4 - Get Flag key
let flag2 = visitor.getFlag(key: "vipFeature")

// Step 5 - Read Flag value
let value = flag2.value(defaultValue: 16)

}
7 changes: 7 additions & 0 deletions example/src/java/SDK_V4/sample.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
val flagRank = visitor.getFlag("btnColor")
val flagRankValue = flagRank.value("red")

val flagRankValue2 = visitor.getFlag("backgroundSize").value(1)

val flagRank1 = visitor.getFlag("showBackground")
val flagRankValue = flagRank1.value(true)
75 changes: 75 additions & 0 deletions example/src/js/SDK_V4/sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//start demo
// Usage: node demo/index.js
const express = require("express");
const { Flagship, HitType, EventCategory } = require("@flagship.io/js-sdk");

const app = express();
app.use(express.json());

const visitorId = "visitor-id";

// Step 1: Start the Flagship SDK by providing the environment ID and API key
Flagship.start("<ENV_ID>", "<API_KEY>", {
fetchNow: false,
});

// Endpoint to get an item
app.get("/item", async (req, res) => {

// Step 2: Create a new visitor with a visitor ID and consent status
const visitor = Flagship.newVisitor({
visitorId,
hasConsented: true,
context: {
fs_is_vip: true,
},
});

// Step 3: Fetch the flags for the visitor
await visitor.fetchFlags();

// fe:flag:fs_disable_coupon, string
const fsDisableCoupon = visitor.getFlag("fs_disable_coupon");

// Step 4: Get the values of the flags for the visitor
const fsEnableDiscount = visitor.getFlag("fs_enable_discount");
const fsAddToCartBtnColor = visitor.getFlag("fs_add_to_cart_btn_color");

const fsEnableDiscountValue = fsEnableDiscount.getValue(false);
const fsAddToCartBtnColorValue = fsAddToCartBtnColor.getValue("blue");

res.json({
item: {
name: "Flagship T-shirt",
price: 20,
},
fsEnableDiscount: fsEnableDiscountValue,
fsAddToCartBtnColor: fsAddToCartBtnColorValue,
});
});

// Endpoint to add an item to the cart
app.post("/add-to-cart", async (req, res) => {

const visitor = Flagship.newVisitor({
visitorId,
hasConsented: true
});

// Step 5: Send a hit to track an action
visitor.sendHit({
type: HitType.EVENT,
category: EventCategory.ACTION_TRACKING,
action: "add-to-cart-clicked",
});

res.json(null);
});

const port = 3000;

app.listen(port, () => {
console.log(`Server running on port ${port}`);
});

//end demo
29 changes: 29 additions & 0 deletions example/src/js/SDK_V4/sample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Flagship } from "@flagship.io/js-sdk";

Flagship.start("your_env_id", "your_api_key");

const visitor = Flagship.newVisitor({
visitorId: "your_visitor_id",
context: { isVip: true },
});

visitor.fetchFlags();

const variableKey = visitor.getFlag("flagKey");
const boxSizeFlagDefaultValue = variableKey.getValue("flagDefaultValue");

const variableKey1: any = visitor.getFlag("flagKey1");

const variableKey3: any = visitor.getFlag("flagKey3");
const boxSizeFlagDefaultValue1 = variableKey3.getValue(16);

const variableKey4: any = visitor.getFlag("flagKey4");

const variableKey5: any = visitor.getFlag("flagKey5").getValue(false);

// fe:flag: flagKey6, true
const variable6: any = visitor.getFlag("flagKey6");

visitor.getFlag("flagKey5").getValue(false);

visitor.getFlagValue("FlagKey5", false);
24 changes: 24 additions & 0 deletions example/src/net/SDK_V4/sample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Flagship.Main;

//Step 2: Create a visitor
var visitor = Fs.NewVisitor("<VISITOR_ID>", true)
.SetContext(new Dictionary<string, object>(){["isVip"] = true})
.Build();


//Step 3: Fetch flag from the Flagship platform
await visitor.FetchFlags();

/* Step 4: Retrieves a flag named "displayVipFeature",
*/
var flag = visitor.GetFlag("showBtn");

//Step 5: get the flag value and if the flag does not exist, it returns the default value "false"
var flagValue = flag.GetValue(false);

var flag_ = visitor.GetFlag("btnSize").GetValue(15);

var flag1 = visitor.GetFlag("btnColor");
var flagValue = flag1.GetValue("red");

Console.WriteLine($"Flag {flagValue}");
20 changes: 20 additions & 0 deletions example/src/net/SDK_V4/sample.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
open System.Collections.Generic
open Flagship

let client = FlagshipBuilder.Start("ENV_ID","API_KEY");

let context = new Dictionary<string, obj>();
context.Add("key", "value");

let visitor = client.NewVisitor("visitor_id", context);

visitor.FetchFlags();

let btnColorFlag = visitor.GetFlag("btnColor");
let btnColorFlagValue = btnColorFlag.GetValue('red');

let flag = visitor.GetFlag("btnSize");
let flagValue = flag.GetValue(13);

let showBtnFlag = visitor.GetFlag("showBtn");
let showBtnFlagValue = showBtnFlag.GetValue(true);
25 changes: 25 additions & 0 deletions example/src/php/SDK_V4/sample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use Flagship\Flagship;

// Step 1: start the SDK
Flagship::start("<ENV_ID>", "<API_KEY>");

//Step 2: Create a visitor
$visitor = Flagship::newVisitor("<VISITOR_ID>", true)
->setContext(["isVip" => true])
->build();

//Step 3: Fetch flag from the Flagship platform
$visitor->fetchFlags();

//Step 4: Retrieves a flag named "displayVipFeature"
$flag = $visitor->getFlag("displayVipFeature");

//Step 5: Returns the flag value ,or if the flag does not exist, it returns the default value "false"
echo "flag value:". $flag->getValue(false);

$flag1 = $visitor->getFlag("vipFeatureSize")->getValue(15);

$flag1 = $visitor->getFlag("vipFeatureColor")->getValue("red");

//Step 6: Batch all the collected hits and send them
Flagship::close();
2 changes: 1 addition & 1 deletion example/src/react/SDK_V3/sample.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {useFsFlag} from "@flagship.io/react-sdk";
export const MyReactComponent = () => {
const backgroundColorFlag = useFsFlag("backgroundColor", "green")
const btnColorFlag = useFsFlag("btnColor", "red")
const backgroundSize = useFsFlag("backgroundColor", 16)
const backgroundSize = useFsFlag("backgroundSize", 16)
const showBtn = useFsFlag("showBtn", true)

return (
Expand Down
28 changes: 28 additions & 0 deletions example/src/react/SDK_V4/sample.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from "react";
import { useFsFlag } from "@flagship.io/react-sdk";

export const MyReactComponent = () => {
///Step 2: Retrieves a flag named "backgroundColor"
const flag = useFsFlag("backgroundColor")

//Step 3: Returns the value of the flag or if the flag does not exist, it returns the default value "green"
const flagValue = flag.getValue("green")


const flag_ = useFsFlag("btnSize").getValue(16)

const flag1 = useFsFlag("showBtn")
const flagValue_ = flag1.getValue(true)

return (
<button
style={{
height: "200px",
width: "200px",
backgroundColor: flagValue,
}}
>
{"I'm a square with color=" + flagValue}
</button>
);
};
Loading

0 comments on commit db36ff0

Please sign in to comment.