Skip to content

Commit 3d6020b

Browse files
committed
Add space
1 parent 92fb65e commit 3d6020b

2 files changed

Lines changed: 29 additions & 29 deletions

File tree

metablock/orgs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,8 @@ async def get(self, name_or_id: str) -> Org:
9898
"""Get an organization by name or id"""
9999
data = await self.cli.get(f"{self.url}/{name_or_id}")
100100
return Org(root=self, root_path=data["short_name"], **data)
101+
102+
async def create(self, **data: Any) -> Org:
103+
"""Create a new organization"""
104+
data = await self.cli.post(self.url, json=data)
105+
return Org(root=self, root_path=data["short_name"], **data)

metablock/spaces.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
from datetime import datetime
44
from pathlib import Path
5-
from typing import Any
5+
from typing import TYPE_CHECKING, Any
66

77
from pydantic import BaseModel, Field
88

99
from .components import MetablockComponent, MetablockEntity
1010

11+
if TYPE_CHECKING:
12+
from .client import Metablock
13+
1114

1215
# Space
1316
class Space(MetablockEntity):
@@ -87,7 +90,7 @@ class Block(MetablockEntity):
8790
default_factory=list,
8891
description="An optional set of strings",
8992
)
90-
# space: Space = Field(description="The space of the block")
93+
space: Space = Field(description="The space of the block")
9194

9295
@property
9396
def deployments(self) -> Deployments:
@@ -131,47 +134,39 @@ class Blocks(MetablockComponent):
131134
async def get(self, block_id: str) -> Block:
132135
"""Get a block by id"""
133136
data = await self.cli.get(f"{self.url}/{block_id}")
134-
return Block(
135-
root=self,
136-
root_path=data["id"],
137-
is_root=data.pop("root", False),
138-
**data,
139-
)
137+
return block_from_data(self.cli, data)
140138

141139
async def update(self, block_id: str, **kwargs: Any) -> Block:
142140
"""Update a block by id"""
143141
data = await self.cli.patch(f"{self.url}/{block_id}", json=kwargs)
144-
return Block(
145-
root=self,
146-
root_path=data["id"],
147-
is_root=data.pop("root", False),
148-
**data,
149-
)
142+
return block_from_data(self.cli, data)
150143

151144

152145
class SpaceBlocks(Blocks):
153146
async def get_list(self) -> list[Block]:
154147
"""Get a list of blocks in the space"""
155148
data = await self.cli.get(self.url)
156-
return [
157-
Block(
158-
root=self.cli.blocks,
159-
root_path=s["id"],
160-
is_root=s.pop("root", False),
161-
**s,
162-
)
163-
for s in data
164-
]
149+
return [block_from_data(self.cli, d) for d in data]
165150

166151
async def create(self, name: str, **kwargs: Any) -> Block:
167152
"""Create a new block in the space"""
168153
data = await self.cli.post(self.url, json=dict(name=name, **kwargs))
169-
return Block(
170-
root=self.cli.cli,
171-
root_path=data["id"],
172-
is_root=data.pop("root", False),
173-
**data,
174-
)
154+
return block_from_data(self.cli, data)
155+
156+
157+
def block_from_data(cli: Metablock, data: dict) -> Block:
158+
data = data.copy()
159+
data["space"] = Space(
160+
root=cli.spaces,
161+
root_path=data["space"]["id"],
162+
**data["space"],
163+
)
164+
return Block(
165+
root=cli.blocks,
166+
root_path=data["id"],
167+
is_root=data.pop("root", False),
168+
**data,
169+
)
175170

176171

177172
# SpaceExtension

0 commit comments

Comments
 (0)