|
| 1 | +# Connect RPC in OpenTDF Python SDK |
| 2 | + |
| 3 | +This document describes the Connect RPC implementation in the OpenTDF Python SDK, which provides a modern HTTP-friendly alternative to traditional gRPC. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Connect RPC is a protocol that brings the benefits of RPC to HTTP APIs. It's designed to be: |
| 8 | +- **HTTP-compatible**: Works with standard HTTP infrastructure |
| 9 | +- **Type-safe**: Generated from Protocol Buffer definitions |
| 10 | +- **Efficient**: Binary protocol with JSON fallback |
| 11 | +- **Simple**: Easy to debug and integrate |
| 12 | + |
| 13 | +## Architecture |
| 14 | + |
| 15 | +The SDK uses a two-module structure for protocol buffer generation: |
| 16 | + |
| 17 | +- **Main SDK**: `src/otdf_python/` - Core SDK functionality |
| 18 | +- **Protocol Generation**: `otdf-python-proto/` - Generated Connect RPC and protobuf files |
| 19 | + |
| 20 | +### Generated Files |
| 21 | + |
| 22 | +Connect RPC generates the following files in `otdf-python-proto/src/otdf_python_proto/`: |
| 23 | + |
| 24 | +- `*_connect.py` - Connect RPC client implementations (recommended) |
| 25 | +- `*_pb2.py` - Protocol buffer message definitions |
| 26 | +- `legacy_grpc/*_pb2_grpc.py` - Traditional gRPC clients (backward compatibility) |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +### Client Creation |
| 31 | + |
| 32 | +Connect RPC clients are created using the generated `*_connect.py` modules: |
| 33 | + |
| 34 | +```python |
| 35 | +from otdf_python_proto.kas import kas_connect |
| 36 | +from otdf_python_proto.policy import policy_connect |
| 37 | + |
| 38 | +# Create Connect RPC clients |
| 39 | +kas_client = kas_connect.KeyAccessServiceClient(base_url="https://example.com") |
| 40 | +policy_client = policy_connect.PolicyServiceClient(base_url="https://your-policy-endpoint") |
| 41 | +``` |
| 42 | + |
| 43 | +### Authentication |
| 44 | + |
| 45 | +Connect RPC clients support standard HTTP authentication: |
| 46 | + |
| 47 | +```python |
| 48 | +import httpx |
| 49 | + |
| 50 | +# Create authenticated HTTP client, assuming `token` holds your auth token |
| 51 | +http_client = httpx.Client( |
| 52 | + headers={"Authorization": f"Bearer {token}"} |
| 53 | +) |
| 54 | + |
| 55 | +# Use with Connect RPC client |
| 56 | +kas_client = kas_connect.KeyAccessServiceClient( |
| 57 | + base_url="https://example.com", |
| 58 | + http_client=http_client |
| 59 | +) |
| 60 | +``` |
| 61 | + |
| 62 | +## Regenerating Connect RPC Files |
| 63 | + |
| 64 | +To regenerate the Connect RPC and protobuf files: |
| 65 | + |
| 66 | +```bash |
| 67 | +cd otdf-python-proto |
| 68 | +uv run python scripts/generate_connect_proto.py |
| 69 | +``` |
| 70 | + |
| 71 | +### Download Fresh Proto Files |
| 72 | + |
| 73 | +To download the latest protocol definitions: |
| 74 | + |
| 75 | +```bash |
| 76 | +cd otdf-python-proto |
| 77 | +uv run python scripts/generate_connect_proto.py --download |
| 78 | +``` |
| 79 | + |
| 80 | +### Requirements |
| 81 | + |
| 82 | +- `buf` tool: `brew install bufbuild/buf/buf` (or see [official installation guide](https://buf.build/docs/installation)) |
| 83 | +- Python dependencies managed by `uv` |
| 84 | + |
| 85 | +## Benefits Over gRPC |
| 86 | + |
| 87 | +1. **HTTP Compatibility**: Works with load balancers, proxies, and other HTTP infrastructure |
| 88 | +2. **Debugging**: Easy to inspect with standard HTTP tools |
| 89 | +3. **Flexibility**: Supports both binary and JSON encoding |
| 90 | +4. **Simplicity**: No special HTTP/2 requirements |
| 91 | + |
| 92 | +## Migration from gRPC |
| 93 | + |
| 94 | +If migrating from legacy gRPC clients: |
| 95 | + |
| 96 | +1. Replace `*_pb2_grpc.py` imports with `*_connect.py` |
| 97 | +2. Update client instantiation to use base URLs instead of channels |
| 98 | +3. Leverage HTTP client features for authentication and configuration |
| 99 | + |
| 100 | +## Testing |
| 101 | + |
| 102 | +Connect RPC clients can be easily mocked and tested using standard HTTP testing tools: |
| 103 | + |
| 104 | +```python |
| 105 | +import httpx |
| 106 | +import respx |
| 107 | +from otdf_python_proto.kas import kas_pb2, kas_connect |
| 108 | + |
| 109 | +@respx.mock |
| 110 | +def test_connect_rpc_client(): |
| 111 | + # 1. Create a sample protobuf response message |
| 112 | + expected_response = kas_pb2.RewrapResponse( |
| 113 | + entity_wrapped_key=b'some-unwrapped-key' |
| 114 | + ) |
| 115 | + |
| 116 | + # 2. Mock the correct Connect RPC endpoint URL |
| 117 | + respx.post("https://example.com/kas.AccessService/Rewrap").mock( |
| 118 | + return_value=httpx.Response( |
| 119 | + 200, |
| 120 | + # 3. Return the serialized protobuf message as content |
| 121 | + content=expected_response.SerializeToString(), |
| 122 | + headers={'Content-Type': 'application/proto'} |
| 123 | + ) |
| 124 | + ) |
| 125 | + |
| 126 | + client = kas_connect.KeyAccessServiceClient(base_url="https://example.com") |
| 127 | + # Test client calls... |
| 128 | +``` |
| 129 | + |
| 130 | +## Error Handling |
| 131 | + |
| 132 | +Connect RPC provides structured error handling through standard HTTP status codes and error details: |
| 133 | + |
| 134 | +```python |
| 135 | +from connectrpc import ConnectError |
| 136 | + |
| 137 | +try: |
| 138 | + # Assuming `kas_client` and `request` are defined as in previous examples |
| 139 | + response = kas_client.rewrap(request) |
| 140 | +except ConnectError as e: |
| 141 | + print(f"Connect RPC error: {e.code} - {e.message}") |
| 142 | + # Handle specific error types |
| 143 | +``` |
| 144 | + |
| 145 | +## Performance Considerations |
| 146 | + |
| 147 | +- Use connection pooling with `httpx.Client` for better performance |
| 148 | +- Configure appropriate timeouts for your use case |
| 149 | +- Consider using binary encoding for high-throughput scenarios |
| 150 | + |
| 151 | +## Additional Resources |
| 152 | + |
| 153 | +For more information, see: |
| 154 | +- [Connect RPC Documentation](https://connectrpc.com/docs/) |
| 155 | +- [Connect Python Repository](https://github.com/connectrpc/connect-python) |
| 156 | +- [OpenTDF Platform](https://github.com/opentdf/platform) |
0 commit comments