Skip to content

Commit 27493eb

Browse files
authored
fix(server): add missing username to auth as premium user (jxmorris12#198)
1 parent ea04de4 commit 27493eb

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

docs/source/references/advanced.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,19 @@ Accepted formats:
3737
Authenticating with a premium API key
3838
--------------------------------------
3939

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

4546
.. code-block:: python
4647
4748
import os
4849
import language_tool_python
4950
5051
with language_tool_python.LanguageToolPublicAPI("en-US") as tool:
52+
tool.premium_username = os.environ["LANGUAGETOOL_USERNAME"]
5153
tool.premium_key = os.environ["LANGUAGETOOL_API_KEY"]
5254
print(tool.correct("A sentence with a error in the Hitchhiker's Guide tot he Galaxy"))
5355
# → A sentence with an error in the Hitchhiker's Guide to the Galaxy

src/language_tool_python/server.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ class LanguageTool:
210210
_proxies: dict[str, str] | None
211211
"""A dictionary of proxies for network requests (used in requests to the server)."""
212212

213+
_premium_username: str | None
214+
"""The premium API username for the LanguageTool API."""
215+
213216
_premium_key: str | None
214217
"""The premium API key for the LanguageTool API."""
215218

@@ -291,6 +294,7 @@ def __init__( # noqa: PLR0913 # Too many arguments, but they are all necessary
291294
self._enabled_rules_only = False
292295
self._preferred_variants = set()
293296
self._picky = False
297+
self._premium_username = None
294298
self._premium_key = None
295299

296300
def __enter__(self) -> LanguageTool:
@@ -585,6 +589,24 @@ def picky(self, value: bool) -> None:
585589
"""
586590
self._picky = value
587591

592+
@property
593+
def premium_username(self) -> str | None:
594+
"""Get the premium API username.
595+
596+
:return: The premium API username if set, otherwise None.
597+
:rtype: str | None
598+
"""
599+
return self._premium_username
600+
601+
@premium_username.setter
602+
def premium_username(self, value: str | None) -> None:
603+
"""Set the premium API username.
604+
605+
:param value: The premium API username.
606+
:type value: str | None
607+
"""
608+
self._premium_username = value
609+
588610
@property
589611
def premium_key(self) -> str | None:
590612
"""Get the premium API key.
@@ -740,7 +762,7 @@ def check_matching_regions(
740762

741763
return sorted(all_matches, key=_match_offset)
742764

743-
def _create_params(self, text: str) -> dict[str, str]:
765+
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.
744766
"""Create a dictionary of parameters for the language tool server request.
745767
746768
:param text: The text to be checked.
@@ -782,6 +804,8 @@ def _create_params(self, text: str) -> dict[str, str]:
782804
params["preferredVariants"] = ",".join(self._preferred_variants)
783805
if self._picky:
784806
params["level"] = "picky"
807+
if self._premium_username:
808+
params["username"] = self._premium_username
785809
if self._premium_key:
786810
params["apiKey"] = self._premium_key
787811
return params

0 commit comments

Comments
 (0)