|
1 |
| -https://pypi.org/project/cocalc-api/ |
| 1 | +# CoCalc Python API Client |
2 | 2 |
|
3 |
| -This is a Python package that provides an API client for https://cocalc.com |
| 3 | +[](https://pypi.org/project/cocalc-api/) |
| 4 | +[](https://www.python.org/downloads/) |
| 5 | + |
| 6 | +This is a Python package that provides an API client for [CoCalc](https://cocalc.com), enabling programmatic access to CoCalc's features including project management, Jupyter execution, file operations, messaging, and organization management. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +```bash |
| 11 | +pip install cocalc-api |
| 12 | +``` |
| 13 | + |
| 14 | +## Quick Start |
| 15 | + |
| 16 | +```python |
| 17 | +import cocalc_api |
| 18 | + |
| 19 | +# Initialize hub client with your API key |
| 20 | +hub = cocalc_api.Hub(api_key="your-api-key") |
| 21 | + |
| 22 | +# Ping the server |
| 23 | +response = hub.system.ping() |
| 24 | +print(f"Server time: {response['now']}") |
| 25 | + |
| 26 | +# List your projects |
| 27 | +projects = hub.projects.get() |
| 28 | +for project in projects: |
| 29 | + print(f"Project: {project['title']} ({project['project_id']})") |
| 30 | +``` |
| 31 | + |
| 32 | +## Features |
| 33 | + |
| 34 | +### Hub Client (Account-Level Operations) |
| 35 | + |
| 36 | +The `Hub` class provides access to account-level operations: |
| 37 | + |
| 38 | +- **System**: Server ping, user search, account name resolution |
| 39 | +- **Projects**: Project management (create, start, stop, add/remove collaborators) |
| 40 | +- **Jupyter**: Execute code using Jupyter kernels in any project or anonymously |
| 41 | +- **Database**: Direct PostgreSQL database queries for advanced operations |
| 42 | +- **Messages**: Send and receive messages between users |
| 43 | +- **Organizations**: Manage organizations, users, and temporary access tokens |
| 44 | +- **Sync**: Access file edit history and synchronization features |
| 45 | + |
| 46 | +### Project Client (Project-Specific Operations) |
| 47 | + |
| 48 | +The `Project` class provides project-specific operations: |
| 49 | + |
| 50 | +- **System**: Execute shell commands and Jupyter code within a specific project |
| 51 | + |
| 52 | +## Authentication |
| 53 | + |
| 54 | +The client supports two types of API keys: |
| 55 | + |
| 56 | +1. **Account API Keys**: Provide full access to all hub functionality |
| 57 | +2. **Project API Keys**: Limited to project-specific operations |
| 58 | + |
| 59 | +Get your API key from [CoCalc Account Settings](https://cocalc.com/settings/account) under "API Keys". |
| 60 | + |
| 61 | +## Architecture |
| 62 | + |
| 63 | +### Package Structure |
| 64 | + |
| 65 | +``` |
| 66 | +src/cocalc_api/ |
| 67 | +├── __init__.py # Package exports (Hub, Project classes) |
| 68 | +├── hub.py # Hub client for account-level operations |
| 69 | +├── project.py # Project client for project-specific operations |
| 70 | +├── api_types.py # TypedDict definitions for API responses |
| 71 | +└── util.py # Utility functions and decorators |
| 72 | +``` |
| 73 | + |
| 74 | +### Design Patterns |
| 75 | + |
| 76 | +- **Decorator-based Methods**: Uses `@api_method()` decorator to automatically convert method calls to API requests |
| 77 | +- **TypedDict Responses**: All API responses use TypedDict for type safety |
| 78 | +- **Error Handling**: Centralized error handling via `handle_error()` utility |
| 79 | +- **HTTP Client**: Uses `httpx` for HTTP requests with authentication |
| 80 | +- **Nested Namespaces**: API organized into logical namespaces (system, projects, jupyter, etc.) |
| 81 | + |
| 82 | +## Development |
| 83 | + |
| 84 | +### Requirements |
| 85 | + |
| 86 | +- Python 3.9+ |
| 87 | +- [uv](https://github.com/astral-sh/uv) package manager |
| 88 | + |
| 89 | +### Setup |
| 90 | + |
| 91 | +```bash |
| 92 | +# Install dependencies |
| 93 | +make install |
| 94 | +# or: uv sync --dev && uv pip install -e . |
| 95 | + |
| 96 | +# Format Python code |
| 97 | +make format |
| 98 | +# or: uv run yapf --in-place --recursive src/ |
| 99 | + |
| 100 | +# Run code quality checks |
| 101 | +make check |
| 102 | +# or: uv run ruff check src/ && uv run mypy src/ && uv run pyright src/ |
| 103 | + |
| 104 | +# Serve documentation locally |
| 105 | +make serve-docs |
| 106 | +# or: uv run mkdocs serve |
| 107 | + |
| 108 | +# Build documentation |
| 109 | +make build-docs |
| 110 | +``` |
| 111 | + |
| 112 | +### Code Quality |
| 113 | + |
| 114 | +This project uses multiple tools for code quality: |
| 115 | + |
| 116 | +- **[YAPF](https://github.com/google/yapf)**: Python code formatter |
| 117 | +- **[Ruff](https://docs.astral.sh/ruff/)**: Fast Python linter |
| 118 | +- **[MyPy](http://mypy-lang.org/)**: Static type checking |
| 119 | +- **[Pyright](https://github.com/microsoft/pyright)**: Additional static type checking |
| 120 | +- **[MkDocs](https://www.mkdocs.org/)**: Documentation generation |
| 121 | + |
| 122 | +### Documentation Standards |
| 123 | + |
| 124 | +All docstrings follow the [Google Style Guide](https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings) for Python docstrings. This includes: |
| 125 | + |
| 126 | +- Clear one-line summary |
| 127 | +- Detailed description when needed |
| 128 | +- Properly formatted `Args:`, `Returns:`, `Raises:`, and `Examples:` sections |
| 129 | +- Type information consistent with function signatures |
| 130 | +- Consistent capitalization and punctuation |
| 131 | + |
| 132 | +Example: |
| 133 | +```python |
| 134 | +def example_function(param1: str, param2: Optional[int] = None) -> dict[str, Any]: |
| 135 | + """ |
| 136 | + Brief description of the function. |
| 137 | +
|
| 138 | + Longer description if needed, explaining the function's behavior, |
| 139 | + side effects, or important usage notes. |
| 140 | +
|
| 141 | + Args: |
| 142 | + param1 (str): Description of the first parameter. |
| 143 | + param2 (Optional[int]): Description of the optional parameter. |
| 144 | +
|
| 145 | + Returns: |
| 146 | + dict[str, Any]: Description of the return value. |
| 147 | +
|
| 148 | + Raises: |
| 149 | + ValueError: When this exception might be raised. |
| 150 | +
|
| 151 | + Examples: |
| 152 | + >>> result = example_function("hello", 42) |
| 153 | + >>> print(result) |
| 154 | + {'status': 'success', 'data': 'hello'} |
| 155 | + """ |
| 156 | +``` |
| 157 | + |
| 158 | +## License |
| 159 | + |
| 160 | +MIT License. See the [LICENSE](LICENSE) file for details. |
| 161 | + |
| 162 | +## Links |
| 163 | + |
| 164 | +- [PyPI Package](https://pypi.org/project/cocalc-api/) |
| 165 | +- [CoCalc Website](https://cocalc.com) |
| 166 | +- [Documentation](https://cocalc.com/api/python) |
| 167 | +- [Source Code](https://github.com/sagemathinc/cocalc/tree/master/src/python/cocalc-api) |
| 168 | +- [Issue Tracker](https://github.com/sagemathinc/cocalc/issues) |
0 commit comments