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: use auth strategy for python #28

Merged
merged 1 commit into from
Jan 10, 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
6 changes: 3 additions & 3 deletions flipt-python/flipt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import typing
from .evaluation import Evaluation
from .authentication import AuthenticationStrategy


class FliptClient:
def __init__(
self,
url: str = "http://localhost:8080",
client_token: typing.Optional[str] = None,
jwt_token: typing.Optional[str] = None,
timeout: int = 60,
authentication: typing.Optional[AuthenticationStrategy] = None,
):
self.evaluation = Evaluation(url, client_token, jwt_token, timeout)
self.evaluation = Evaluation(url, timeout, authentication)
19 changes: 19 additions & 0 deletions flipt-python/flipt/authentication/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class AuthenticationStrategy:
def authenticate(self, headers: dict):
raise NotImplementedError()


class ClientTokenAuthentication(AuthenticationStrategy):
def __init__(self, token: str):
self.token = token

def authenticate(self, headers: dict):
headers["Authorization"] = f"Bearer {self.token}"


class JWTAuthentication(AuthenticationStrategy):
def __init__(self, token: str):
self.token = token

def authenticate(self, headers: dict):
headers["Authorization"] = f"JWT {self.token}"
10 changes: 4 additions & 6 deletions flipt-python/flipt/evaluation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@
EvaluationRequest,
VariantEvaluationResponse,
)
from ..authentication import AuthenticationStrategy


class Evaluation:
def __init__(
self,
url: str,
client_token: typing.Optional[str],
jwt_token: typing.Optional[str],
timeout: int,
authentication: typing.Optional[AuthenticationStrategy] = None,
):
self.url = url
self.headers = {}
if client_token != None:
self.headers["Authorization"] = f"Bearer {client_token}"
if jwt_token != None:
self.headers["Authorization"] = f"JWT {jwt_token}"
self.timeout = timeout
if authentication:
authentication.authenticate(self.headers)

def variant(self, request: EvaluationRequest) -> VariantEvaluationResponse:
response = httpx.post(
Expand Down
1 change: 0 additions & 1 deletion flipt-python/flipt/evaluation/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import enum
from pydantic import BaseModel, Field
from typing import List, Optional

Expand Down
5 changes: 4 additions & 1 deletion flipt-python/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
from flipt import FliptClient
from flipt.evaluation import BatchEvaluationRequest, EvaluationRequest
from flipt.authentication import ClientTokenAuthentication


class TestFliptEvaluationClient(unittest.TestCase):
Expand All @@ -14,7 +15,9 @@ def setUp(self) -> None:
if auth_token is None:
raise Exception("FLIPT_AUTH_TOKEN not set")

self.flipt_client = FliptClient(url=flipt_url, client_token=auth_token)
self.flipt_client = FliptClient(
url=flipt_url, authentication=ClientTokenAuthentication(auth_token)
)

def test_variant(self):
variant = self.flipt_client.evaluation.variant(
Expand Down
Loading