Skip to content

Commit

Permalink
Use kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell committed Jan 23, 2024
1 parent 20ba3e4 commit 4f1d23d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
34 changes: 22 additions & 12 deletions flagsmith_admin_client/flagsmith_admin_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from requests import Session, Response

from flagsmith_admin_client.models import Organisation, Project, Environment, Feature, Segment
from flagsmith_admin_client.models import Organisation, Project, Environment, Feature, Segment, SegmentRule

DEFAULT_API_URL = "https://api.flagsmith.com/api/v1"

Expand All @@ -21,8 +21,8 @@ def get_organisations(self) -> list[Organisation]:
def get_organisation_by_name(self, name: str) -> Organisation:
return next(filter(lambda o: o.name == name, self.get_organisations()))

def create_organisation(self, organisation: Organisation) -> Organisation:
data = {"name": organisation.name}
def create_organisation(self, name: str) -> Organisation:
data = {"name": name}
url = f"{self.api_url}/organisations/"
response = self._make_request(url, method="POST", data=data)
# TODO: can we find a (pythonic) way to mutate the input object so that it
Expand All @@ -42,30 +42,40 @@ def get_projects(self, organisation_id: int) -> list[Project]:
def get_project_by_name(self, organisation_id: int, name: str) -> list[Project]:
return next(filter(lambda p: p.name == name, self.get_projects(organisation_id)))

def create_project(self, project: Project) -> Project:
def create_project(self, name: str, organisation_id: int) -> Project:
data = {"name": name, "organisation": organisation_id}
url = f"{self.api_url}/projects/"
response = self._make_request(url, method="POST", data=project.model_dump(by_alias=True))
response = self._make_request(url, method="POST", data=data)
return Project.model_validate(response.json())

def delete_project(self, project: Project) -> None:
pass

def create_environment(self, environment: Environment) -> Environment:
def create_environment(self, name: str, project_id: int) -> Environment:
data = {"name": name, "project": project_id}
url = f"{self.api_url}/environments/"
response = self._make_request(url, method="POST", data=environment.model_dump(by_alias=True))
response = self._make_request(url, method="POST", data=data)
return Environment.model_validate(response.json())

# TODO: more environment methods

def create_feature(self, feature: Feature) -> Feature:
url = f"{self.api_url}/projects/{feature.project_id}/features/"
response = self._make_request(url, method="POST", data=feature.model_dump(by_alias=True))
def create_feature(self, name: str, project_id: int) -> Feature:
data = {"name": name, "project": project_id}
url = f"{self.api_url}/projects/{project_id}/features/"
response = self._make_request(url, method="POST", data=data)
return Feature.model_validate(response.json())

# TODO: more feature methods

def create_segment(self, segment: Segment) -> Segment:
url = f"{self.api_url}/projects/{segment.project_id}/segments/"
def create_segment(self, name: str, project_id: int, rules: list[SegmentRule]) -> Segment:
segment = Segment.model_validate(
{
"name": name,
"project_id": project_id,
"rules": [rule.model_dump() for rule in rules]
}
)
url = f"{self.api_url}/projects/{project_id}/segments/"
response = self._make_request(url, method="POST", data=segment.model_dump(by_alias=True))
return Segment.model_validate(response.json())

Expand Down
6 changes: 1 addition & 5 deletions flagsmith_admin_client/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@

class _BaseModel(BaseModel):
model_config = ConfigDict(extra="ignore")
id: int = None

@property
def is_created(self):
return self.id is not None
id: int | None = None


class Organisation(_BaseModel):
Expand Down
13 changes: 6 additions & 7 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@pytest.fixture()
def auth_token() -> str:
return ""
return "1aa8568202cb644f6663cd091c702b1365ebc2dc"


@pytest.fixture()
Expand All @@ -19,19 +19,19 @@ def test(client) -> None:
organisation = Organisation(name="test from admin client")

try:
organisation = client.create_organisation(organisation)
organisation = client.create_organisation("test from admin client")
assert organisation.id is not None

project = client.create_project(Project(name="Test project", organisation_id=organisation.id))
project = client.create_project(name="Test project", organisation_id=organisation.id)
assert project.id

environment = client.create_environment(Environment(name="Test environment", project_id=project.id))
environment = client.create_environment(name="Test environment", project_id=project.id)
assert environment.id

feature = client.create_feature(Feature(name="test_feature", project_id=project.id))
feature = client.create_feature(name="test_feature", project_id=project.id)
assert feature.id

segment = Segment(
segment = client.create_segment(
name="test segment",
project_id=project.id,
rules=[
Expand All @@ -50,7 +50,6 @@ def test(client) -> None:
)
]
)
segment = client.create_segment(segment)
assert segment.id is not None

finally:
Expand Down

0 comments on commit 4f1d23d

Please sign in to comment.