Skip to content

Commit e7b4c8c

Browse files
Client add accept language header (canada-ca#1781)
* Add language arg to create_client() * Add test for language input checking * Add language arg to Client __init__
1 parent b1cc38d commit e7b4c8c

3 files changed

Lines changed: 20 additions & 8 deletions

File tree

clients/python/tests/test_core.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,16 @@ def test_create_client_with_auth():
5151
def test_create_client_invalid_token_not_str():
5252
"""Check that create_client raises a TypeError when given non-string auth-token"""
5353
with pytest.raises(TypeError):
54-
create_client("https://tracker.alpha.canada.ca/graphql", 123)
54+
create_client(auth_token=123)
5555

5656

5757
def test_create_client_invalid_token_malformed():
5858
"""Check that create_client raises a ValueError when given malformed auth-token"""
5959
with pytest.raises(ValueError):
60-
create_client("https://tracker.alpha.canada.ca/graphql", "foo")
60+
create_client(auth_token="foo")
61+
62+
63+
def test_create_client_invalid_language():
64+
"""Check that create_client raises a ValueError when given invalid language"""
65+
with pytest.raises(ValueError):
66+
create_client(auth_token=REAL_JWT, language="foo")

clients/python/tracker_client/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ class Client:
2020
and domains their organization(s) control.
2121
2222
:param str url: Tracker GraphQL endpoint, defaults to alpha endpoint.
23+
:param str lang: desired language to get data from Tracker in ('en' or 'fr').
2324
:ivar gql.Client gql_client: gql client instance used to execute queries.
2425
"""
2526

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

2930
def get_organization(self, name):
3031
"""Get an :class:`~tracker_client.organization.Organization` from specified name. You must be a member of that

clients/python/tracker_client/core.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
"""Regex to validate a JWT"""
1313

1414

15-
def create_transport(url, auth_token=None):
15+
def create_transport(url, auth_token, language):
1616
"""Create and return a gql transport object.
1717
1818
Users should rarely, if ever, need to call this.
1919
2020
:param str url: the Tracker GraphQL endpoint url.
2121
:param str auth_token: JWT auth token, omit when initially obtaining the token (default is none).
22+
:param str lang: value to set the http "accept-language" header to.
2223
:return: A gql transport for given url.
2324
:rtype: AIOHTTPTransport
2425
:raises ValueError: if auth_token is not a valid JWT.
@@ -37,24 +38,28 @@ def create_transport(url, auth_token=None):
3738
if not re.match(_JWT_RE, auth_token):
3839
raise ValueError("auth_token is not a valid JWT")
3940

41+
if language.lower() != 'en' and language.lower() != 'fr':
42+
raise ValueError("Language must be 'en' or 'fr'")
43+
4044
transport = AIOHTTPTransport(
4145
url=url,
42-
headers={"authorization": auth_token},
46+
headers={"authorization": auth_token, 'accept-language': language.lower()},
4347
)
4448

4549
return transport
4650

4751

48-
def create_client(url="https://tracker.alpha.canada.ca/graphql", auth_token=None):
52+
def create_client(url="https://tracker.alpha.canada.ca/graphql", auth_token=None, language='en'):
4953
"""Create and return a gql client object
5054
5155
:param str url: the Tracker GraphQL endpoint url.
5256
:param str auth_token: JWT auth token, omit when initially obtaining the token (default is None).
57+
:param str lang: desired language to get data from Tracker in ('en' or 'fr').
5358
:return: A gql client with AIOHTTPTransport.
5459
:rtype: Client
5560
"""
5661
client = Client(
57-
transport=create_transport(url=url, auth_token=auth_token),
62+
transport=create_transport(url, auth_token, language),
5863
fetch_schema_from_transport=True,
5964
)
6065
return client

0 commit comments

Comments
 (0)