Skip to content

Commit

Permalink
Python provider in the monorepo (#1236)
Browse files Browse the repository at this point in the history
* Add python provider to the mono repo

Signed-off-by: Thomas Poignant <[email protected]>

* Add Python SDK in doc

Signed-off-by: Thomas Poignant <[email protected]>

* Add how to install kotlin sdk

Signed-off-by: Thomas Poignant <[email protected]>

* test ci

Signed-off-by: Thomas Poignant <[email protected]>

* Fix sonar

Signed-off-by: Thomas Poignant <[email protected]>

* Add CI setup

Signed-off-by: Thomas Poignant <[email protected]>

* Fix pydantic issue

Signed-off-by: Thomas Poignant <[email protected]>

* Remove print

Signed-off-by: Thomas Poignant <[email protected]>

* lint

Signed-off-by: Thomas Poignant <[email protected]>

* add lint stage

Signed-off-by: Thomas Poignant <[email protected]>

---------

Signed-off-by: Thomas Poignant <[email protected]>
  • Loading branch information
thomaspoignant authored Nov 6, 2023
1 parent 756e14d commit 04e7d7d
Show file tree
Hide file tree
Showing 33 changed files with 1,654 additions and 5 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,31 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}
comment-on-alert: true
alert-comment-cc-users: "@$GITHUB_ACTOR"

Python-provider:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Get changed files in the docs folder
id: changed-files-specific
uses: tj-actions/changed-files@v40
with:
files: ./openfeature/providers/python-provider/**
- uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Setup Poetry
if: steps.changed-files-specific.outputs.any_changed == 'true'
uses: abatilo/actions-poetry@v2
- name: Poetry install
if: steps.changed-files-specific.outputs.any_changed == 'true'
working-directory: ./openfeature/providers/python-provider
run: poetry install
- name: lint black
if: steps.changed-files-specific.outputs.any_changed == 'true'
working-directory: ./openfeature/providers/python-provider
run: poetry run black . --check
- name: Pytest
if: steps.changed-files-specific.outputs.any_changed == 'true'
working-directory: ./openfeature/providers/python-provider
run: poetry run pytest
2 changes: 1 addition & 1 deletion .github/workflows/release-kotlin-provider.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Release Kotlin SDK to Sonatype
name: Release Kotlin Provider to Sonatype
on:
push:
tags:
Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/release-python-provider.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Release Python provider to PyPi
on:
push:
tags:
- python-provider-v*

jobs:
python-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Extract version
run: |
FULL_TAG_NAME=${{ github.ref_name }}
VERSION=${FULL_TAG_NAME:17}
echo "version=$VERSION" >> $GITHUB_ENV
- name: Edit Toml
uses: ciiiii/[email protected]
with:
file: "openfeature/providers/python-provider/pyproject.toml"
key: "tool.poetry.version"
value: ${{ env.version }}

- name: Build and publish to PyPi
uses: JRubics/[email protected]
with:
package_directory: ./openfeature/providers/python-provider
pypi_token: ${{ secrets.PYPI_TOKEN }}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ For now, we have providers for `python`, `java`, `typescript`, `javascript`, `GO
| Kotlin / Android | [Kotlin Provider](openfeature/providers/kotlin-provider) ![WIP](https://img.shields.io/badge/status-beta-red.svg) |
| Javascript/Typescript | [Server provider](https://github.com/open-feature/js-sdk-contrib/tree/main/libs/providers/go-feature-flag) / [Client provider](https://github.com/open-feature/js-sdk-contrib/tree/main/libs/providers/go-feature-flag-web) |
| PHP | Not currently available [help by contributing here](https://github.com/open-feature/php-sdk-contrib/) |
| Python | [Python provider](https://github.com/thomaspoignant/gofeatureflag-python-provider) ![WIP](https://img.shields.io/badge/status-WIP-red.svg) |
| Python | [Python provider](https://github.com/thomaspoignant/gofeatureflag-python-provider) ![WIP](https://img.shields.io/badge/status-beta-red.svg) |
| .Net | [.Net Provider](https://github.com/open-feature/dotnet-sdk-contrib/tree/main/src/OpenFeature.Contrib.Providers.GOFeatureFlag) |
| Ruby | Not currently available [help by contributing here](https://github.com/open-feature/ruby-sdk-contrib) |

Expand Down
160 changes: 160 additions & 0 deletions openfeature/providers/python-provider/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
77 changes: 77 additions & 0 deletions openfeature/providers/python-provider/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# GO Feature Flag Python Provider

GO Feature Flag provider allows you to connect to your GO Feature Flag instance.

[GO Feature Flag](https://gofeatureflag.org) believes in simplicity and offers a simple and lightweight solution to use feature flags.
Our focus is to avoid any complex infrastructure work to use GO Feature Flag.

This is a complete feature flagging solution with the possibility to target only a group of users, use any types of flags, store your configuration in various location and advanced rollout functionality. You can also collect usage data of your flags and be notified of configuration changes.

# Python SDK usage

## Install dependencies

The first things we will do is install the **Open Feature SDK** and the **GO Feature Flag provider**.

```shell
pip install gofeatureflag-python-provider
```

## Initialize your Open Feature client

To evaluate the flags you need to have an Open Feature configured in you app.
This code block shows you how you can create a client that you can use in your application.

```python
from gofeatureflag_python_provider.provider import GoFeatureFlagProvider
from gofeatureflag_python_provider.options import GoFeatureFlagOptions
from openfeature import api
from openfeature.evaluation_context import EvaluationContext

// ...

goff_provider = GoFeatureFlagProvider(
options=GoFeatureFlagOptions(endpoint="https://gofeatureflag.org/")
)
api.set_provider(goff_provider)
client = api.get_client(name="test-client")
```

## Evaluate your flag

This code block explain how you can create an `EvaluationContext` and use it to evaluate your flag.


> In this example we are evaluating a `boolean` flag, but other types are available.
>
> **Refer to the [Open Feature documentation](https://docs.openfeature.dev/docs/reference/concepts/evaluation-api#basic-evaluation) to know more about it.**
```csharp
// Context of your flag evaluation.
// With GO Feature Flag you MUST have a targetingKey that is a unique identifier of the user.
evaluation_ctx = EvaluationContext(
targeting_key="d45e303a-38c2-11ed-a261-0242ac120002",
attributes={
"email": "[email protected]",
"firstname": "john",
"lastname": "doe",
"anonymous": False,
"professional": True,
"rate": 3.14,
"age": 30,
"company_info": {"name": "my_company", "size": 120},
"labels": ["pro", "beta"],
},
)

admin_flag = client.get_boolean_value(
flag_key=flag_key,
default_value=default_value,
evaluation_context=ctx,
)

if admin_flag:
# flag "flag-only-for-admin" is true for the user
else:
# flag "flag-only-for-admin" is false for the user
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from dataclasses import dataclass

from openfeature.provider.metadata import Metadata


@dataclass
class GoFeatureFlagMetadata(Metadata):
name: str = "GO Feature Flag"
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import urllib3
import typing
from pydantic import AnyHttpUrl, BaseModel as PydanticBaseModel, ConfigDict


class BaseModel(PydanticBaseModel):
model_config: ConfigDict = ConfigDict({"arbitrary_types_allowed": True})


class GoFeatureFlagOptions(BaseModel):
endpoint: AnyHttpUrl
urllib3_pool_manager: typing.Optional[urllib3.PoolManager] = None
Loading

0 comments on commit 04e7d7d

Please sign in to comment.