Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ with language_tool_python.LanguageToolPublicAPI("es") as tool:
print(matches)
```

#### Public LanguageTool API with an API key

If you have a LanguageTool API key, set `premium_key` before calling `check()`:

```python
import os

import language_tool_python

with language_tool_python.LanguageToolPublicAPI("en-US") as tool:
tool.premium_key = os.environ["LANGUAGETOOL_API_KEY"]
matches = tool.check("This are bad.")
print(matches)
```

The key is sent to the public API as `apiKey` for each request.

### Your own remote LanguageTool server

```python
Expand Down
24 changes: 24 additions & 0 deletions language_tool_python/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ class LanguageTool:
_proxies: dict[str, str] | None
"""A dictionary of proxies for network requests (used in requests to the server)."""

_premium_key: str | None
"""The premium API key for the LanguageTool API."""

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.
self,
language: str | None = None,
Expand Down Expand Up @@ -285,6 +288,7 @@ def __init__( # noqa: PLR0913 # Too many arguments, but they are all necessary
self._enabled_rules_only = False
self._preferred_variants = set()
self._picky = False
self._premium_key = None

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

@property
def premium_key(self) -> str | None:
"""Get the premium API key.

:return: The premium API key if set, otherwise None.
:rtype: str | None
"""
return self._premium_key

@premium_key.setter
def premium_key(self, value: str | None) -> None:
"""Set the premium API key.

:param value: The premium API key.
:type value: str | None
"""
self._premium_key = value

@property
def config(self) -> LanguageToolConfig | None:
"""Get the server configuration.
Expand Down Expand Up @@ -756,6 +778,8 @@ def _create_params(self, text: str) -> dict[str, str]:
params["preferredVariants"] = ",".join(self._preferred_variants)
if self._picky:
params["level"] = "picky"
if self._premium_key:
params["apiKey"] = self._premium_key
return params

def correct(self, text: str) -> str:
Expand Down
Loading