|
| 1 | +# fastcomments-python |
| 2 | +The FastComments Python SDK. You can use this to build secure and scalable backend applications that interact with FastComments, or build reactive client applications. |
| 3 | + |
| 4 | +## Installation |
| 5 | + |
| 6 | +### PyPI |
| 7 | + |
| 8 | +```bash |
| 9 | +pip install fastcomments-python |
| 10 | +``` |
| 11 | + |
| 12 | +### Library Contents |
| 13 | + |
| 14 | +This library contains two modules: the generated API client and the core Python library which contains hand-written utilities to make working with the API easier, including SSO support. |
| 15 | + |
| 16 | +- [API Client Library Docs](./client/README.md) |
| 17 | +- [Core Library Docs, Including SSO Examples](./sso/README.md) |
| 18 | + |
| 19 | +### Public vs Secured APIs |
| 20 | + |
| 21 | +For the API client, there are two classes, `DefaultApi` and `PublicApi`. The `DefaultApi` contains methods that require your API key, and `PublicApi` contains API calls that can be made directly from a browser/mobile device/etc without authentication. |
| 22 | + |
| 23 | +## Quick Start |
| 24 | + |
| 25 | +### Using Authenticated APIs (DefaultApi) |
| 26 | + |
| 27 | +**Important:** You must set your API key on the Configuration before making authenticated requests. If you don't, requests will fail with a 401 error. |
| 28 | + |
| 29 | +```python |
| 30 | +from client import ApiClient, Configuration, DefaultApi |
| 31 | +from client.models import CreateAPISSOUserData |
| 32 | + |
| 33 | +# Create and configure the API client |
| 34 | +config = Configuration() |
| 35 | +config.host = "https://fastcomments.com/api" |
| 36 | + |
| 37 | +# REQUIRED: Set your API key (get this from your FastComments dashboard) |
| 38 | +config.api_key = {"ApiKeyAuth": "YOUR_API_KEY_HERE"} |
| 39 | + |
| 40 | +# Create the API instance with the configured client |
| 41 | +api_client = ApiClient(configuration=config) |
| 42 | +api = DefaultApi(api_client) |
| 43 | + |
| 44 | +# Now you can make authenticated API calls |
| 45 | +try: |
| 46 | + # Example: Add an SSO user |
| 47 | + user_data = CreateAPISSOUserData( |
| 48 | + id="user-123", |
| 49 | + email="user@example.com", |
| 50 | + display_name="John Doe" |
| 51 | + ) |
| 52 | + |
| 53 | + response = api.add_sso_user( |
| 54 | + tenant_id="YOUR_TENANT_ID", |
| 55 | + create_apisso_user_data=user_data |
| 56 | + ) |
| 57 | + print(f"User created: {response}") |
| 58 | + |
| 59 | +except Exception as e: |
| 60 | + print(f"Error: {e}") |
| 61 | + # Common errors: |
| 62 | + # - 401: API key is missing or invalid |
| 63 | + # - 400: Request validation failed |
| 64 | +``` |
| 65 | + |
| 66 | +### Using Public APIs (PublicApi) |
| 67 | + |
| 68 | +Public endpoints don't require authentication: |
| 69 | + |
| 70 | +```python |
| 71 | +from client import ApiClient, Configuration, PublicApi |
| 72 | + |
| 73 | +config = Configuration() |
| 74 | +config.host = "https://fastcomments.com/api" |
| 75 | + |
| 76 | +api_client = ApiClient(configuration=config) |
| 77 | +public_api = PublicApi(api_client) |
| 78 | + |
| 79 | +try: |
| 80 | + response = public_api.get_comments_public( |
| 81 | + tenant_id="YOUR_TENANT_ID", |
| 82 | + url_id="page-url-id" |
| 83 | + ) |
| 84 | + print(response) |
| 85 | +except Exception as e: |
| 86 | + print(f"Error: {e}") |
| 87 | +``` |
| 88 | + |
| 89 | +### Using SSO (Single Sign-On) |
| 90 | + |
| 91 | +The SDK includes utilities for generating secure SSO tokens: |
| 92 | + |
| 93 | +```python |
| 94 | +from sso import FastCommentsSSO, SecureSSOUserData |
| 95 | + |
| 96 | +# Create user data |
| 97 | +user_data = SecureSSOUserData( |
| 98 | + user_id="user-123", |
| 99 | + email="user@example.com", |
| 100 | + username="johndoe", |
| 101 | + avatar="https://example.com/avatar.jpg" |
| 102 | +) |
| 103 | + |
| 104 | +# Create SSO instance with your API secret |
| 105 | +sso = FastCommentsSSO.new_secure( |
| 106 | + api_secret="YOUR_API_SECRET", |
| 107 | + user_data=user_data |
| 108 | +) |
| 109 | + |
| 110 | +# Generate the SSO token |
| 111 | +sso_token = sso.create_token() |
| 112 | + |
| 113 | +# Use this token in your frontend or pass to API calls |
| 114 | +print(f"SSO Token: {sso_token}") |
| 115 | +``` |
| 116 | + |
| 117 | +For simple SSO (less secure, for testing): |
| 118 | + |
| 119 | +```python |
| 120 | +from sso import FastCommentsSSO, SimpleSSOUserData |
| 121 | + |
| 122 | +user_data = SimpleSSOUserData( |
| 123 | + user_id="user-123", |
| 124 | + email="user@example.com" |
| 125 | +) |
| 126 | + |
| 127 | +sso = FastCommentsSSO.new_simple(user_data) |
| 128 | +sso_token = sso.create_token() |
| 129 | +``` |
| 130 | + |
| 131 | +### Common Issues |
| 132 | + |
| 133 | +1. **401 "missing-api-key" error**: Make sure you set `config.api_key = {"ApiKeyAuth": "YOUR_KEY"}` before creating the DefaultApi instance. |
| 134 | +2. **Wrong API class**: Use `DefaultApi` for server-side authenticated requests, `PublicApi` for client-side/public requests. |
| 135 | +3. **Import errors**: Make sure you're importing from the correct module: |
| 136 | + - API client: `from client import ...` |
| 137 | + - SSO utilities: `from sso import ...` |
| 138 | + |
| 139 | +## Development |
| 140 | + |
| 141 | +### Running Tests |
| 142 | + |
| 143 | +```bash |
| 144 | +# Set up environment variables |
| 145 | +export FASTCOMMENTS_API_KEY="your-api-key" |
| 146 | +export FASTCOMMENTS_TENANT_ID="your-tenant-id" |
| 147 | + |
| 148 | +# Run tests |
| 149 | +pytest tests/ |
| 150 | +``` |
| 151 | + |
| 152 | +### Regenerating the Client |
| 153 | + |
| 154 | +To regenerate the API client from the latest OpenAPI specification: |
| 155 | + |
| 156 | +```bash |
| 157 | +./update.sh |
| 158 | +``` |
| 159 | + |
| 160 | +This will: |
| 161 | +1. Download the latest OpenAPI spec from a running FastComments server (or use local openapi.yaml) |
| 162 | +2. Generate the Python client code |
| 163 | +3. Flatten the directory structure |
| 164 | +4. Clean up redundant configuration files |
| 165 | + |
| 166 | +## Notes |
| 167 | + |
| 168 | +### Broadcast IDs |
| 169 | + |
| 170 | +You'll see you're supposed to pass a `broadcast_id` in some API calls. When you receive events, you'll get this ID back, so you know to ignore the event if you plan to optimistically apply changes on the client (which you'll probably want to do since it offers the best experience). Pass a UUID here. The ID should be unique enough to not occur twice in a browser session. |
| 171 | + |
| 172 | +## Requirements |
| 173 | + |
| 174 | +- Python >= 3.8 |
| 175 | +- urllib3 >= 1.25.3 |
| 176 | +- python-dateutil >= 2.8.2 |
| 177 | +- pydantic >= 2.0.0 |
| 178 | +- typing-extensions >= 4.0.0 |
| 179 | + |
| 180 | +## License |
| 181 | + |
| 182 | +MIT |
| 183 | + |
| 184 | +## Support |
| 185 | + |
| 186 | +- [Documentation](https://docs.fastcomments.com) |
| 187 | +- [GitHub Issues](https://github.com/fastcomments/fastcomments-python/issues) |
| 188 | +- [FastComments Support](https://fastcomments.com/auth/my-account/help) |
0 commit comments