|
1 | | -import httpx |
2 | | -from pydantic import BaseModel |
3 | | -from typing import Optional, Any |
4 | | -from functools import partial |
| 1 | +""" |
| 2 | +Codesphere SDK Client |
5 | 3 |
|
6 | | -from .config import settings |
| 4 | +This module provides the main client class, CodesphereSDK. |
| 5 | +""" |
7 | 6 |
|
| 7 | +from .cs_types.rest.http_client import APIHttpClient |
| 8 | +from .resources.metadata.resources import MetadataResource |
| 9 | +from .resources.team.resources import TeamsResource |
| 10 | +from .resources.workspace.resources import WorkspacesResource |
8 | 11 |
|
9 | | -class APIHttpClient: |
10 | | - def __init__(self, base_url: str = "https://codesphere.com/api"): |
11 | | - self._token = settings.token.get_secret_value() |
12 | | - self._base_url = base_url or str(settings.base_url) |
13 | | - self.client: Optional[httpx.AsyncClient] = None |
14 | 12 |
|
15 | | - # Dynamically create get, post, put, patch, delete methods |
16 | | - for method in ["get", "post", "put", "patch", "delete"]: |
17 | | - setattr(self, method, partial(self.request, method.upper())) |
| 13 | +class CodesphereSDK: |
| 14 | + """The main entrypoint for interacting with the `Codesphere Public API <https://codesphere.com/api/swagger-ui/?ref=codesphere.ghost.io#/>`_. |
18 | 15 |
|
19 | | - async def __aenter__(self): |
20 | | - timeout_config = httpx.Timeout( |
21 | | - settings.client_timeout_connect, read=settings.client_timeout_read |
22 | | - ) |
23 | | - self.client = httpx.AsyncClient( |
24 | | - base_url=self._base_url, |
25 | | - headers={"Authorization": f"Bearer {self._token}"}, |
26 | | - timeout=timeout_config, |
27 | | - ) |
28 | | - return self |
| 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 | + """ |
| 37 | + |
| 38 | + teams: TeamsResource |
| 39 | + """Access to the Team API. (e.g., `sdk.teams.list()`)""" |
29 | 40 |
|
30 | | - async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any): |
31 | | - if self.client: |
32 | | - await self.client.aclose() |
| 41 | + workspaces: WorkspacesResource |
| 42 | + """Access to the Workspace API. (e.g., `sdk.workspaces.list()`)""" |
33 | 43 |
|
34 | | - async def request( |
35 | | - self, method: str, endpoint: str, **kwargs: Any |
36 | | - ) -> httpx.Response: |
37 | | - if not self.client: |
38 | | - raise RuntimeError( |
39 | | - "APIHttpClient must be used within an 'async with' statement." |
40 | | - ) |
| 44 | + metadata: MetadataResource |
| 45 | + """Access to the Metadata API. (e.g., `sdk.metadata.list_plans()`)""" |
41 | 46 |
|
42 | | - # If a 'json' payload is a Pydantic model, automatically convert it. |
43 | | - if "json" in kwargs and isinstance(kwargs["json"], BaseModel): |
44 | | - kwargs["json"] = kwargs["json"].model_dump(exclude_none=True) |
| 47 | + def __init__(self): |
| 48 | + self._http_client = APIHttpClient() |
| 49 | + self.teams = TeamsResource(self._http_client) |
| 50 | + self.workspaces = WorkspacesResource(self._http_client) |
| 51 | + self.metadata = MetadataResource(self._http_client) |
45 | 52 |
|
46 | | - print(f"{method} {endpoint} {kwargs}") |
| 53 | + async def open(self): |
| 54 | + """Manually opens the underlying HTTP client session. |
| 55 | +
|
| 56 | + Required for manual lifecycle control when not using `async with`. |
| 57 | +
|
| 58 | + Usage: |
| 59 | + >>> sdk = CodesphereSDK() |
| 60 | + >>> await sdk.open() |
| 61 | + >>> # ... API calls ... |
| 62 | + >>> await sdk.close() |
| 63 | + """ |
| 64 | + await self._http_client.open() |
| 65 | + |
| 66 | + async def close(self): |
| 67 | + """Manually closes the underlying HTTP client session.""" |
| 68 | + await self._http_client.close() |
| 69 | + |
| 70 | + async def __aenter__(self): |
| 71 | + await self.open() |
| 72 | + return self |
47 | 73 |
|
48 | | - response = await self.client.request(method, endpoint, **kwargs) |
49 | | - response.raise_for_status() |
50 | | - return response |
| 74 | + async def __aexit__(self, exc_type, exc_val, exc_tb): |
| 75 | + await self._http_client.close(exc_type, exc_val, exc_tb) |
0 commit comments