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
10 changes: 8 additions & 2 deletions clients/python/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@ def test_create_client_with_auth():
def test_create_client_invalid_token_not_str():
"""Check that create_client raises a TypeError when given non-string auth-token"""
with pytest.raises(TypeError):
create_client("https://tracker.alpha.canada.ca/graphql", 123)
create_client(auth_token=123)


def test_create_client_invalid_token_malformed():
"""Check that create_client raises a ValueError when given malformed auth-token"""
with pytest.raises(ValueError):
create_client("https://tracker.alpha.canada.ca/graphql", "foo")
create_client(auth_token="foo")


def test_create_client_invalid_language():
"""Check that create_client raises a ValueError when given invalid language"""
with pytest.raises(ValueError):
create_client(auth_token=REAL_JWT, language="foo")
5 changes: 3 additions & 2 deletions clients/python/tracker_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ class Client:
and domains their organization(s) control.

:param str url: Tracker GraphQL endpoint, defaults to alpha endpoint.
:param str lang: desired language to get data from Tracker in ('en' or 'fr').
:ivar gql.Client gql_client: gql client instance used to execute queries.
"""

def __init__(self, url="https://tracker.alpha.canada.ca/graphql"):
self.gql_client = create_client(url, auth_token=get_auth_token())
def __init__(self, url="https://tracker.alpha.canada.ca/graphql", language="en"):
self.gql_client = create_client(url, get_auth_token(), language)

def get_organization(self, name):
"""Get an :class:`~tracker_client.organization.Organization` from specified name. You must be a member of that
Expand Down
13 changes: 9 additions & 4 deletions clients/python/tracker_client/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
"""Regex to validate a JWT"""


def create_transport(url, auth_token=None):
def create_transport(url, auth_token, language):
"""Create and return a gql transport object.

Users should rarely, if ever, need to call this.

:param str url: the Tracker GraphQL endpoint url.
:param str auth_token: JWT auth token, omit when initially obtaining the token (default is none).
:param str lang: value to set the http "accept-language" header to.
:return: A gql transport for given url.
:rtype: AIOHTTPTransport
:raises ValueError: if auth_token is not a valid JWT.
Expand All @@ -37,24 +38,28 @@ def create_transport(url, auth_token=None):
if not re.match(_JWT_RE, auth_token):
raise ValueError("auth_token is not a valid JWT")

if language.lower() != 'en' and language.lower() != 'fr':
raise ValueError("Language must be 'en' or 'fr'")

transport = AIOHTTPTransport(
url=url,
headers={"authorization": auth_token},
headers={"authorization": auth_token, 'accept-language': language.lower()},
)

return transport


def create_client(url="https://tracker.alpha.canada.ca/graphql", auth_token=None):
def create_client(url="https://tracker.alpha.canada.ca/graphql", auth_token=None, language='en'):
"""Create and return a gql client object

:param str url: the Tracker GraphQL endpoint url.
:param str auth_token: JWT auth token, omit when initially obtaining the token (default is None).
:param str lang: desired language to get data from Tracker in ('en' or 'fr').
:return: A gql client with AIOHTTPTransport.
:rtype: Client
"""
client = Client(
transport=create_transport(url=url, auth_token=auth_token),
transport=create_transport(url, auth_token, language),
fetch_schema_from_transport=True,
)
return client
Expand Down