Move from using API key to userIp in making discovery requests. · Charg/google-api-python-client@583d9e4 · GitHub
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

3 files changed

Lines changed: 36 additions & 11 deletions

File tree

‎apiclient/discovery.py‎

Lines changed: 8 additions & 1 deletion

‎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

0 commit comments

Comments
 (0)