Skip to content

Commit b8580ec

Browse files
committed
Add a User-Agent header.
1 parent ef8018d commit b8580ec

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ Generating documentation:
22

33
$ sphinx-build -b html docs docs/html
44

5-
65
Installing dependencies:
76

8-
$ [sudo] pip install requests
7+
$ pip install requests responses
98

109
Running tests:
1110

‎googlemaps/common.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import requests
77

88

9+
_VERSION = "0.1"
10+
_USER_AGENT = "GoogleGeoApiClientPython/%s" % _VERSION
11+
912
class Context(object):
1013
"""Holds state between requests, such as credentials (API key), timeout
1114
settings"""
@@ -52,6 +55,7 @@ def _get(ctx, url, params):
5255
params["key"] = ctx.key
5356
resp = requests.get(
5457
"https://maps.googleapis.com" + url,
58+
headers={"User-Agent": _USER_AGENT},
5559
verify=True, # NOTE(cbro): verify SSL certs.
5660
params=params)
5761

‎test/test_useragent.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# TODO: move to test_common when PR #8 is merged.
2+
3+
import googlemaps
4+
import unittest
5+
import responses
6+
7+
class UATest(unittest.TestCase):
8+
9+
@responses.activate
10+
def test_ua_sent(self):
11+
responses.add(responses.GET,
12+
"https://maps.googleapis.com/maps/api/geocode/json",
13+
body='{"status":"OK","results":[]}',
14+
status=200,
15+
content_type='application/json')
16+
17+
ctx = googlemaps.Context(key="AIzaasdf")
18+
googlemaps.geocode(ctx, "Sesame St.")
19+
20+
self.assertEquals(1, len(responses.calls))
21+
user_agent = responses.calls[0].request.headers["User-Agent"]
22+
self.assertTrue(user_agent.startswith("GoogleGeoApiClientPython"))

0 commit comments

Comments
 (0)