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
8 changes: 5 additions & 3 deletions docs/source/references/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,19 @@ Accepted formats:
Authenticating with a premium API key
--------------------------------------

If you have a LanguageTool premium key, assign it to
If you have a LanguageTool premium account, assign your username and API key to
:attr:`~language_tool_python.server.LanguageTool.premium_username` and
:attr:`~language_tool_python.server.LanguageTool.premium_key` before calling
:meth:`~language_tool_python.server.LanguageTool.check`. The key is forwarded to the
public API as the ``apiKey`` parameter:
:meth:`~language_tool_python.server.LanguageTool.check`. They are forwarded to the
public API as the ``username`` and ``apiKey`` parameters respectively:

.. code-block:: python

import os
import language_tool_python

with language_tool_python.LanguageToolPublicAPI("en-US") as tool:
tool.premium_username = os.environ["LANGUAGETOOL_USERNAME"]
tool.premium_key = os.environ["LANGUAGETOOL_API_KEY"]
print(tool.correct("A sentence with a error in the Hitchhiker's Guide tot he Galaxy"))
# → A sentence with an error in the Hitchhiker's Guide to the Galaxy
Expand Down
26 changes: 25 additions & 1 deletion src/language_tool_python/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ class LanguageTool:
_proxies: dict[str, str] | None
"""A dictionary of proxies for network requests (used in requests to the server)."""

_premium_username: str | None
"""The premium API username for the LanguageTool API."""

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

Expand Down Expand Up @@ -291,6 +294,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_username = None
self._premium_key = None

def __enter__(self) -> LanguageTool:
Expand Down Expand Up @@ -585,6 +589,24 @@ def picky(self, value: bool) -> None:
"""
self._picky = value

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

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

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

:param value: The premium API username.
:type value: str | None
"""
self._premium_username = value

@property
def premium_key(self) -> str | None:
"""Get the premium API key.
Expand Down Expand Up @@ -740,7 +762,7 @@ def check_matching_regions(

return sorted(all_matches, key=_match_offset)

def _create_params(self, text: str) -> dict[str, str]:
def _create_params(self, text: str) -> dict[str, str]: # noqa: C901 # Too complex, but it needs to handle many different parameters for the server request.
"""Create a dictionary of parameters for the language tool server request.

:param text: The text to be checked.
Expand Down Expand Up @@ -782,6 +804,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_username:
params["username"] = self._premium_username
if self._premium_key:
params["apiKey"] = self._premium_key
return params
Expand Down
Loading