|
| 1 | +""" |
| 2 | +Type-safe configuration for Linkbreakers SDK |
| 3 | +
|
| 4 | +This module provides a type-safe Configuration class that prevents the common |
| 5 | +mistake of using `api_key` instead of `access_token`. |
| 6 | +
|
| 7 | +✅ CORRECT: Use `access_token` for Bearer token authentication |
| 8 | +❌ INCORRECT: Using `api_key` is not supported and will raise an error |
| 9 | +
|
| 10 | +For more information, see: |
| 11 | +https://linkbreakers.com/help/article/api-authentication-accesstoken-vs-apikey |
| 12 | +""" |
| 13 | + |
| 14 | +from typing import Optional |
| 15 | +import warnings |
| 16 | + |
| 17 | + |
| 18 | +class LinkbreakersConfiguration: |
| 19 | + """ |
| 20 | + Type-safe configuration class that only accepts Bearer token authentication |
| 21 | +
|
| 22 | + This wrapper prevents you from accidentally using the wrong authentication |
| 23 | + parameter. The Linkbreakers API requires Bearer token authentication via |
| 24 | + the `access_token` parameter. |
| 25 | +
|
| 26 | + Args: |
| 27 | + access_token: Your Linkbreakers API token (Bearer token) |
| 28 | + Get this from: https://app.linkbreakers.com/settings/api |
| 29 | + host: API base URL (default: https://api.linkbreakers.com) |
| 30 | +
|
| 31 | + Example: |
| 32 | + >>> config = LinkbreakersConfiguration( |
| 33 | + ... access_token='your-api-token', |
| 34 | + ... host='https://api.linkbreakers.com' |
| 35 | + ... ) |
| 36 | +
|
| 37 | + Raises: |
| 38 | + ValueError: If you try to pass `api_key` instead of `access_token` |
| 39 | + """ |
| 40 | + |
| 41 | + def __init__( |
| 42 | + self, |
| 43 | + access_token: str, |
| 44 | + host: str = 'https://api.linkbreakers.com', |
| 45 | + **kwargs |
| 46 | + ): |
| 47 | + """ |
| 48 | + Initialize Linkbreakers configuration |
| 49 | +
|
| 50 | + Args: |
| 51 | + access_token: Your Linkbreakers API token (Bearer token) |
| 52 | + host: API base URL (optional, defaults to production) |
| 53 | + **kwargs: Additional parameters (for internal use) |
| 54 | +
|
| 55 | + Raises: |
| 56 | + ValueError: If `api_key` is passed instead of `access_token` |
| 57 | + """ |
| 58 | + # Explicitly check for the wrong parameter |
| 59 | + if 'api_key' in kwargs: |
| 60 | + raise ValueError( |
| 61 | + "❌ INCORRECT: Do not use `api_key`. " |
| 62 | + "Use `access_token` instead for Bearer token authentication.\n" |
| 63 | + "See: https://linkbreakers.com/help/article/api-authentication-accesstoken-vs-apikey" |
| 64 | + ) |
| 65 | + |
| 66 | + # Store configuration |
| 67 | + self.access_token = access_token |
| 68 | + self.host = host |
| 69 | + self._extra_kwargs = kwargs |
| 70 | + |
| 71 | + def to_openapi_config(self): |
| 72 | + """ |
| 73 | + Convert to OpenAPI Generator Configuration object |
| 74 | +
|
| 75 | + Returns: |
| 76 | + Configuration: OpenAPI Generator configuration object |
| 77 | + """ |
| 78 | + from linkbreakers.configuration import Configuration as OpenAPIConfiguration |
| 79 | + |
| 80 | + return OpenAPIConfiguration( |
| 81 | + access_token=self.access_token, |
| 82 | + host=self.host, |
| 83 | + **self._extra_kwargs |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +# Monkey-patch warning for direct Configuration usage |
| 88 | +def _create_safe_configuration_class(): |
| 89 | + """Create a Configuration class that warns about unsafe usage""" |
| 90 | + from linkbreakers.configuration import Configuration as OriginalConfiguration |
| 91 | + |
| 92 | + class SafeConfiguration(OriginalConfiguration): |
| 93 | + """ |
| 94 | + Safe Configuration wrapper with validation |
| 95 | +
|
| 96 | + This class extends the auto-generated Configuration to add validation |
| 97 | + that prevents common authentication mistakes. |
| 98 | + """ |
| 99 | + |
| 100 | + def __init__(self, *args, **kwargs): |
| 101 | + # Check if user is trying to use api_key dictionary |
| 102 | + if 'api_key' in kwargs and isinstance(kwargs.get('api_key'), dict): |
| 103 | + warnings.warn( |
| 104 | + "⚠️ Using `api_key={'ApiKeyAuth': '...'}` is deprecated. " |
| 105 | + "Please use `access_token='your-token'` instead for Bearer authentication. " |
| 106 | + "See: https://linkbreakers.com/help/article/api-authentication-accesstoken-vs-apikey", |
| 107 | + DeprecationWarning, |
| 108 | + stacklevel=2 |
| 109 | + ) |
| 110 | + |
| 111 | + # Check if they passed access_token - this is correct |
| 112 | + if 'access_token' not in kwargs and 'api_key' not in kwargs: |
| 113 | + raise ValueError( |
| 114 | + "❌ Missing authentication parameter. " |
| 115 | + "You must provide `access_token='your-token'` for Bearer authentication.\n" |
| 116 | + "See: https://linkbreakers.com/help/article/api-authentication-accesstoken-vs-apikey" |
| 117 | + ) |
| 118 | + |
| 119 | + super().__init__(*args, **kwargs) |
| 120 | + |
| 121 | + return SafeConfiguration |
| 122 | + |
| 123 | + |
| 124 | +# Export the safe Configuration class as the default |
| 125 | +Configuration = _create_safe_configuration_class() |
| 126 | + |
| 127 | +__all__ = ['LinkbreakersConfiguration', 'Configuration'] |
0 commit comments