Skip to content

Commit 706fbfd

Browse files
authored
feat: Add initial implementation (#1)
1 parent 3999b1a commit 706fbfd

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed

.circleci/config.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
version: 2.1
2+
3+
workflows:
4+
test:
5+
jobs:
6+
- build-run-linux:
7+
context: hello-world-demos
8+
9+
test-daily:
10+
triggers:
11+
- schedule:
12+
cron: "0 6 * * *"
13+
filters:
14+
branches:
15+
only: main
16+
jobs:
17+
- build-run-linux:
18+
context: hello-world-demos
19+
20+
# This CI build ensures that the demo both compiles and works correctly. For the runtime test,
21+
# we use an SDK key and flag key that are passed in via the CircleCI project configuration;
22+
# the flag is configured to return a true value.
23+
24+
jobs:
25+
build-run-linux:
26+
docker:
27+
- image: python:3.8
28+
steps:
29+
- checkout
30+
- run:
31+
name: Install poetry
32+
command: pipx install poetry
33+
34+
- run:
35+
name: Install dependencies
36+
command: poetry install
37+
38+
- run:
39+
name: Run hello
40+
command: |
41+
export LAUNCHDARKLY_SERVER_SDK="${LD_HELLO_WORLD_SDK_KEY}"
42+
export LAUNCHDARKLY_FLAG_KEY="${LD_HELLO_WORLD_FLAG_KEY_WITH_TRUE_VALUE}"
43+
poetry run python main.py | tee output.txt
44+
45+
- run:
46+
name: Check output
47+
command: |
48+
grep "is True for this context" output.txt || (echo "Flag did not evaluate to expected true value" && exit 1)

.gitignore

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
env/
12+
build/
13+
!.github/actions/build/
14+
develop-eggs/
15+
dist/
16+
downloads/
17+
eggs/
18+
.eggs/
19+
lib/
20+
lib64/
21+
parts/
22+
sdist/
23+
var/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*,cover
47+
.hypothesis/
48+
.pytest_cache
49+
50+
# Translations
51+
*.mo
52+
*.pot
53+
54+
# Django stuff:
55+
*.log
56+
57+
# Sphinx documentation
58+
docs/_build/
59+
60+
# PyBuilder
61+
target/
62+
63+
64+
65+
venv
66+
p2venv
67+
.idea
68+
*.iml
69+
.vagrant
70+
test-packaging-venv
71+
72+
.vscode/
73+
.python-version
74+
75+
# Poetry
76+
poetry.lock

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# LaunchDarkly Sample OpenFeature Python Server application
2+
3+
[![CircleCI](https://dl.circleci.com/status-badge/img/gh/launchdarkly/hello-openfeature-python-server/tree/main.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/launchdarkly/hello-openfeature-python-server/tree/main)
4+
5+
We've built a simple console script that demonstrates how LaunchDarkly's OpenFeature provider works.
6+
7+
## Build instructions
8+
9+
1. Install the project dependencies by running `poetry install`
10+
2. Set the environment variable `LAUNCHDARKLY_SERVER_SDK` to your LaunchDarkly SDK key.
11+
3. Set the environment variable `LAUNCHDARKLY_FLAG_KEY` to the LaunchDarkly boolean flag key you wish to evaluate.
12+
4. Run `poetry run python main.py`.
13+
14+
You should see the message `"Feature flag '<flag key>' is <True/False> for this context"`.

main.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from openfeature import api
2+
from openfeature.evaluation_context import EvaluationContext
3+
from ldclient import Config
4+
from ld_openfeature import LaunchDarklyProvider
5+
from os import getenv
6+
7+
sdk_key = getenv("LAUNCHDARKLY_SERVER_SDK", "")
8+
flag_key = getenv("LAUNCHDARKLY_FLAG_KEY", "")
9+
10+
if sdk_key == "":
11+
print("*** Set the 'LAUNCHDARKLY_SERVER_SDK' environment variable before running this script")
12+
exit(1)
13+
elif flag_key == "":
14+
print("*** Set the 'LAUNCHDARKLY_FLAG_KEY' environment variable before running this script")
15+
exit(1)
16+
17+
provider = LaunchDarklyProvider(Config(sdk_key))
18+
19+
api.set_provider(provider)
20+
client = api.get_client()
21+
22+
# Set up the evaluation context. This context should appear on your LaunchDarkly
23+
# contexts dashboard soon after you run the demo.
24+
context = EvaluationContext("example-user-key", {"name": "Sandy"})
25+
26+
flag_value = client.get_boolean_value(flag_key, False, context)
27+
print(f"*** Feature flag '{flag_key}' is {flag_value} for this context")

pyproject.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[tool.poetry]
2+
name = "hello-openfeature-python-server"
3+
version = "0.1.0"
4+
description = ""
5+
authors = ["LaunchDarkly <[email protected]>"]
6+
readme = "README.md"
7+
8+
[tool.poetry.dependencies]
9+
python = "^3.8"
10+
launchdarkly-openfeature-server = "^0.1.0"
11+
12+
13+
[build-system]
14+
requires = ["poetry-core"]
15+
build-backend = "poetry.core.masonry.api"

0 commit comments

Comments
 (0)