Skip to content

Commit f3a9601

Browse files
committed
refactor: supprot for api key names, other fixes
1 parent a95acde commit f3a9601

File tree

5 files changed

+41
-13
lines changed

5 files changed

+41
-13
lines changed

appwrite_lab/_orchestrator.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from appwrite_lab.automations.models import BaseVarModel, AppwriteAPIKeyCreation
1010
from ._state import State
1111
from dataclasses import dataclass
12-
from .models import Lab, Automation, SyncType, Project
12+
from .models import Lab, Automation, Project
1313
from dotenv import dotenv_values
1414
from appwrite_lab.utils import console
1515
from .utils import is_cli
@@ -20,7 +20,7 @@
2020
@dataclass
2121
class Response:
2222
message: str
23-
data: any = None
23+
data: str | dict | None = None
2424
error: bool = False
2525
_print_data: bool = False
2626

@@ -275,7 +275,6 @@ def deploy_playwright_automation(
275275
args: list[str] = [],
276276
*,
277277
print_data: bool = False,
278-
**kwargs,
279278
) -> str | Response:
280279
"""
281280
Deploy playwright automations on a lab (very few automations supported).

appwrite_lab/cli/new_menu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ def new_api_key(
103103
key = labs.create_api_key(
104104
project_name=project_name, lab_name=lab_name, expiration=expiration
105105
)
106-
return key
107-
# status.update(f"Creating API key for project '{project_name}'... done")
106+
status.update(f"Creating API key for project '{project_name}'... done")
107+
return key.data
108108

109109

110110
@new_menu.command(name="project", help="Create a new project")

appwrite_lab/labs.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,14 @@ def sync_with_appwrite_config(
100100
model=AppwriteSyncProject(sync_type),
101101
args=addn_args,
102102
)
103+
key_name = f"{proj_name}-key"
103104
key = self.create_api_key(
104-
lab=lab, expiration=expiration, project_name=proj_name
105+
lab=lab, expiration=expiration, project_name=proj_name, key_name=key_name
105106
)
106107
lab.projects[proj_name] = Project(
107108
project_id=proj_id,
108109
project_name=proj_name,
109-
api_key=key,
110+
api_key=key.data,
110111
)
111112
labs = self.state.get("labs")
112113
labs[name] = lab.to_dict()
@@ -115,6 +116,7 @@ def sync_with_appwrite_config(
115116
def create_api_key(
116117
self,
117118
project_name: str,
119+
key_name: str,
118120
expiration: Expiration = "30 days",
119121
lab_name: str | None = None,
120122
lab: Lab | None = None,
@@ -135,7 +137,7 @@ def create_api_key(
135137
automation=Automation.CREATE_API_KEY,
136138
model=AppwriteAPIKeyCreation(
137139
project_name=project_name,
138-
key_name=project_name,
140+
key_name=key_name,
139141
key_expiry=str(expiration.value),
140142
),
141143
print_data=True,
@@ -144,9 +146,11 @@ def create_api_key(
144146
return Response(
145147
message=f"Failed to create API key: {api_key.message}", error=True
146148
)
147-
# create another Response to print key
148-
# return Response(message=api_key.data, data=api_key.data)
149-
return api_key
149+
return Response(
150+
message=f"API key created for {project_name}",
151+
data=api_key.data,
152+
_print_data=True,
153+
)
150154

151155
def stop(self, name: str):
152156
return self.orchestrator.teardown_service(name)
@@ -177,8 +181,9 @@ def create_project(
177181
project_name=project_name,
178182
project_id=project_id,
179183
)
180-
self.orchestrator.deploy_playwright_automation(
184+
return self.orchestrator.deploy_playwright_automation(
181185
lab=lab,
182186
automation=Automation.CREATE_PROJECT,
187+
project=Project(project_id=project_id, project_name=project_name),
183188
model=apc,
184189
)

appwrite_lab/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def to_dict(self):
3737
class Project(_BaseClass):
3838
project_id: str
3939
project_name: str
40-
api_key: str
40+
api_key: str | None = None
4141

4242

4343
@dataclass

tests/test_labs.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,35 @@ def test_labs_new(lab: Lab):
1111
assert lab.url.endswith("8080")
1212
assert lab.projects.get("default") is not None
1313

14+
1415
@pytest.mark.e2e
1516
def test_labs_create_api_key(lab: Lab, lab_svc: Labs):
1617
default = lab.projects.get("default")
1718
res = lab_svc.create_api_key(
1819
project_name=default.project_name,
20+
key_name="default-api-key",
21+
expiration=Expiration.THIRTY_DAYS,
22+
lab=lab,
23+
)
24+
assert not res.error
25+
assert type(res.data) is str
26+
assert res.data.startswith("standard_")
27+
28+
29+
@pytest.mark.e2e
30+
def test_labs_create_project(lab: Lab, lab_svc: Labs):
31+
project_name = "test-project"
32+
project_id = "test-project-id"
33+
res = lab_svc.create_project(
34+
project_name=project_name,
35+
project_id=project_id,
36+
lab=lab,
37+
)
38+
assert not res.error
39+
40+
res = lab_svc.create_api_key(
41+
project_name=project_name,
42+
key_name=project_name,
1943
expiration=Expiration.THIRTY_DAYS,
2044
lab=lab,
2145
)

0 commit comments

Comments
 (0)