Skip to content

Commit 409c43b

Browse files
authored
feat(server): add the possibility to attach an api key (jxmorris12#184)
1 parent 4c069bc commit 409c43b

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ with language_tool_python.LanguageToolPublicAPI("es") as tool:
6161
print(matches)
6262
```
6363

64+
#### Public LanguageTool API with an API key
65+
66+
If you have a LanguageTool API key, set `premium_key` before calling `check()`:
67+
68+
```python
69+
import os
70+
71+
import language_tool_python
72+
73+
with language_tool_python.LanguageToolPublicAPI("en-US") as tool:
74+
tool.premium_key = os.environ["LANGUAGETOOL_API_KEY"]
75+
matches = tool.check("This are bad.")
76+
print(matches)
77+
```
78+
79+
The key is sent to the public API as `apiKey` for each request.
80+
6481
### Your own remote LanguageTool server
6582

6683
```python

language_tool_python/server.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ class LanguageTool:
207207
_proxies: dict[str, str] | None
208208
"""A dictionary of proxies for network requests (used in requests to the server)."""
209209

210+
_premium_key: str | None
211+
"""The premium API key for the LanguageTool API."""
212+
210213
def __init__( # noqa: PLR0913 # Too many arguments, but they are all necessary for configuring the server. Maybe refactor in a future breaking release to use a configuration object instead of individual parameters.
211214
self,
212215
language: str | None = None,
@@ -285,6 +288,7 @@ def __init__( # noqa: PLR0913 # Too many arguments, but they are all necessary
285288
self._enabled_rules_only = False
286289
self._preferred_variants = set()
287290
self._picky = False
291+
self._premium_key = None
288292

289293
def __enter__(self) -> LanguageTool:
290294
"""Enter the runtime context related to this object.
@@ -577,6 +581,24 @@ def picky(self, value: bool) -> None:
577581
"""
578582
self._picky = value
579583

584+
@property
585+
def premium_key(self) -> str | None:
586+
"""Get the premium API key.
587+
588+
:return: The premium API key if set, otherwise None.
589+
:rtype: str | None
590+
"""
591+
return self._premium_key
592+
593+
@premium_key.setter
594+
def premium_key(self, value: str | None) -> None:
595+
"""Set the premium API key.
596+
597+
:param value: The premium API key.
598+
:type value: str | None
599+
"""
600+
self._premium_key = value
601+
580602
@property
581603
def config(self) -> LanguageToolConfig | None:
582604
"""Get the server configuration.
@@ -756,6 +778,8 @@ def _create_params(self, text: str) -> dict[str, str]:
756778
params["preferredVariants"] = ",".join(self._preferred_variants)
757779
if self._picky:
758780
params["level"] = "picky"
781+
if self._premium_key:
782+
params["apiKey"] = self._premium_key
759783
return params
760784

761785
def correct(self, text: str) -> str:

0 commit comments

Comments
 (0)