Skip to content

Commit d2dfc02

Browse files
author
Datata1
committed
feat(xyz): dasdasd
1 parent 01384b8 commit d2dfc02

28 files changed

+692
-770
lines changed

examples/teams/domains/update_workspace_connections.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ async def main():
1111
domainBuilder = DomainRouting()
1212

1313
routing = (
14-
domainBuilder.add_route("/", [74861])
15-
.add_route("/api", [74868])
16-
.add_route("/test", [74868])
14+
domainBuilder.add("/", [74861]).add("/api", [74868]).add("/test", [74868])
1715
)
1816

1917
domain = await team.domains.update_workspace_connections(
20-
domain_name="test.com", connections=routing
18+
name="test.com", connections=routing
2119
)
2220
print(f"Current routing: {domain.workspaces}")
2321

src/codesphere/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
TeamBase,
3030
Domain,
3131
CustomDomainConfig,
32-
WorkspaceConnectionItem,
3332
DomainVerificationStatus,
3433
DomainBase,
3534
DomainRouting,
@@ -63,7 +62,6 @@
6362
"Image",
6463
"Domain",
6564
"CustomDomainConfig",
66-
"WorkspaceConnectionItem",
6765
"DomainVerificationStatus",
6866
"DomainBase",
6967
"DomainsResource",

src/codesphere/client.py

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,13 @@
1-
"""
2-
Codesphere SDK Client
3-
4-
This module provides the main client class, CodesphereSDK.
5-
"""
6-
71
from .http_client import APIHttpClient
82
from .resources.metadata import MetadataResource
93
from .resources.team import TeamsResource
104
from .resources.workspace import WorkspacesResource
115

126

137
class CodesphereSDK:
14-
"""The main entrypoint for interacting with the `Codesphere Public API <https://codesphere.com/api/swagger-ui/?ref=codesphere.ghost.io#/>`_.
15-
16-
This class manages the HTTP client, its lifecycle,
17-
and provides access to the various API resources.
18-
19-
Primary usage is via an asynchronous context manager:
20-
21-
Usage:
22-
>>> import asyncio
23-
>>> from codesphere import CodesphereSDK
24-
>>>
25-
>>> async def main():
26-
>>> async with CodesphereSDK() as sdk:
27-
>>> teams = await sdk.teams.list()
28-
>>> print(teams)
29-
>>>
30-
>>> asyncio.run(main())
31-
32-
Attributes:
33-
teams (TeamsResource): Access to Team API operations.
34-
workspaces (WorkspacesResource): Access to Workspace API operations.
35-
metadata (MetadataResource): Access to Metadata API operations.
36-
domains (DomainResource): Access to Domain API operations.
37-
"""
38-
398
teams: TeamsResource
40-
"""Access to the Team API. (e.g., `sdk.teams.list()`)"""
41-
429
workspaces: WorkspacesResource
43-
"""Access to the Workspace API. (e.g., `sdk.workspaces.list()`)"""
44-
4510
metadata: MetadataResource
46-
"""Access to the Metadata API. (e.g., `sdk.metadata.list_plans()`)"""
4711

4812
def __init__(self):
4913
self._http_client = APIHttpClient()
@@ -52,20 +16,9 @@ def __init__(self):
5216
self.metadata = MetadataResource(self._http_client)
5317

5418
async def open(self):
55-
"""Manually opens the underlying HTTP client session.
56-
57-
Required for manual lifecycle control when not using `async with`.
58-
59-
Usage:
60-
>>> sdk = CodesphereSDK()
61-
>>> await sdk.open()
62-
>>> # ... API calls ...
63-
>>> await sdk.close()
64-
"""
6519
await self._http_client.open()
6620

6721
async def close(self):
68-
"""Manually closes the underlying HTTP client session."""
6922
await self._http_client.close()
7023

7124
async def __aenter__(self):

src/codesphere/config.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33

44

55
class Settings(BaseSettings):
6-
"""
7-
API Client Settings
8-
"""
9-
106
model_config = SettingsConfigDict(
117
env_file=".env", env_file_encoding="utf-8", env_prefix="CS_"
128
)

src/codesphere/core/base.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
1+
from typing import Generic, List, TypeVar
2+
from pydantic import BaseModel, ConfigDict, RootModel
3+
from pydantic.alias_generators import to_camel
4+
15
from ..http_client import APIHttpClient
26
from .handler import _APIOperationExecutor
37

8+
ModelT = TypeVar("ModelT")
9+
410

511
class ResourceBase(_APIOperationExecutor):
612
def __init__(self, http_client: APIHttpClient):
713
self._http_client = http_client
14+
15+
16+
class CamelModel(BaseModel):
17+
model_config = ConfigDict(
18+
alias_generator=to_camel,
19+
populate_by_name=True,
20+
)
21+
22+
23+
class ResourceList(RootModel[List[ModelT]], Generic[ModelT]):
24+
root: List[ModelT]
25+
26+
def __iter__(self):
27+
return iter(self.root)
28+
29+
def __getitem__(self, item):
30+
return self.root[item]
31+
32+
def __len__(self):
33+
return len(self.root)

src/codesphere/core/operations.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
from typing import Callable, Awaitable, List, Optional, Type, TypeAlias, TypeVar
1+
from typing import Callable, Awaitable, Generic, Optional, Type, TypeAlias, TypeVar
22

33
from pydantic import BaseModel
44

55

66
_T = TypeVar("_T")
7+
ResponseT = TypeVar("ResponseT")
8+
InputT = TypeVar("InputT")
79

810
AsyncCallable: TypeAlias = Callable[[], Awaitable[_T]]
911

1012

11-
class APIOperation:
12-
def __init__(
13-
self,
14-
method: str,
15-
endpoint_template: str,
16-
response_model: Type[BaseModel] | Type[List[BaseModel]],
17-
input_model: Optional[Type[BaseModel]] = None,
18-
):
19-
self.method = method
20-
self.endpoint_template = endpoint_template
21-
self.response_model = response_model
22-
self.input_model = input_model
13+
class APIOperation(BaseModel, Generic[ResponseT, InputT]):
14+
method: str
15+
endpoint_template: str
16+
response_model: Type[ResponseT]
17+
input_model: Optional[Type[InputT]] = None
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Metadata Resource & Models"""
22

3-
from .models import Datacenter, Characteristic, WsPlan, Image
3+
from .schemas import Datacenter, Characteristic, WsPlan, Image
44
from .resources import MetadataResource
55

66
__all__ = ["Datacenter", "Characteristic", "WsPlan", "Image", "MetadataResource"]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from ...core.base import ResourceList
2+
from ...core.operations import APIOperation
3+
from .schemas import Datacenter, Image, WsPlan
4+
5+
_LIST_DC_OP = APIOperation(
6+
method="GET",
7+
endpoint_template="/metadata/datacenters",
8+
input_model=type(None),
9+
response_model=ResourceList[Datacenter],
10+
)
11+
12+
_LIST_PLANS_OP = APIOperation(
13+
method="GET",
14+
endpoint_template="/metadata/workspace-plans",
15+
input_model=type(None),
16+
response_model=ResourceList[WsPlan],
17+
)
18+
19+
_LIST_IMAGES_OP = APIOperation(
20+
method="GET",
21+
endpoint_template="/metadata/workspace-base-images",
22+
input_model=type(None),
23+
response_model=ResourceList[Image],
24+
)
Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,31 @@
1-
"""
2-
Defines the resource class for the Metadata API endpoints.
3-
"""
4-
51
from typing import List
6-
from ...core import APIOperation, AsyncCallable
2+
from pydantic import Field
3+
from ...core.base import ResourceList
4+
from ...core import AsyncCallable
75
from ...core import ResourceBase
8-
from .models import Datacenter, WsPlan, Image
6+
from .operations import _LIST_DC_OP, _LIST_IMAGES_OP, _LIST_PLANS_OP
7+
from .schemas import Datacenter, WsPlan, Image
98

109

1110
class MetadataResource(ResourceBase):
12-
list_datacenters: AsyncCallable[List[Datacenter]]
13-
"""Fetches a list of all available data centers."""
14-
list_datacenters = APIOperation(
15-
method="GET",
16-
endpoint_template="/metadata/datacenters",
17-
input_model=None,
18-
response_model=List[Datacenter],
11+
list_datacenters_op: AsyncCallable[ResourceList[Datacenter]] = Field(
12+
default=_LIST_DC_OP, exclude=True
1913
)
20-
21-
list_plans: AsyncCallable[List[WsPlan]]
22-
"""Fetches a list of all available workspace plans."""
23-
list_plans = APIOperation(
24-
method="GET",
25-
endpoint_template="/metadata/workspace-plans",
26-
input_model=None,
27-
response_model=List[WsPlan],
14+
list_plans_op: AsyncCallable[ResourceList[WsPlan]] = Field(
15+
default=_LIST_PLANS_OP, exclude=True
2816
)
29-
30-
list_images: AsyncCallable[List[Image]]
31-
"""Fetches a list of all available workspace base images."""
32-
list_images = APIOperation(
33-
method="GET",
34-
endpoint_template="/metadata/workspace-base-images",
35-
input_model=None,
36-
response_model=List[Image],
17+
list_images_op: AsyncCallable[ResourceList[Image]] = Field(
18+
default=_LIST_IMAGES_OP, exclude=True
3719
)
20+
21+
async def list_datacenters(self) -> List[Datacenter]:
22+
result = await self.list_datacenters_op()
23+
return result.root
24+
25+
async def list_plans(self) -> List[WsPlan]:
26+
result = await self.list_plans_op()
27+
return result.root
28+
29+
async def list_images(self) -> List[Image]:
30+
result = await self.list_images_op()
31+
return result.root
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,46 @@
11
from __future__ import annotations
2-
from pydantic import BaseModel
32
import datetime
43

4+
from ...core.base import CamelModel
55

6-
class Datacenter(BaseModel):
6+
7+
class Datacenter(CamelModel):
78
"""Represents a physical data center location."""
89

910
id: int
1011
name: str
1112
city: str
12-
countryCode: str
13+
country_code: str
1314

1415

15-
class Characteristic(BaseModel):
16+
class Characteristic(CamelModel):
1617
"""Defines the resource specifications for a WsPlan."""
1718

1819
id: int
19-
CPU: float
20-
GPU: int
21-
RAM: int
22-
SSD: int
23-
TempStorage: int
24-
onDemand: bool
20+
cpu: float
21+
gpu: int
22+
ram: int
23+
ssd: int
24+
temp_storage: int
25+
on_demand: bool
2526

2627

27-
class WsPlan(BaseModel):
28+
class WsPlan(CamelModel):
2829
"""
2930
Represents a purchasable workspace plan.
3031
"""
3132

3233
id: int
33-
priceUsd: int
34+
price_usd: int
3435
title: str
3536
deprecated: bool
3637
characteristics: Characteristic
37-
maxReplicas: int
38+
max_replicas: int
3839

3940

40-
class Image(BaseModel):
41+
class Image(CamelModel):
4142
"""Represents a runnable workspace base image."""
4243

4344
id: str
4445
name: str
45-
supportedUntil: datetime.datetime
46+
supported_until: datetime.datetime

0 commit comments

Comments
 (0)