Skip to content

Commit

Permalink
Small refactor of URL logic + additional methods
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell committed Jan 23, 2024
1 parent 4f1d23d commit c964afa
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions flagsmith_admin_client/flagsmith_admin_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ def __init__(self, auth_token: str, api_url: str = None):
self.api_url = api_url or DEFAULT_API_URL

def get_organisations(self) -> list[Organisation]:
url = f"{self.api_url}/organisations/"
response = self._make_request(url)
response = self._make_request("/organisations/")
return [Organisation.model_validate(org) for org in response.json()["results"]]

def get_organisation_by_name(self, name: str) -> Organisation:
return next(filter(lambda o: o.name == name, self.get_organisations()))

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)
response = self._make_request("/organisations/", method="POST", json_data=data)
# TODO: can we find a (pythonic) way to mutate the input object so that it
# includes the id. Similar to the django ORM for example.
return Organisation.model_validate(response.json())
Expand All @@ -33,36 +31,41 @@ def update_organisation(self, organisation: Organisation) -> Organisation:
pass

def delete_organisation(self, organisation: Organisation) -> None:
url = f"{self.api_url}/organisations/{organisation.id}/"
self._make_request(url, method="DELETE")
self._make_request(f"/organisations/{organisation.id}/", method="DELETE")

def get_projects(self, organisation_id: int) -> list[Project]:
pass
uri = "/projects/"
query_params = {"organisation": str(organisation_id)}
response = self._make_request(uri, query_params=query_params)
return [Project.model_validate(result) for result in response.json()["results"]]

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, 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=data)
response = self._make_request("/projects/", method="POST", json_data=data)
return Project.model_validate(response.json())

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

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=data)
response = self._make_request("/environments/", method="POST", json_data=data)
return Environment.model_validate(response.json())

def get_environments(self, project_id: int) -> list[Environment]:
uri = "/environments/"
query_params = {"project": str(project_id)}
response = self._make_request(uri, query_params=query_params)
return [Environment.model_validate(result) for result in response.json()["results"]]

# TODO: more environment methods

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)
response = self._make_request(f"/projects/{project_id}/features/", method="POST", json_data=data)
return Feature.model_validate(response.json())

# TODO: more feature methods
Expand All @@ -75,12 +78,14 @@ def create_segment(self, name: str, project_id: int, rules: list[SegmentRule]) -
"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))
response = self._make_request(f"/projects/{project_id}/segments/", method="POST", json_data=segment.model_dump(by_alias=True))
return Segment.model_validate(response.json())

def _make_request(self, url: str, method: str = "GET", data: dict[str, Any] = None) -> Response:
response: Response = getattr(self.session, method.lower())(url, json=data)
def _make_request(self, uri: str, method: str = "GET", json_data: dict[str, Any] = None, query_params: dict[str, str] = None) -> Response:
url = f"{self.api_url}{uri}"
if query_params:
url += f"?{'&'.join([f'{k}={v}' for k,v in query_params.items()])}"
response: Response = getattr(self.session, method.lower())(url, json=json_data)
if response.status_code >= 400:
# TODO: better error handling
response.raise_for_status()
Expand Down

0 comments on commit c964afa

Please sign in to comment.