Skip to content

Commit 583d9e4

Browse files
committed
Move from using API key to userIp in making discovery requests.
Reviewed in http://codereview.appspot.com/5031048/
1 parent 0c26218 commit 583d9e4

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

apiclient/discovery.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,14 @@ def build(serviceName, version,
155155
http = httplib2.Http()
156156

157157
requested_url = uritemplate.expand(discoveryServiceUrl, params)
158-
requested_url = _add_query_parameter(requested_url, 'key', developerKey)
158+
159+
# REMOTE_ADDR is defined by the CGI spec [RFC3875] as the environment variable
160+
# that contains the network address of the client sending the request. If it
161+
# exists then add that to the request for the discovery document to avoid
162+
# exceeding the quota on discovery requests.
163+
if 'REMOTE_ADDR' in os.environ:
164+
requested_url = _add_query_parameter(requested_url, 'userIp',
165+
os.environ['REMOTE_ADDR'])
159166
logging.info('URL being requested: %s' % requested_url)
160167

161168
resp, content = http.request(requested_url)

samples/api-python-client-doc/main.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,26 @@
3838
from google.appengine.ext.webapp import util
3939

4040

41+
DISCOVERY_URI = 'https://www.googleapis.com/discovery/v1/apis?preferred=true'
42+
43+
44+
def get_directory_doc():
45+
http = httplib2.Http(memcache)
46+
ip = os.environ.get('REMOTE_ADDR', None)
47+
uri = DISCOVERY_URI
48+
if ip:
49+
uri += ('&userIp=' + ip)
50+
resp, content = http.request(uri)
51+
directory = simplejson.loads(content)['items']
52+
return directory
53+
54+
4155
class MainHandler(webapp.RequestHandler):
4256
"""Handles serving the main landing page.
4357
"""
4458

4559
def get(self):
46-
http = httplib2.Http(memcache)
47-
resp, content = http.request('https://www.googleapis.com/discovery/v1/apis?preferred=true')
48-
directory = simplejson.loads(content)['items']
60+
directory = get_directory_doc()
4961
for item in directory:
5062
item['title'] = item.get('title', item.get('description', ''))
5163
path = os.path.join(os.path.dirname(__file__), 'index.html')
@@ -60,9 +72,7 @@ class GadgetHandler(webapp.RequestHandler):
6072
"""
6173

6274
def get(self):
63-
http = httplib2.Http(memcache)
64-
resp, content = http.request('https://www.googleapis.com/discovery/v1/apis?preferred=true')
65-
directory = simplejson.loads(content)['items']
75+
directory = get_directory_doc()
6676
for item in directory:
6777
item['title'] = item.get('title', item.get('description', ''))
6878
path = os.path.join(os.path.dirname(__file__), 'gadget.html')
@@ -86,7 +96,8 @@ class ResourceHandler(webapp.RequestHandler):
8696
"""
8797

8898
def get(self, service_name, version, collection):
89-
resource = discovery.build(service_name, version)
99+
http = httplib2.Http(memcache)
100+
resource = discovery.build(service_name, version, http=http)
90101
# descend the object path
91102
if collection:
92103
path = collection.split('/')

tests/test_discovery.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
2525

26+
import copy
2627
import httplib2
2728
import os
2829
import unittest
@@ -83,10 +84,16 @@ def test_building_with_base_remembers_base(self):
8384

8485

8586
class DiscoveryFromHttp(unittest.TestCase):
87+
def setUp(self):
88+
self.old_environ = copy.copy(os.environ)
8689

87-
def test_api_key_is_added_to_discovery_uri(self):
90+
def tearDown(self):
91+
os.environ = self.old_environ
92+
93+
def test_userip_is_added_to_discovery_uri(self):
8894
# build() will raise an HttpError on a 400, use this to pick the request uri
8995
# out of the raised exception.
96+
os.environ['REMOTE_ADDR'] = '10.0.0.1'
9097
try:
9198
http = HttpMockSequence([
9299
({'status': '400'}, file(datafile('zoo.json'), 'r').read()),
@@ -95,9 +102,9 @@ def test_api_key_is_added_to_discovery_uri(self):
95102
discoveryServiceUrl='http://example.com')
96103
self.fail('Should have raised an exception.')
97104
except HttpError, e:
98-
self.assertEqual(e.uri, 'http://example.com?key=foo')
105+
self.assertEqual(e.uri, 'http://example.com?userIp=10.0.0.1')
99106

100-
def test_api_key_of_none_is_not_added_to_discovery_uri(self):
107+
def test_userip_missing_is_not_added_to_discovery_uri(self):
101108
# build() will raise an HttpError on a 400, use this to pick the request uri
102109
# out of the raised exception.
103110
try:

0 commit comments

Comments
 (0)