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
14 changes: 13 additions & 1 deletion pygitguardian/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(
:param user_agent: user agent to identify requests, defaults to ""
:param timeout: request timeout, defaults to 20s

:raises ValueError: if the protocol is invalid
:raises ValueError: if the protocol or the api_key is invalid
"""

if isinstance(base_uri, str):
Expand All @@ -93,6 +93,18 @@ def __init__(
if not isinstance(api_key, str):
raise TypeError("api_key is not a string")

try:
# The requests module encodes headers in latin-1, if api_key contains
# characters which cannot be encoded in latin-1, the raised exception is
# going to be very obscure. Catch the problem early to raise a clearer
# exception.
# See https://github.com/GitGuardian/ggshield/issues/101
api_key.encode("latin-1")
except UnicodeEncodeError:
raise ValueError(
"Invalid value for API Key: must be only latin-1 characters."
)

self.base_uri = base_uri
self.api_key = api_key
self.session = session if isinstance(session, Session) else Session()
Expand Down
8 changes: 8 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@
None,
id="Custom headers",
),
pytest.param(
"–––––––FILL-ME–––––––––",
"https://api.gitguardian.com/",
"None",
30.0,
ValueError,
id="U+2013 dash characters in API key",
),
],
)
def test_client_creation(
Expand Down