Skip to content

Commit

Permalink
feat: use auth strategy for python (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
markphelps committed Jan 10, 2024
1 parent 3f65c2e commit 930291a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
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

0 comments on commit 930291a

Please sign in to comment.