-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(python): Add
ListFlags
api (#127)
- Loading branch information
Showing
13 changed files
with
204 additions
and
12 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
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,15 @@ | ||
from .async_flag_client import AsyncFlag | ||
from .models import ( | ||
Flag, | ||
FlagType, | ||
ListFlagsResponse, | ||
) | ||
from .sync_flag_client import SyncFlag | ||
|
||
__all__ = [ | ||
"AsyncFlag", | ||
"SyncFlag", | ||
"ListFlagsResponse", | ||
"Flag", | ||
"FlagType", | ||
] |
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,45 @@ | ||
from http import HTTPStatus | ||
|
||
import httpx | ||
|
||
from flipt.models import ListParameters | ||
|
||
from ..authentication import AuthenticationStrategy | ||
from ..exceptions import FliptApiError | ||
from .models import ( | ||
ListFlagsResponse, | ||
) | ||
|
||
|
||
class AsyncFlag: | ||
def __init__( | ||
self, | ||
url: str, | ||
authentication: AuthenticationStrategy | None = None, | ||
httpx_client: httpx.AsyncClient | None = None, | ||
): | ||
self.url = url | ||
self.headers: dict[str, str] = {} | ||
|
||
self._client = httpx_client or httpx.AsyncClient() | ||
|
||
if authentication: | ||
authentication.authenticate(self.headers) | ||
|
||
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 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 {}, | ||
headers=self.headers, | ||
) | ||
self._raise_on_error(response) | ||
return ListFlagsResponse.model_validate_json(response.text) |
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,26 @@ | ||
from datetime import datetime | ||
from enum import Enum | ||
from typing import Any | ||
|
||
from flipt.models import CamelAliasModel, PaginatedResponse | ||
|
||
|
||
class FlagType(str, Enum): | ||
variant = "VARIANT_FLAG_TYPE" | ||
boolean = "BOOLEAN_FLAG_TYPE" | ||
|
||
|
||
class Flag(CamelAliasModel): | ||
created_at: datetime | ||
description: str | ||
enabled: bool | ||
key: str | ||
name: str | ||
namespacekey: str | None = None | ||
type: FlagType | ||
updatedAt: datetime | ||
variants: list[Any] | ||
|
||
|
||
class ListFlagsResponse(CamelAliasModel, PaginatedResponse): | ||
flags: list[Flag] |
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,45 @@ | ||
from http import HTTPStatus | ||
|
||
import httpx | ||
|
||
from flipt.models import ListParameters | ||
|
||
from ..authentication import AuthenticationStrategy | ||
from ..exceptions import FliptApiError | ||
from .models import ( | ||
ListFlagsResponse, | ||
) | ||
|
||
|
||
class SyncFlag: | ||
def __init__( | ||
self, | ||
url: str, | ||
authentication: AuthenticationStrategy | None = None, | ||
httpx_client: httpx.Client | None = None, | ||
): | ||
self.url = url | ||
self.headers: dict[str, str] = {} | ||
|
||
self._client = httpx_client or httpx.Client() | ||
|
||
if authentication: | ||
authentication.authenticate(self.headers) | ||
|
||
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 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 {}, | ||
headers=self.headers, | ||
) | ||
self._raise_on_error(response) | ||
return ListFlagsResponse.model_validate_json(response.text) |
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,21 @@ | ||
from pydantic import AliasGenerator, BaseModel, ConfigDict | ||
from pydantic.alias_generators import to_camel | ||
|
||
|
||
class CamelAliasModel(BaseModel): | ||
model_config = ConfigDict( | ||
alias_generator=AliasGenerator(alias=to_camel), | ||
populate_by_name=True, | ||
) | ||
|
||
|
||
class ListParameters(BaseModel): | ||
limit: int | None = None | ||
offset: int | None = None | ||
pageToken: str | None = None | ||
reference: str | None = None | ||
|
||
|
||
class PaginatedResponse(BaseModel): | ||
nextPageToken: str | ||
totalCount: int |
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
Empty file.
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,13 @@ | ||
from http import HTTPStatus | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture(params=[{}, {'message': 'some error'}]) | ||
def _mock_list_flags_response_error(httpx_mock, flipt_url, request): | ||
httpx_mock.add_response( | ||
method="GET", | ||
url=f'{flipt_url}/api/v1/namespaces/default/flags', | ||
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, | ||
json=request.param, | ||
) |
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,15 @@ | ||
import pytest | ||
|
||
from flipt.async_client import AsyncFliptClient | ||
from flipt.exceptions import FliptApiError | ||
|
||
|
||
class TestListFlags: | ||
async def test_success(self, async_flipt_client: AsyncFliptClient): | ||
list_response = await async_flipt_client.flag.list_flags(namespace_key="default") | ||
assert len(list_response.flags) == 2 | ||
|
||
@pytest.mark.usefixtures("_mock_list_flags_response_error") | ||
async def test_list_error(self, async_flipt_client): | ||
with pytest.raises(FliptApiError): | ||
await async_flipt_client.flag.list_flags(namespace_key="default") |
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,14 @@ | ||
import pytest | ||
|
||
from flipt.exceptions import FliptApiError | ||
|
||
|
||
class TestListFlags: | ||
def test_success(self, sync_flipt_client): | ||
list_response = sync_flipt_client.flag.list_flags(namespace_key="default") | ||
assert len(list_response.flags) == 2 | ||
|
||
@pytest.mark.usefixtures("_mock_list_flags_response_error") | ||
def test_list_error(self, sync_flipt_client): | ||
with pytest.raises(FliptApiError): | ||
sync_flipt_client.flag.list_flags(namespace_key="default") |