Skip to content

Commit

Permalink
feat(python): added async client
Browse files Browse the repository at this point in the history
  • Loading branch information
v.kozyar committed Feb 22, 2024
1 parent a029fcb commit 893f98d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions flipt-python/flipt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from .async_client import AsyncFliptClient
from .sync_client import FliptClient

__all__ = [
'FliptClient',
'AsyncFliptClient',
]
19 changes: 19 additions & 0 deletions flipt-python/flipt/async_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import httpx

from .authentication import AuthenticationStrategy
from .evaluation import AsyncEvaluation


class AsyncFliptClient:
def __init__(
self,
url: str = "http://localhost:8080",
timeout: int = 60,
authentication: AuthenticationStrategy | None = None,
):
self.httpx_client = httpx.AsyncClient(timeout=timeout)

self.evaluation = AsyncEvaluation(url, authentication, self.httpx_client)

async def close(self) -> None:
await self.httpx_client.aclose()
2 changes: 2 additions & 0 deletions flipt-python/flipt/evaluation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .async_evaluation_client import AsyncEvaluation
from .models import (
BatchEvaluationRequest,
BatchEvaluationResponse,
Expand All @@ -14,6 +15,7 @@

__all__ = [
'Evaluation',
'AsyncEvaluation',
'EvaluationResponseType',
'EvaluationReason',
'ErrorEvaluationReason',
Expand Down
74 changes: 74 additions & 0 deletions flipt-python/flipt/evaluation/async_evaluation_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from http import HTTPStatus

import httpx

from ..authentication import AuthenticationStrategy
from ..exceptions import FliptApiError
from .models import (
BatchEvaluationRequest,
BatchEvaluationResponse,
BooleanEvaluationResponse,
EvaluationRequest,
VariantEvaluationResponse,
)


class AsyncEvaluation:
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()

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)

return VariantEvaluationResponse.model_validate_json(response.text)

async def boolean(self, request: EvaluationRequest) -> BooleanEvaluationResponse:
response = await self._client.post(
f"{self.url}/evaluate/v1/boolean",
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)

return BooleanEvaluationResponse.model_validate_json(response.text)

async def batch(self, request: BatchEvaluationRequest) -> BatchEvaluationResponse:
response = await self._client.post(
f"{self.url}/evaluate/v1/batch",
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)

return BatchEvaluationResponse.model_validate_json(response.text)

0 comments on commit 893f98d

Please sign in to comment.