Skip to content

Commit b6895d2

Browse files
committed
Use pydantic
1 parent fee0ade commit b6895d2

11 files changed

Lines changed: 614 additions & 505 deletions

File tree

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2025 Quantmind
1+
Copyright (c) 2026 Quantmind
22

33
Redistribution and use in source and binary forms, with or without modification,
44
are permitted provided that the following conditions are met:

metablock/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ async def _apply(path: str, space_name: str, token: str, dry_run: bool) -> None:
125125
raise click.Abort()
126126
async with Metablock(auth_key=token) as mb:
127127
space = await mb.spaces.get(space_name)
128-
blocks = await space.blocks.get_list()
129-
click.echo(f"space {space.name} has {len(blocks)} blocks")
130-
by_name = {s.name: s for s in blocks}
128+
space_blocks = await space.blocks.get_list()
129+
click.echo(f"space {space.name} has {len(space_blocks)} blocks")
130+
by_name = {s.name: s for s in space_blocks}
131131
for name, config in blocks:
132132
block = by_name.get(name)
133133
if block:

metablock/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ async def handle_response(self, response: ClientResponse, wrap: Any = None) -> A
128128
data = response.json()
129129
return wrap(data) if wrap else data
130130

131-
async def get_user(self) -> User:
132-
data = await self.get(f"{self.url}/user")
131+
async def get_user(self, **kwargs: Any) -> User:
132+
data = await self.get(f"{self.url}/user", **kwargs)
133133
return User(root=self, root_path="user", **data)
134134

135135
async def update_user(self, **params: Any) -> User:

metablock/orgs.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,32 @@
33
from typing import Any
44

55
from pydantic import Field
6+
from typing_extensions import Annotated, Doc
67

78
from .components import MetablockComponent, MetablockEntity
89
from .extensions import Extension, Extensions
910
from .spaces import Space
11+
from .utils import compact_dict
1012

1113

1214
class OrgSpaces(MetablockComponent):
13-
async def get_list(self) -> list[Space]:
14-
data = await self.cli.get(self.url)
15-
return [
16-
Space(root=self.cli, root_path=f"spaces/{s['name']}", **s) for s in data
17-
]
15+
async def get_list(
16+
self,
17+
*,
18+
limit: Annotated[int | None, Doc("Maximum number of spaces to return")] = None,
19+
cursor: Annotated[str | None, Doc("Cursor for pagination")] = None,
20+
**kwargs: Any,
21+
) -> list[Space]:
22+
data = await self.cli.get(
23+
self.url,
24+
params=compact_dict(limit=limit, cursor=cursor),
25+
**kwargs,
26+
)
27+
return [Space(root=self.cli.spaces, root_path=s["name"], **s) for s in data]
1828

1929
async def create(self, **data: Any) -> Space:
2030
data = await self.cli.post(f"{self.url}", json=data)
21-
return Space(root=self.cli, root_path=f"spaces/{data['name']}", **data)
31+
return Space(root=self.cli.spaces, root_path=data["name"], **data)
2232

2333

2434
class OrgExtensions(Extensions):

metablock/spaces.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Spaces(MetablockComponent):
3232

3333
async def get(self, space_id_or_name: str) -> Space:
3434
data = await self.cli.get(f"{self.url}/{space_id_or_name}")
35-
return Space(root=self.cli, root_path=data["id"], **data)
35+
return Space(root=self, root_path=data["id"], **data)
3636

3737

3838
class Route(BaseModel):
@@ -82,9 +82,6 @@ class Block(MetablockEntity):
8282
def deployments(self) -> Deployments:
8383
return Deployments(root=self, root_path="deployments")
8484

85-
async def config(self, *, callback: Any = None) -> dict:
86-
return await self.cli.get(f"{self.url}/config", callback=callback)
87-
8885
async def certificate(self) -> Certificate:
8986
data = await self.cli.get(f"{self.url}/certificate")
9087
return Certificate(**data)
@@ -124,7 +121,7 @@ async def get(self, block_id: str) -> Block:
124121
"""Get a block by id"""
125122
data = await self.cli.get(f"{self.url}/{block_id}")
126123
return Block(
127-
root=self.cli,
124+
root=self,
128125
root_path=data["id"],
129126
is_root=data.pop("root", False),
130127
**data,
@@ -134,7 +131,7 @@ async def patch(self, block_id: str, **kwargs: Any) -> Block:
134131
"""Update a block by id"""
135132
data = await self.cli.patch(f"{self.url}/{block_id}", json=kwargs)
136133
return Block(
137-
root=self.cli,
134+
root=self,
138135
root_path=data["id"],
139136
is_root=data.pop("root", False),
140137
**data,
@@ -146,15 +143,20 @@ async def get_list(self) -> list[Block]:
146143
"""Get a list of blocks in the space"""
147144
data = await self.cli.get(self.url)
148145
return [
149-
Block(root=self.cli, root_path=s["id"], is_root=s.pop("root", False), **s)
146+
Block(
147+
root=self.cli.blocks,
148+
root_path=s["id"],
149+
is_root=s.pop("root", False),
150+
**s,
151+
)
150152
for s in data
151153
]
152154

153155
async def create(self, name: str, **kwargs: Any) -> Block:
154156
"""Create a new block in the space"""
155157
data = await self.cli.post(self.url, json=dict(name=name, **kwargs))
156158
return Block(
157-
root=self.cli,
159+
root=self.cli.cli,
158160
root_path=data["id"],
159161
is_root=data.pop("root", False),
160162
**data,

metablock/user.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class OrgMember(BaseModel):
1919
roles: list[str] = Field(description="The user's roles in the organization")
2020

2121
@classmethod
22-
def create(cls, cli: Metablock, **data: Any) -> Self:
23-
name = data["org"]["name"]
22+
def from_cli(cls, cli: Metablock, **data: Any) -> Self:
23+
name = data["org_name"]
2424
org = Org(
2525
root=cli,
2626
root_path=f"orgs/{name}",
@@ -48,7 +48,7 @@ class User(MetablockEntity):
4848
async def orgs(self, **kwargs: Any) -> list[OrgMember]:
4949
"""List user organizations"""
5050
orgs = await self.cli.get(f"{self.url}/orgs", **kwargs)
51-
return [OrgMember.create(self.cli, **data) for data in orgs]
51+
return [OrgMember.from_cli(self.cli, **data) for data in orgs]
5252

5353
async def get_permissions(self, **kwargs: Any) -> list[dict]:
5454
"""List user permissions"""

0 commit comments

Comments
 (0)