-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python provider in the monorepo (#1236)
* 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
1 parent
756e14d
commit 04e7d7d
Showing
33 changed files
with
1,654 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
1 change: 1 addition & 0 deletions
1
openfeature/providers/python-provider/gofeatureflag_python_provider/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__version__ = "0.1.0" |
8 changes: 8 additions & 0 deletions
8
openfeature/providers/python-provider/gofeatureflag_python_provider/metadata.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
12 changes: 12 additions & 0 deletions
12
openfeature/providers/python-provider/gofeatureflag_python_provider/options.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.