open-keypool 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,32 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *.egg-info/
5
+ dist/
6
+ build/
7
+ *.egg
8
+
9
+ # Virtual environments
10
+ .venv/
11
+ venv/
12
+ env/
13
+
14
+ # IDE
15
+ .idea/
16
+ .vscode/
17
+ *.swp
18
+ *.swo
19
+
20
+ # Testing
21
+ .pytest_cache/
22
+ .coverage
23
+ htmlcov/
24
+
25
+ # OS
26
+ .DS_Store
27
+ Thumbs.db
28
+
29
+ # Local Test
30
+ deep_test/
31
+ run_groq_test.py
32
+ demo_qa.py
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Open KeyPool Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,172 @@
1
+ Metadata-Version: 2.5
2
+ Name: open-keypool
3
+ Version: 0.2.0
4
+ Summary: Minimal Python library for pooling and rotating API keys to avoid HTTP 429 rate-limit errors.
5
+ Author: Open KeyPool Contributors
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 Open KeyPool Contributors
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+ License-File: LICENSE
28
+ Keywords: api-keys,key-pool,key-rotation,rate-limiting
29
+ Classifier: Development Status :: 4 - Beta
30
+ Classifier: Intended Audience :: Developers
31
+ Classifier: License :: OSI Approved :: MIT License
32
+ Classifier: Programming Language :: Python :: 3
33
+ Classifier: Programming Language :: Python :: 3.9
34
+ Classifier: Programming Language :: Python :: 3.10
35
+ Classifier: Programming Language :: Python :: 3.11
36
+ Classifier: Programming Language :: Python :: 3.12
37
+ Classifier: Programming Language :: Python :: 3.13
38
+ Requires-Python: >=3.9
39
+ Requires-Dist: cachetools>=5.3.0
40
+ Requires-Dist: httpx>=0.27.0
41
+ Requires-Dist: python-dotenv>=1.0.0
42
+ Provides-Extra: dev
43
+ Requires-Dist: pdoc>=15.0.0; extra == 'dev'
44
+ Requires-Dist: pytest>=8.0.0; extra == 'dev'
45
+ Requires-Dist: respx>=0.22.0; extra == 'dev'
46
+ Description-Content-Type: text/markdown
47
+
48
+ # open-keypool
49
+
50
+ Minimal Python library for pooling and rotating API keys to avoid HTTP 429 rate-limit errors. Provide a list of keys (or pull them from Doppler), choose a rotation strategy (round-robin or least-recently-used), and the pool handles cooldown on rate-limit responses and permanent disablement on invalid keys — all thread-safe.
51
+
52
+ ## Install
53
+
54
+ ```bash
55
+ # From TestPyPI (until published on PyPI):
56
+ pip install --index-url https://test.pypi.org/simple/ open-keypool
57
+ ```
58
+
59
+ ## Quickstart
60
+
61
+ ### Local keys array
62
+
63
+ ```python
64
+ from open_keypool import KeyPool, AllKeysExhaustedError, KeyState
65
+
66
+ pool = KeyPool(keys=["sk-key1", "sk-key2", "sk-key3"], strategy="round_robin")
67
+
68
+ for attempt in range(pool.max_retries):
69
+ key = pool.get_key()
70
+ response = call_your_api(key)
71
+
72
+ # Feed the response — the pool decides success / cooldown / disable
73
+ new_state = pool.handle_response(
74
+ key, response.status_code,
75
+ headers=dict(response.headers),
76
+ body=response.json(),
77
+ )
78
+
79
+ if new_state == KeyState.ACTIVE:
80
+ break # success
81
+ elif new_state == KeyState.COOLDOWN:
82
+ continue # key is rate-limited, rotate to next
83
+ elif new_state == KeyState.DISABLED:
84
+ continue # key is invalid, rotate to next
85
+ ```
86
+
87
+ ### Handle response auto-dispatching
88
+
89
+ `pool.handle_response(key, status_code, headers, body)` introspects the HTTP response and automatically:
90
+
91
+ | Status | Action |
92
+ |---|---|
93
+ | **2xx** | Marks success — clears errors, resets failure count |
94
+ | **429**, **413**, or `"rate_limit_exceeded"` in body | Marks cooldown, reads `Retry-After` header |
95
+ | **401**, **403** | Permanently disables the key |
96
+ | **5xx** | Places on cooldown (transient) |
97
+
98
+ Returns `KeyState` so you can branch on the result.
99
+
100
+ ### Multi-key Doppler pool with status tracking
101
+
102
+ ```python
103
+ import os
104
+ from open_keypool import KeyPool
105
+
106
+ DOPPLER_TOKEN = os.getenv("DOPPLER_TOKEN", "dp.st.YOUR_SERVICE_TOKEN")
107
+
108
+ pool = KeyPool.from_doppler(
109
+ token=DOPPLER_TOKEN,
110
+ project="refactor-ai",
111
+ config="dev",
112
+ key_prefix="GROQ_",
113
+ strategy="round_robin",
114
+ )
115
+
116
+ # Every key's state, error history, and cooldown — safely masked
117
+ for masked_key, info in pool.status().items():
118
+ print(f"{masked_key} state={info['state']} "
119
+ f"http={info.get('last_status_code')} "
120
+ f"err=[{info.get('last_error_code')}] "
121
+ f"failures={info['failure_count']}")
122
+ ```
123
+
124
+ ### Load keys from `.env` file
125
+
126
+ ```python
127
+ from open_keypool import KeyPool
128
+
129
+ # .env contains:
130
+ # TSN_GROQ_KEY=sk-aaa
131
+ # BACKUP_GROQ_KEY=sk-bbb
132
+ # OTHER_SECRET=sk-ccc
133
+
134
+ pool = KeyPool.from_env(suffix="GROQ_KEY")
135
+ # Picks TSN_GROQ_KEY and BACKUP_GROQ_KEY (ends with "GROQ_KEY")
136
+ ```
137
+
138
+ ### Load keys from JSON file
139
+
140
+ ```json
141
+ {
142
+ "TSN_GROQ_KEY": "sk-aaa",
143
+ "BACKUP_GROQ_KEY": "sk-bbb",
144
+ "OTHER_SECRET": "sk-ccc"
145
+ }
146
+ ```
147
+
148
+ ```python
149
+ from open_keypool import KeyPool
150
+
151
+ pool = KeyPool.from_json("keys.json", suffix="GROQ_KEY")
152
+ # Picks TSN_GROQ_KEY and BACKUP_GROQ_KEY (ends with "GROQ_KEY")
153
+ ```
154
+
155
+ ## Constructor parameters
156
+
157
+ | Parameter | Type | Default | Description |
158
+ |---|---|---|---|
159
+ | `keys` | `list[str]` | *required* | Initial API key strings (non-empty). |
160
+ | `max_retries` | `int` | `3` | Max retry count reference for the caller's loop. |
161
+ | `cooldown_seconds` | `int` | `60` | How long a rate-limited key stays in cooldown. |
162
+ | `strategy` | `str` | `"round_robin"` | Rotation strategy: `"round_robin"` or `"lru"`. |
163
+
164
+ ## Doppler caching
165
+
166
+ `KeyPool.from_doppler()` uses an in-memory TTL cache with a 1-hour expiration. On the first call within a process, keys are fetched from Doppler and cached. Subsequent calls within the same hour serve keys from memory without touching the network. After one hour (if the process is still running), the cache entry expires and the next call fetches fresh keys automatically. The cache is never persisted across process restarts — every fresh process starts with an empty cache.
167
+
168
+ Pass `force_refresh=True` to bypass the cache and re-fetch immediately (useful after rotating keys in Doppler when you don't want to wait out the TTL).
169
+
170
+ ## Full API reference
171
+
172
+ [docs/index.html](docs/index.html) — self-contained HTML page with quickstart + class/method documentation generated from docstrings.
@@ -0,0 +1,125 @@
1
+ # open-keypool
2
+
3
+ Minimal Python library for pooling and rotating API keys to avoid HTTP 429 rate-limit errors. Provide a list of keys (or pull them from Doppler), choose a rotation strategy (round-robin or least-recently-used), and the pool handles cooldown on rate-limit responses and permanent disablement on invalid keys — all thread-safe.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ # From TestPyPI (until published on PyPI):
9
+ pip install --index-url https://test.pypi.org/simple/ open-keypool
10
+ ```
11
+
12
+ ## Quickstart
13
+
14
+ ### Local keys array
15
+
16
+ ```python
17
+ from open_keypool import KeyPool, AllKeysExhaustedError, KeyState
18
+
19
+ pool = KeyPool(keys=["sk-key1", "sk-key2", "sk-key3"], strategy="round_robin")
20
+
21
+ for attempt in range(pool.max_retries):
22
+ key = pool.get_key()
23
+ response = call_your_api(key)
24
+
25
+ # Feed the response — the pool decides success / cooldown / disable
26
+ new_state = pool.handle_response(
27
+ key, response.status_code,
28
+ headers=dict(response.headers),
29
+ body=response.json(),
30
+ )
31
+
32
+ if new_state == KeyState.ACTIVE:
33
+ break # success
34
+ elif new_state == KeyState.COOLDOWN:
35
+ continue # key is rate-limited, rotate to next
36
+ elif new_state == KeyState.DISABLED:
37
+ continue # key is invalid, rotate to next
38
+ ```
39
+
40
+ ### Handle response auto-dispatching
41
+
42
+ `pool.handle_response(key, status_code, headers, body)` introspects the HTTP response and automatically:
43
+
44
+ | Status | Action |
45
+ |---|---|
46
+ | **2xx** | Marks success — clears errors, resets failure count |
47
+ | **429**, **413**, or `"rate_limit_exceeded"` in body | Marks cooldown, reads `Retry-After` header |
48
+ | **401**, **403** | Permanently disables the key |
49
+ | **5xx** | Places on cooldown (transient) |
50
+
51
+ Returns `KeyState` so you can branch on the result.
52
+
53
+ ### Multi-key Doppler pool with status tracking
54
+
55
+ ```python
56
+ import os
57
+ from open_keypool import KeyPool
58
+
59
+ DOPPLER_TOKEN = os.getenv("DOPPLER_TOKEN", "dp.st.YOUR_SERVICE_TOKEN")
60
+
61
+ pool = KeyPool.from_doppler(
62
+ token=DOPPLER_TOKEN,
63
+ project="refactor-ai",
64
+ config="dev",
65
+ key_prefix="GROQ_",
66
+ strategy="round_robin",
67
+ )
68
+
69
+ # Every key's state, error history, and cooldown — safely masked
70
+ for masked_key, info in pool.status().items():
71
+ print(f"{masked_key} state={info['state']} "
72
+ f"http={info.get('last_status_code')} "
73
+ f"err=[{info.get('last_error_code')}] "
74
+ f"failures={info['failure_count']}")
75
+ ```
76
+
77
+ ### Load keys from `.env` file
78
+
79
+ ```python
80
+ from open_keypool import KeyPool
81
+
82
+ # .env contains:
83
+ # TSN_GROQ_KEY=sk-aaa
84
+ # BACKUP_GROQ_KEY=sk-bbb
85
+ # OTHER_SECRET=sk-ccc
86
+
87
+ pool = KeyPool.from_env(suffix="GROQ_KEY")
88
+ # Picks TSN_GROQ_KEY and BACKUP_GROQ_KEY (ends with "GROQ_KEY")
89
+ ```
90
+
91
+ ### Load keys from JSON file
92
+
93
+ ```json
94
+ {
95
+ "TSN_GROQ_KEY": "sk-aaa",
96
+ "BACKUP_GROQ_KEY": "sk-bbb",
97
+ "OTHER_SECRET": "sk-ccc"
98
+ }
99
+ ```
100
+
101
+ ```python
102
+ from open_keypool import KeyPool
103
+
104
+ pool = KeyPool.from_json("keys.json", suffix="GROQ_KEY")
105
+ # Picks TSN_GROQ_KEY and BACKUP_GROQ_KEY (ends with "GROQ_KEY")
106
+ ```
107
+
108
+ ## Constructor parameters
109
+
110
+ | Parameter | Type | Default | Description |
111
+ |---|---|---|---|
112
+ | `keys` | `list[str]` | *required* | Initial API key strings (non-empty). |
113
+ | `max_retries` | `int` | `3` | Max retry count reference for the caller's loop. |
114
+ | `cooldown_seconds` | `int` | `60` | How long a rate-limited key stays in cooldown. |
115
+ | `strategy` | `str` | `"round_robin"` | Rotation strategy: `"round_robin"` or `"lru"`. |
116
+
117
+ ## Doppler caching
118
+
119
+ `KeyPool.from_doppler()` uses an in-memory TTL cache with a 1-hour expiration. On the first call within a process, keys are fetched from Doppler and cached. Subsequent calls within the same hour serve keys from memory without touching the network. After one hour (if the process is still running), the cache entry expires and the next call fetches fresh keys automatically. The cache is never persisted across process restarts — every fresh process starts with an empty cache.
120
+
121
+ Pass `force_refresh=True` to bypass the cache and re-fetch immediately (useful after rotating keys in Doppler when you don't want to wait out the TTL).
122
+
123
+ ## Full API reference
124
+
125
+ [docs/index.html](docs/index.html) — self-contained HTML page with quickstart + class/method documentation generated from docstrings.