-
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.
- Loading branch information
v.kozyar
committed
Feb 22, 2024
1 parent
a029fcb
commit 893f98d
Showing
4 changed files
with
97 additions
and
0 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
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', | ||
] |
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,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() |
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,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) |