Skip to content
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

feat(python): add get_flag method #148

Merged
merged 5 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint-sdks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.7.0
version: 1.7.1

- name: Lint Python source
working-directory: flipt-python
Expand Down
4 changes: 2 additions & 2 deletions flipt-python/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
format:
poetry run ruff flipt tests --fix
poetry run ruff check flipt tests --fix
poetry run black flipt tests

lint:
poetry run ruff flipt tests
poetry run ruff check flipt tests
poetry run black flipt tests --check
poetry run mypy flipt
poetry run pytest --dead-fixtures
Expand Down
2 changes: 2 additions & 0 deletions flipt-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ There is a more detailed example in the [examples](./examples) directory.

After adding new code, please don't forget to add unit tests for new features.
To format the code, check it with linters and run tests, use the `make check` command.

Please keep the Python [PEP8](https://peps.python.org/pep-0008/) in mind while adding new code.
4 changes: 2 additions & 2 deletions flipt-python/flipt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
from .sync_client import FliptClient

__all__ = [
'FliptClient',
'AsyncFliptClient',
"FliptClient",
"AsyncFliptClient",
]
2 changes: 1 addition & 1 deletion flipt-python/flipt/authentication/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class AuthenticationStrategy:
def authenticate(self, headers: dict[str, str]) -> None:
raise NotImplementedError()
raise NotImplementedError


class ClientTokenAuthentication(AuthenticationStrategy):
Expand Down
24 changes: 12 additions & 12 deletions flipt-python/flipt/evaluation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
from .sync_evaluation_client import Evaluation

__all__ = [
'Evaluation',
'AsyncEvaluation',
'EvaluationResponseType',
'EvaluationReason',
'ErrorEvaluationReason',
'EvaluationRequest',
'BatchEvaluationRequest',
'VariantEvaluationResponse',
'BooleanEvaluationResponse',
'ErrorEvaluationResponse',
'EvaluationResponse',
'BatchEvaluationResponse',
"Evaluation",
"AsyncEvaluation",
"EvaluationResponseType",
"EvaluationReason",
"ErrorEvaluationReason",
"EvaluationRequest",
"BatchEvaluationRequest",
"VariantEvaluationResponse",
"BooleanEvaluationResponse",
"ErrorEvaluationResponse",
"EvaluationResponse",
"BatchEvaluationResponse",
]
29 changes: 12 additions & 17 deletions flipt-python/flipt/evaluation/async_evaluation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import httpx

from ..authentication import AuthenticationStrategy
from ..exceptions import FliptApiError
from flipt.authentication import AuthenticationStrategy
from flipt.exceptions import FliptApiError

from .models import (
BatchEvaluationRequest,
BatchEvaluationResponse,
Expand Down Expand Up @@ -31,18 +32,20 @@ def __init__(
async def close(self) -> None:
await self._client.aclose()

def _raise_on_error(self, response: httpx.Response) -> None:
if response.status_code != 200:
body = response.json()
message = body.get("message", HTTPStatus(response.status_code).description)
raise FliptApiError(message, response.status_code)

async def variant(self, request: EvaluationRequest) -> VariantEvaluationResponse:
response = await self._client.post(
f"{self.url}/evaluate/v1/variant",
headers=self.headers,
json=request.model_dump(),
)

if response.status_code != 200:
body = response.json()
message = body.get("message", HTTPStatus(response.status_code).description)
raise FliptApiError(message, response.status_code)

self._raise_on_error(response)
return VariantEvaluationResponse.model_validate_json(response.text)

async def boolean(self, request: EvaluationRequest) -> BooleanEvaluationResponse:
Expand All @@ -52,11 +55,7 @@ async def boolean(self, request: EvaluationRequest) -> BooleanEvaluationResponse
json=request.model_dump(),
)

if response.status_code != 200:
body = response.json()
message = body.get("message", HTTPStatus(response.status_code).description)
raise FliptApiError(message, response.status_code)

self._raise_on_error(response)
return BooleanEvaluationResponse.model_validate_json(response.text)

async def batch(self, request: BatchEvaluationRequest) -> BatchEvaluationResponse:
Expand All @@ -66,9 +65,5 @@ async def batch(self, request: BatchEvaluationRequest) -> BatchEvaluationRespons
json=request.model_dump(),
)

if response.status_code != 200:
body = response.json()
message = body.get("message", HTTPStatus(response.status_code).description)
raise FliptApiError(message, response.status_code)

self._raise_on_error(response)
return BatchEvaluationResponse.model_validate_json(response.text)
29 changes: 12 additions & 17 deletions flipt-python/flipt/evaluation/sync_evaluation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import httpx

from ..authentication import AuthenticationStrategy
from ..exceptions import FliptApiError
from flipt.authentication import AuthenticationStrategy
from flipt.exceptions import FliptApiError

from .models import (
BatchEvaluationRequest,
BatchEvaluationResponse,
Expand Down Expand Up @@ -31,18 +32,20 @@ def __init__(
def close(self) -> None:
self._client.close()

def _raise_on_error(self, response: httpx.Response) -> None:
if response.status_code != 200:
body = response.json()
message = body.get("message", HTTPStatus(response.status_code).description)
raise FliptApiError(message, response.status_code)

def variant(self, request: EvaluationRequest) -> VariantEvaluationResponse:
response = self._client.post(
f"{self.url}/evaluate/v1/variant",
headers=self.headers,
json=request.model_dump(),
)

if response.status_code != 200:
body = response.json()
message = body.get("message", HTTPStatus(response.status_code).description)
raise FliptApiError(message, response.status_code)

self._raise_on_error(response)
return VariantEvaluationResponse.model_validate_json(response.text)

def boolean(self, request: EvaluationRequest) -> BooleanEvaluationResponse:
Expand All @@ -52,11 +55,7 @@ def boolean(self, request: EvaluationRequest) -> BooleanEvaluationResponse:
json=request.model_dump(),
)

if response.status_code != 200:
body = response.json()
message = body.get("message", HTTPStatus(response.status_code).description)
raise FliptApiError(message, response.status_code)

self._raise_on_error(response)
return BooleanEvaluationResponse.model_validate_json(response.text)

def batch(self, request: BatchEvaluationRequest) -> BatchEvaluationResponse:
Expand All @@ -66,9 +65,5 @@ def batch(self, request: BatchEvaluationRequest) -> BatchEvaluationResponse:
json=request.model_dump(),
)

if response.status_code != 200:
body = response.json()
message = body.get("message", HTTPStatus(response.status_code).description)
raise FliptApiError(message, response.status_code)

self._raise_on_error(response)
return BatchEvaluationResponse.model_validate_json(response.text)
23 changes: 15 additions & 8 deletions flipt-python/flipt/flags/async_flag_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import httpx

from flipt.models import ListParameters
from flipt.authentication import AuthenticationStrategy
from flipt.exceptions import FliptApiError
from flipt.models import CommonParameters, ListParameters

from ..authentication import AuthenticationStrategy
from ..exceptions import FliptApiError
from .models import (
ListFlagsResponse,
)
from .models import Flag, ListFlagsResponse


class AsyncFlag:
Expand All @@ -35,11 +33,20 @@ def _raise_on_error(self, response: httpx.Response) -> None:
message = body.get("message", HTTPStatus(response.status_code).description)
raise FliptApiError(message, response.status_code)

async def list_flags(self, *, namespace_key: str, params: ListParameters | None = None) -> ListFlagsResponse:
async def list_flags(self, namespace_key: str, params: ListParameters | None = None) -> ListFlagsResponse:
response = await self._client.get(
f"{self.url}/api/v1/namespaces/{namespace_key}/flags",
params=params.model_dump_json(exclude_none=True) if params else {},
params=params.model_dump_json(exclude_none=True, by_alias=True) if params else {},
headers=self.headers,
)
self._raise_on_error(response)
return ListFlagsResponse.model_validate_json(response.text)

async def get_flag(self, namespace_key: str, flag_key: str, params: CommonParameters | None = None) -> Flag:
response = await self._client.get(
f"{self.url}/api/v1/namespaces/{namespace_key}/flags/{flag_key}",
params=params.model_dump_json(exclude_none=True, by_alias=True) if params else {},
headers=self.headers,
)
self._raise_on_error(response)
return Flag.model_validate_json(response.text)
31 changes: 21 additions & 10 deletions flipt-python/flipt/flags/models.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
from datetime import datetime
from enum import Enum
from typing import Any
from enum import StrEnum

from flipt.models import CamelAliasModel, PaginatedResponse


class FlagType(str, Enum):
class FlagType(StrEnum):
variant = "VARIANT_FLAG_TYPE"
boolean = "BOOLEAN_FLAG_TYPE"


class Flag(CamelAliasModel):
created_at: datetime
class Variant(CamelAliasModel):
attachment: str
description: str
enabled: bool
flag_key: str
id: str
key: str
name: str
namespace_key: str
created_at: datetime
updated_at: datetime


class Flag(CamelAliasModel):
key: str
name: str
namespacekey: str | None = None
description: str
enabled: bool
namespace_key: str
type: FlagType
updatedAt: datetime
variants: list[Any]
created_at: datetime
updated_at: datetime
variants: list[Variant]


class ListFlagsResponse(CamelAliasModel, PaginatedResponse):
class ListFlagsResponse(PaginatedResponse):
flags: list[Flag]
23 changes: 15 additions & 8 deletions flipt-python/flipt/flags/sync_flag_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import httpx

from flipt.models import ListParameters
from flipt.authentication import AuthenticationStrategy
from flipt.exceptions import FliptApiError
from flipt.models import CommonParameters, ListParameters

from ..authentication import AuthenticationStrategy
from ..exceptions import FliptApiError
from .models import (
ListFlagsResponse,
)
from .models import Flag, ListFlagsResponse


class SyncFlag:
Expand All @@ -35,11 +33,20 @@ def _raise_on_error(self, response: httpx.Response) -> None:
message = body.get("message", HTTPStatus(response.status_code).description)
raise FliptApiError(message, response.status_code)

def list_flags(self, *, namespace_key: str, params: ListParameters | None = None) -> ListFlagsResponse:
def list_flags(self, namespace_key: str, params: ListParameters | None = None) -> ListFlagsResponse:
response = self._client.get(
f"{self.url}/api/v1/namespaces/{namespace_key}/flags",
params=params.model_dump_json(exclude_none=True) if params else {},
params=params.model_dump_json(exclude_none=True, by_alias=True) if params else {},
headers=self.headers,
)
self._raise_on_error(response)
return ListFlagsResponse.model_validate_json(response.text)

def get_flag(self, namespace_key: str, flag_key: str, params: CommonParameters | None = None) -> Flag:
response = self._client.get(
f"{self.url}/api/v1/namespaces/{namespace_key}/flags/{flag_key}",
params=params.model_dump_json(exclude_none=True, by_alias=True) if params else {},
headers=self.headers,
)
self._raise_on_error(response)
return Flag.model_validate_json(response.text)
15 changes: 9 additions & 6 deletions flipt-python/flipt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ class CamelAliasModel(BaseModel):
)


class ListParameters(BaseModel):
class CommonParameters(CamelAliasModel):
reference: str | None = None


class ListParameters(CommonParameters):
limit: int | None = None
offset: int | None = None
pageToken: str | None = None
reference: str | None = None
page_token: str | None = None


class PaginatedResponse(BaseModel):
nextPageToken: str
totalCount: int
class PaginatedResponse(CamelAliasModel):
next_page_token: str
total_count: int
Loading
Loading