Skip to content

Commit 6184b35

Browse files
committed
Merge pull request googlemaps#129 from rbouchez/add_channel
Fix: channel argument should always be allowed when there is a client_id
2 parents 02a6ec7 + 2b6f433 commit 6184b35

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

googlemaps/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __init__(self, key=None, client_id=None, client_secret=None,
6666
:param channel: (for Maps API for Work customers) When set, a channel
6767
parameter with this value will be added to the requests.
6868
This can be used for tracking purpose.
69-
Can not be used with a Maps API key.
69+
Can only be used with a Maps API client ID.
7070
:type channel: str
7171
7272
:param timeout: Combined connect and read timeout for HTTP requests, in
@@ -112,9 +112,9 @@ def __init__(self, key=None, client_id=None, client_secret=None,
112112
raise ValueError("Invalid API key provided.")
113113

114114
if channel:
115-
if key:
116-
raise ValueError("The channel argument can not be used with an "
117-
"API key")
115+
if not client_id:
116+
raise ValueError("The channel argument must be used with a "
117+
"client ID")
118118
if not re.match("^[a-zA-Z0-9._-]*$", channel):
119119
raise ValueError("The channel argument must be an ASCII "
120120
"alphanumeric string. The period (.), underscore (_)"

test/test_client.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -217,31 +217,35 @@ def __call__(self, req):
217217

218218
self.assertEqual(2, len(responses.calls))
219219

220-
def test_channel_with_api_key(self):
220+
def test_channel_without_client_id(self):
221221
with self.assertRaises(ValueError):
222222
client = googlemaps.Client(key="AIzaasdf", channel="mychannel")
223223

224224
def test_invalid_channel(self):
225+
# Cf. limitations here:
226+
# https://developers.google.com/maps/premium/reports
227+
# /usage-reports#channels
225228
with self.assertRaises(ValueError):
226-
client = googlemaps.Client(client_id="foo", client_secret="a2V5", channel="auieauie$? ")
227-
228-
@responses.activate
229-
def test_channel_with_client_id(self):
230-
responses.add(responses.GET,
231-
"https://maps.googleapis.com/maps/api/geocode/json",
232-
body='{"status":"OK","results":[]}',
233-
status=200,
234-
content_type="application/json")
235-
236-
client = googlemaps.Client(client_id="foo", client_secret="a2V5", channel="MyChannel_1")
237-
client.geocode("Sesame St.")
238-
239-
self.assertEqual(1, len(responses.calls))
240-
241-
# Check ordering of parameters.
242-
self.assertIn("address=Sesame+St.&channel=MyChannel_1&client=foo&signature",
243-
responses.calls[0].request.url)
244-
self.assertURLEqual("https://maps.googleapis.com/maps/api/geocode/json?"
245-
"address=Sesame+St.&channel=MyChannel_1&client=foo&"
246-
"signature=5s4Hw2AitGZlkipugXkjPuxhmME=",
247-
responses.calls[0].request.url)
229+
client = googlemaps.Client(client_id="foo", client_secret="a2V5",
230+
channel="auieauie$? ")
231+
232+
def test_auth_url_with_channel(self):
233+
client = googlemaps.Client(key="AIzaasdf",
234+
client_id="foo",
235+
client_secret="a2V5",
236+
channel="MyChannel_1")
237+
238+
# Check ordering of parameters + signature.
239+
auth_url = client._generate_auth_url("/test",
240+
{"param": "param"},
241+
accepts_clientid=True)
242+
self.assertEqual(auth_url, "/test?param=param"
243+
"&channel=MyChannel_1"
244+
"&client=foo"
245+
"&signature=OH18GuQto_mEpxj99UimKskvo4k=")
246+
247+
# Check if added to requests to API with accepts_clientid=False
248+
auth_url = client._generate_auth_url("/test",
249+
{"param": "param"},
250+
accepts_clientid=False)
251+
self.assertEqual(auth_url, "/test?param=param&key=AIzaasdf")

0 commit comments

Comments
 (0)