-
Notifications
You must be signed in to change notification settings - Fork 19
feat: add flagd provider #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
bee90ed
feat: add flagd provider
agardnerIT 6521142
test: include fixture for flagd provider
tcarrio 24929f1
style: apply styling, consolidate line length config for isort/flake8…
tcarrio 2213ebf
feat: refactor magic numbers and constants
tcarrio 7f0d8de
chore: update all dependencies with pip-compile
tcarrio 968e04c
feat: further work on FlagdProvider
tcarrio f5fcb4a
build: add pyproject.toml from python-sdk
tcarrio 6f164b6
feat: implemented OpenFeature flagd provider refactor
tcarrio ecc8376
test: implemented tests for flagd provider
tcarrio 8ba6dca
chore: consistent naming to Flagd
tcarrio 038663b
feat: flag types support for all OpenFeature types and dynamic resolu…
tcarrio 559b0e5
refactor: test cases separation of given/when/then bdd cases
tcarrio 30f51d8
style: apply formatting rules
tcarrio 97afe85
chore: remove placeholder test case
federicobond f3a396f
style: replace dict constructor calls with dict literals
federicobond b3defb0
fix: use correct type for default_value in get_object_details
federicobond File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -47,4 +47,7 @@ coverage.xml | |
*.pot | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
docs/_build/ | ||
|
||
# Virtual env directories | ||
.venv |
This file contains hidden or 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,2 @@ | ||
[settings] | ||
line_length=88 |
This file contains hidden or 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,3 @@ | ||
from .provider import FlagdProvider | ||
|
||
FlagdProvider | ||
This file contains hidden or 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,5 @@ | ||
class Defaults: | ||
HOST = "localhost" | ||
PORT = 8013 | ||
SCHEMA = "http" | ||
TIMEOUT = 2 # seconds |
18 changes: 18 additions & 0 deletions
18
open_feature_contrib/providers/flagd/evaluation_context_serializer.py
This file contains hidden or 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,18 @@ | ||
import typing | ||
|
||
from open_feature.evaluation_context.evaluation_context import EvaluationContext | ||
|
||
|
||
class EvaluationContextSerializer: | ||
federicobond marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def to_dict(ctx: typing.Optional[EvaluationContext]): | ||
return ( | ||
{ | ||
**ctx.attributes, | ||
**(EvaluationContextSerializer.__extract_key(ctx)), | ||
} | ||
if ctx | ||
else {} | ||
) | ||
|
||
def __extract_key(ctx: EvaluationContext): | ||
return {"targetingKey": ctx.targeting_key} if ctx.targeting_key is str else {} |
This file contains hidden or 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,9 @@ | ||
from enum import Enum | ||
|
||
|
||
class FlagType(Enum): | ||
BOOLEAN = "BOOLEAN" | ||
STRING = "STRING" | ||
FLOAT = "FLOAT" | ||
INTEGER = "INTEGER" | ||
OBJECT = "OBJECT" | ||
federicobond marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,186 @@ | ||
""" | ||
# This is a Python Provider to interact with flagd | ||
# | ||
# -- Usage -- | ||
# open_feature_api.set_provider(flagd_provider.FlagdProvider()) | ||
# flag_value = open_feature_client.get_string_value( | ||
# key="foo", | ||
# default_value="missingflag" | ||
# ) | ||
# print(f"Flag Value is: {flag_value}") | ||
# OR the more verbose option | ||
# flag = open_feature_client.get_string_details(key="foo", default_value="missingflag") | ||
# print(f"Flag is: {flag.value}") | ||
# OR | ||
# print(f"Flag Details: {vars(flag)}"") | ||
# | ||
# -- Customisation -- | ||
# Follows flagd defaults: 'http' protocol on 'localhost' on port '8013' | ||
# But can be overridden: | ||
# provider = open_feature_api.get_provider() | ||
# provider.initialise(schema="https",endpoint="example.com",port=1234,timeout=10) | ||
""" | ||
|
||
import typing | ||
from numbers import Number | ||
|
||
import requests | ||
from open_feature.evaluation_context.evaluation_context import EvaluationContext | ||
from open_feature.exception.error_code import ErrorCode | ||
from open_feature.flag_evaluation.flag_evaluation_details import FlagEvaluationDetails | ||
from open_feature.provider.provider import AbstractProvider | ||
|
||
from .defaults import Defaults | ||
from .evaluation_context_serializer import EvaluationContextSerializer | ||
from .flag_type import FlagType | ||
from .web_api_url_factory import WebApiUrlFactory | ||
|
||
|
||
class FlagdProvider(AbstractProvider): | ||
"""Flagd OpenFeature Provider""" | ||
|
||
def __init__( | ||
self, | ||
name: str = "flagd", | ||
schema: str = Defaults.SCHEMA, | ||
host: str = Defaults.HOST, | ||
port: int = Defaults.PORT, | ||
timeout: int = Defaults.TIMEOUT, | ||
): | ||
""" | ||
Create an instance of the FlagdProvider | ||
|
||
:param name: the name of the provider to be stored in metadata | ||
:param schema: the schema for the transport protocol, e.g. 'http', 'https' | ||
:param host: the host to make requests to | ||
:param port: the port the flagd service is available on | ||
:param timeout: the maximum to wait before a request times out | ||
""" | ||
self.provider_name = name | ||
self.schema = schema | ||
self.host = host | ||
self.port = port | ||
self.timeout = timeout | ||
|
||
self.url_factory = WebApiUrlFactory(self.schema, self.host, self.port) | ||
|
||
def get_metadata(self): | ||
"""Returns provider metadata""" | ||
return { | ||
"name": self.get_name(), | ||
"schema": self.schema, | ||
"host": self.host, | ||
"port": self.port, | ||
"timeout": self.timeout, | ||
} | ||
federicobond marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def get_name(self) -> str: | ||
"""Returns provider name""" | ||
return self.provider_name | ||
|
||
def get_boolean_details( | ||
self, | ||
key: str, | ||
default_value: bool, | ||
evaluation_context: EvaluationContext = None, | ||
): | ||
return self.__resolve(key, FlagType.BOOLEAN, default_value, evaluation_context) | ||
|
||
def get_string_details( | ||
self, | ||
key: str, | ||
default_value: str, | ||
evaluation_context: EvaluationContext = None, | ||
): | ||
return self.__resolve(key, FlagType.STRING, default_value, evaluation_context) | ||
|
||
def get_float_details( | ||
self, | ||
key: str, | ||
default_value: Number, | ||
evaluation_context: EvaluationContext = None, | ||
): | ||
return self.__resolve(key, FlagType.FLOAT, default_value, evaluation_context) | ||
|
||
def get_int_details( | ||
self, | ||
key: str, | ||
default_value: Number, | ||
evaluation_context: EvaluationContext = None, | ||
): | ||
return self.__resolve(key, FlagType.INTEGER, default_value, evaluation_context) | ||
|
||
def get_object_details( | ||
self, | ||
key: str, | ||
default_value: typing.Union[dict, list], | ||
evaluation_context: EvaluationContext = None, | ||
): | ||
return self.__resolve(key, FlagType.OBJECT, default_value, evaluation_context) | ||
|
||
def __resolve( | ||
self, | ||
flag_key: str, | ||
flag_type: FlagType, | ||
default_value: typing.Any, | ||
evaluation_context: EvaluationContext, | ||
): | ||
""" | ||
This method is equivalent to: | ||
curl -X POST http://localhost:8013/{path} \ | ||
-H "Content-Type: application/json" \ | ||
-d '{"flagKey": key, "context": evaluation_context}' | ||
""" | ||
|
||
payload = { | ||
"flagKey": flag_key, | ||
"context": EvaluationContextSerializer.to_dict(evaluation_context), | ||
} | ||
|
||
try: | ||
url_endpoint = self.url_factory.get_path_for(flag_type) | ||
|
||
response = requests.post( | ||
federicobond marked this conversation as resolved.
Show resolved
Hide resolved
|
||
url=url_endpoint, timeout=self.timeout, json=payload | ||
) | ||
|
||
except Exception: | ||
federicobond marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Perhaps a timeout? Return the default as an error. | ||
# The return above and this are separate because in the case of a timeout, | ||
# the JSON is not available | ||
# So return a stock, generic answer. | ||
|
||
return FlagEvaluationDetails( | ||
flag_key=flag_key, | ||
value=default_value, | ||
reason=ErrorCode.PROVIDER_NOT_READY, | ||
variant=default_value, | ||
) | ||
|
||
json_content = response.json() | ||
federicobond marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# If lookup worked (200 response) get flag (or empty) | ||
# This is the "ideal" case. | ||
if response.status_code == 200: | ||
|
||
# Got a valid flag and valid type. Return it. | ||
if "value" in json_content: | ||
# Got a valid flag value for key: {key} of: {json_content['value']} | ||
return FlagEvaluationDetails( | ||
flag_key=flag_key, | ||
value=json_content["value"], | ||
reason=json_content["reason"], | ||
variant=json_content["variant"], | ||
) | ||
|
||
# Otherwise HTTP call worked | ||
# However, flag either doesn't exist or doesn't match the type | ||
# eg. Expecting a string but this value is a boolean. | ||
# Return whatever we got from the backend. | ||
return FlagEvaluationDetails( | ||
flag_key=flag_key, | ||
value=default_value, | ||
reason=json_content["code"], | ||
variant=default_value, | ||
error_code=json_content["message"], | ||
) |
50 changes: 50 additions & 0 deletions
50
open_feature_contrib/providers/flagd/web_api_url_factory.py
This file contains hidden or 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,50 @@ | ||
from .flag_type import FlagType | ||
|
||
|
||
class WebApiUrlFactory: | ||
BOOLEAN = "schema.v1.Service/ResolveBoolean" | ||
STRING = "schema.v1.Service/ResolveString" | ||
FLOAT = "schema.v1.Service/ResolveFloat" | ||
INTEGER = "schema.v1.Service/ResolveInteger" | ||
OBJECT = "schema.v1.Service/ResolveObject" | ||
|
||
# provides dynamic dictionary-based resolution by flag type | ||
__mapping = { | ||
FlagType.BOOLEAN: "get_boolean_path", | ||
FlagType.STRING: "get_string_path", | ||
FlagType.FLOAT: "get_float_path", | ||
FlagType.INTEGER: "get_integer_path", | ||
FlagType.OBJECT: "get_object_path", | ||
} | ||
__default_mapping_key = "_invalid_flag_type_method" | ||
|
||
def __init__(self, schema, host, port): | ||
self.root = f"{schema}://{host}:{port}" | ||
|
||
def get_boolean_path(self): | ||
return self._format_url(self.BOOLEAN) | ||
|
||
def get_string_path(self): | ||
return self._format_url(self.STRING) | ||
|
||
def get_float_path(self): | ||
return self._format_url(self.FLOAT) | ||
|
||
def get_integer_path(self): | ||
return self._format_url(self.INTEGER) | ||
|
||
def get_object_path(self): | ||
return self._format_url(self.OBJECT) | ||
|
||
def get_path_for(self, flag_type: FlagType): | ||
return self[ | ||
WebApiUrlFactory.__mapping.get( | ||
flag_type, WebApiUrlFactory.__default_mapping_key | ||
) | ||
]() | ||
|
||
def _format_url(self, path: str): | ||
return f"{self.root}/{path}" | ||
|
||
def _invalid_flag_type_method(self): | ||
raise Exception("Invalid flag type passed to factory") |
This file contains hidden or 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,30 @@ | ||
# pyproject.toml | ||
[build-system] | ||
requires = ["setuptools>=61.0.0", "wheel"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
[project] | ||
name = "openfeature_sdk_contrib" | ||
version = "0.1.0" | ||
description = "Contributions around the Python OpenFeature SDK, such as providers and hooks" | ||
readme = "readme.md" | ||
authors = [{ name = "OpenFeature", email = "[email protected]" }] | ||
license = { file = "LICENSE" } | ||
classifiers = [ | ||
"License :: OSI Approved :: Apache Software License", | ||
"Programming Language :: Python", | ||
"Programming Language :: Python :: 3", | ||
] | ||
keywords = [] | ||
dependencies = [] | ||
requires-python = ">=3.8" | ||
|
||
[project.optional-dependencies] | ||
dev = ["black", "flake8", "isort", "pip-tools", "pytest", "pre-commit"] | ||
|
||
[project.urls] | ||
Homepage = "https://github.com/open-feature/python-sdk-contrib" | ||
|
||
[tool.isort] | ||
profile = "black" | ||
multi_line_output = 3 |
This file contains hidden or 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 |
---|---|---|
|
@@ -7,3 +7,4 @@ pre-commit | |
flake8 | ||
pytest-mock | ||
coverage | ||
openfeature-sdk |
This file contains hidden or 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 hidden or 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 @@ | ||
openfeature-sdk |
This file contains hidden or 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 @@ | ||
# | ||
# This file is autogenerated by pip-compile with python 3.10 | ||
# To update, run: | ||
# | ||
# pip-compile --output-file=./requirements.txt ./requirements.in | ||
# | ||
openfeature-sdk==0.0.9 | ||
# via -r ./requirements.in |
Empty file.
This file contains hidden or 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,10 @@ | ||
import pytest | ||
from open_feature import open_feature_api as api | ||
|
||
from open_feature_contrib.providers.flagd import FlagdProvider | ||
|
||
|
||
@pytest.fixture() | ||
def flagd_provider_client(): | ||
api.set_provider(FlagdProvider()) | ||
return api.get_client() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.