|
| 1 | +# RoboSystems Python Client Examples |
| 2 | + |
| 3 | +This directory contains examples demonstrating how to use the RoboSystems Python Client SDK for various common workflows. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +```bash |
| 8 | +# Basic installation (for querying only) |
| 9 | +pip install robosystems-client |
| 10 | + |
| 11 | +# With table ingestion support (for uploading Parquet files) |
| 12 | +pip install robosystems-client[tables] |
| 13 | + |
| 14 | +# Or install all optional features |
| 15 | +pip install robosystems-client[all] |
| 16 | +``` |
| 17 | + |
| 18 | +## Examples |
| 19 | + |
| 20 | +### 1. Basic Query (`basic_query.py`) |
| 21 | + |
| 22 | +**Demonstrates:** Simple querying of an existing graph |
| 23 | + |
| 24 | +The simplest way to get started with the RoboSystems Python SDK. This example shows how to: |
| 25 | +- Initialize the SDK with an API key |
| 26 | +- Execute Cypher queries against a graph |
| 27 | +- Display query results |
| 28 | + |
| 29 | +**Usage:** |
| 30 | +```bash |
| 31 | +python examples/basic_query.py \ |
| 32 | + --api-key YOUR_API_KEY \ |
| 33 | + --graph-id YOUR_GRAPH_ID \ |
| 34 | + --base-url http://localhost:8000 |
| 35 | +``` |
| 36 | + |
| 37 | +**Key Features:** |
| 38 | +- Minimal setup |
| 39 | +- Multiple query examples (count nodes, list labels, sample data) |
| 40 | +- Proper error handling |
| 41 | +- Clean resource management |
| 42 | + |
| 43 | +--- |
| 44 | + |
| 45 | +### 2. End-to-End Workflow (`e2e_workflow.py`) |
| 46 | + |
| 47 | +**Demonstrates:** Complete workflow from user creation to querying |
| 48 | + |
| 49 | +This comprehensive example shows the full RoboSystems workflow: |
| 50 | +- User creation and authentication |
| 51 | +- API key generation |
| 52 | +- Graph creation |
| 53 | +- Parquet file upload to staging tables |
| 54 | +- Table ingestion into the graph |
| 55 | +- Querying the populated graph |
| 56 | + |
| 57 | +**Usage:** |
| 58 | + |
| 59 | +**With new user creation:** |
| 60 | +```bash |
| 61 | +python examples/e2e_workflow.py |
| 62 | +``` |
| 63 | +This will auto-generate credentials and walk through the entire setup. |
| 64 | + |
| 65 | +**With existing API key:** |
| 66 | +```bash |
| 67 | +python examples/e2e_workflow.py --api-key YOUR_API_KEY |
| 68 | +``` |
| 69 | + |
| 70 | +**Custom configuration:** |
| 71 | +```bash |
| 72 | +python examples/e2e_workflow.py \ |
| 73 | + --name "John Doe" \ |
| 74 | + --email "john@example.com" \ |
| 75 | + --password "YourSecurePassword123!" \ |
| 76 | + --base-url http://localhost:8000 |
| 77 | +``` |
| 78 | + |
| 79 | +**Key Features:** |
| 80 | +- Automated user setup |
| 81 | +- Sample data generation (creates Parquet files) |
| 82 | +- **TableIngestClient** extension usage for simplified uploads |
| 83 | +- Progress callbacks |
| 84 | +- LocalStack URL auto-fix for local development |
| 85 | +- Multiple query examples |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +## Extension Features Demonstrated |
| 90 | + |
| 91 | +### TableIngestClient |
| 92 | + |
| 93 | +The examples showcase the new `TableIngestClient` extension which dramatically simplifies the table upload process: |
| 94 | + |
| 95 | +**Before (Raw API calls - 3 steps):** |
| 96 | +```python |
| 97 | +# Step 1: Get presigned upload URL |
| 98 | +response = client.post(f"{base_url}/v1/graphs/{graph_id}/tables/{table_name}/files", ...) |
| 99 | +upload_url = response.json()["upload_url"] |
| 100 | +file_id = response.json()["file_id"] |
| 101 | + |
| 102 | +# Step 2: Upload to S3 |
| 103 | +with open(file_path, "rb") as f: |
| 104 | + s3_client.put(upload_url, data=f.read()) |
| 105 | + |
| 106 | +# Step 3: Update file metadata |
| 107 | +client.patch(f"{base_url}/v1/graphs/{graph_id}/tables/files/{file_id}", ...) |
| 108 | +``` |
| 109 | + |
| 110 | +**After (SDK with TableIngestClient):** |
| 111 | +```python |
| 112 | +from robosystems_client.extensions import UploadOptions |
| 113 | + |
| 114 | +# All 3 steps in one call! |
| 115 | +upload_result = extensions.tables.upload_parquet_file( |
| 116 | + graph_id, |
| 117 | + table_name, |
| 118 | + file_path, |
| 119 | + UploadOptions(on_progress=lambda msg: print(msg)) |
| 120 | +) |
| 121 | +``` |
| 122 | + |
| 123 | +**Additional TableIngestClient Methods:** |
| 124 | +- `list_staging_tables(graph_id)` - List all staging tables |
| 125 | +- `ingest_all_tables(graph_id, options)` - Ingest tables into graph |
| 126 | +- `upload_and_ingest(graph_id, table_name, file_path, ...)` - Upload + ingest in one call |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +## Common Patterns |
| 131 | + |
| 132 | +### Initializing the SDK |
| 133 | + |
| 134 | +```python |
| 135 | +from robosystems_client.extensions import ( |
| 136 | + RoboSystemsExtensions, |
| 137 | + RoboSystemsExtensionConfig, |
| 138 | +) |
| 139 | + |
| 140 | +# With API key |
| 141 | +config = RoboSystemsExtensionConfig( |
| 142 | + base_url="http://localhost:8000", |
| 143 | + headers={"X-API-Key": "your-api-key"} |
| 144 | +) |
| 145 | +extensions = RoboSystemsExtensions(config) |
| 146 | + |
| 147 | +# Always close when done |
| 148 | +extensions.close() |
| 149 | +``` |
| 150 | + |
| 151 | +### Uploading Data |
| 152 | + |
| 153 | +```python |
| 154 | +from robosystems_client.extensions import UploadOptions, IngestOptions |
| 155 | +from pathlib import Path |
| 156 | + |
| 157 | +# Upload a parquet file |
| 158 | +upload_options = UploadOptions( |
| 159 | + on_progress=lambda msg: print(f"Upload: {msg}"), |
| 160 | + fix_localstack_url=True # Auto-fix for local development |
| 161 | +) |
| 162 | + |
| 163 | +result = extensions.tables.upload_parquet_file( |
| 164 | + graph_id="your-graph-id", |
| 165 | + table_name="Entity", |
| 166 | + file_path=Path("data/entities.parquet"), |
| 167 | + options=upload_options |
| 168 | +) |
| 169 | + |
| 170 | +if result.success: |
| 171 | + print(f"✅ Uploaded {result.row_count:,} rows") |
| 172 | +else: |
| 173 | + print(f"❌ Upload failed: {result.error}") |
| 174 | + |
| 175 | +# Ingest the uploaded tables |
| 176 | +ingest_options = IngestOptions( |
| 177 | + ignore_errors=True, # Continue on errors |
| 178 | + rebuild=False, # Don't rebuild existing data |
| 179 | + on_progress=lambda msg: print(f"Ingest: {msg}") |
| 180 | +) |
| 181 | + |
| 182 | +ingest_result = extensions.tables.ingest_all_tables( |
| 183 | + graph_id="your-graph-id", |
| 184 | + options=ingest_options |
| 185 | +) |
| 186 | +``` |
| 187 | + |
| 188 | +### Querying Data |
| 189 | + |
| 190 | +```python |
| 191 | +# Simple query |
| 192 | +result = extensions.query.query( |
| 193 | + graph_id="your-graph-id", |
| 194 | + query="MATCH (n:Entity) RETURN count(n) AS total" |
| 195 | +) |
| 196 | + |
| 197 | +print(f"Total nodes: {result.data[0]['total']}") |
| 198 | + |
| 199 | +# Query with parameters |
| 200 | +result = extensions.query.query( |
| 201 | + graph_id="your-graph-id", |
| 202 | + query="MATCH (n:Entity) WHERE n.ticker = $ticker RETURN n", |
| 203 | + parameters={"ticker": "AAPL"} |
| 204 | +) |
| 205 | + |
| 206 | +for node in result.data: |
| 207 | + print(f"Found: {node}") |
| 208 | +``` |
| 209 | + |
| 210 | +## Environment Variables |
| 211 | + |
| 212 | +You can also use environment variables for configuration: |
| 213 | + |
| 214 | +```bash |
| 215 | +export ROBOSYSTEMS_API_URL="http://localhost:8000" |
| 216 | +export ROBOSYSTEMS_API_KEY="your-api-key" |
| 217 | +export ROBOSYSTEMS_GRAPH_ID="your-graph-id" |
| 218 | +``` |
| 219 | + |
| 220 | +Then reference them in your code: |
| 221 | +```python |
| 222 | +import os |
| 223 | + |
| 224 | +config = RoboSystemsExtensionConfig( |
| 225 | + base_url=os.getenv("ROBOSYSTEMS_API_URL", "http://localhost:8000"), |
| 226 | + headers={"X-API-Key": os.getenv("ROBOSYSTEMS_API_KEY")} |
| 227 | +) |
| 228 | +``` |
| 229 | + |
| 230 | +## Comparison with Raw API Approach |
| 231 | + |
| 232 | +| Operation | Raw httpx Calls | SDK with Extensions | Lines of Code Reduction | |
| 233 | +|-----------|----------------|---------------------|------------------------| |
| 234 | +| Upload file to table | ~25 lines (3 API calls) | ~6 lines (1 method) | **76% reduction** | |
| 235 | +| Query graph | ~10 lines | ~3 lines | **70% reduction** | |
| 236 | +| List tables | ~8 lines | ~2 lines | **75% reduction** | |
| 237 | + |
| 238 | +**Benefits of Using the SDK:** |
| 239 | +- Type-safe model classes |
| 240 | +- Automatic error handling |
| 241 | +- Progress callbacks |
| 242 | +- Connection pooling |
| 243 | +- Cleaner, more maintainable code |
| 244 | +- Built-in support for async operations |
| 245 | + |
| 246 | +## Additional Resources |
| 247 | + |
| 248 | +- **API Documentation:** https://api.robosystems.ai/docs |
| 249 | +- **Main Repository:** https://github.com/RoboSystems/robosystems-python-client |
| 250 | +- **Extension Documentation:** See `robosystems_client/extensions/README.md` |
| 251 | + |
| 252 | +## Getting Help |
| 253 | + |
| 254 | +If you encounter issues: |
| 255 | +1. Check the error message and stack trace |
| 256 | +2. Verify your API key and graph ID are correct |
| 257 | +3. Ensure the API server is running (for local development) |
| 258 | +4. Check the API documentation for endpoint changes |
| 259 | +5. Open an issue on GitHub with a minimal reproducible example |
| 260 | + |
| 261 | +## Contributing |
| 262 | + |
| 263 | +Have a useful example to share? Contributions are welcome! |
| 264 | + |
| 265 | +1. Fork the repository |
| 266 | +2. Create a new example file following the existing patterns |
| 267 | +3. Update this README with your example |
| 268 | +4. Submit a pull request |
| 269 | + |
| 270 | +--- |
| 271 | + |
| 272 | +**Happy coding with RoboSystems! 🤖** |
0 commit comments