-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move bigquery 3lo sample on GAE to github.
Also, update it to use webapp2.
- Loading branch information
Jerjou Cheng
committed
Jul 17, 2015
1 parent
34e0721
commit 4dac692
Showing
5 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{"web":{ | ||
"client_id":"NOTE: this is just a placeholder for unit tests. See the README for what to replace this file with.", | ||
"auth_uri":"TODO","token_uri":"TODO","auth_provider_x509_cert_url":"TODO","client_email":"","client_x509_cert_url":"","client_secret":"TODO","redirect_uris":["TODO","TODO"],"javascript_origins":["TODO","TODO"]}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2015 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# [START all] | ||
"""Sample appengine app demonstrating 3-legged oauth.""" | ||
import json | ||
import os | ||
|
||
from googleapiclient.discovery import build | ||
|
||
from oauth2client.appengine import OAuth2DecoratorFromClientSecrets | ||
|
||
import webapp2 | ||
|
||
|
||
# The project id whose datasets you'd like to list | ||
PROJECTID = '<myproject_id>' | ||
|
||
# Create the method decorator for oauth. | ||
decorator = OAuth2DecoratorFromClientSecrets( | ||
os.path.join(os.path.dirname(__file__), 'client_secrets.json'), | ||
scope='https://www.googleapis.com/auth/bigquery') | ||
|
||
# Create the bigquery api client | ||
service = build('bigquery', 'v2') | ||
|
||
|
||
class MainPage(webapp2.RequestHandler): | ||
|
||
@decorator.oauth_required | ||
def get(self): | ||
"""Lists the datasets in PROJECTID""" | ||
http = decorator.http() | ||
datasets = service.datasets() | ||
|
||
response = datasets.list(projectId=PROJECTID).execute(http) | ||
|
||
self.response.out.write('<h3>Datasets.list raw response:</h3>') | ||
self.response.out.write('<pre>%s</pre>' % | ||
json.dumps(response, sort_keys=True, indent=4, | ||
separators=(',', ': '))) | ||
|
||
|
||
# Create the webapp2 application | ||
app = webapp2.WSGIApplication([ | ||
('/', MainPage), | ||
# Create the endpoint to receive oauth flow callbacks | ||
(decorator.callback_path, decorator.callback_handler()) | ||
], debug=True) | ||
# [END all] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"datasets": [ | ||
{ | ||
"datasetReference": { | ||
"datasetId": "test_dataset_java", | ||
"projectId": "cloud-samples-tests" | ||
}, | ||
"id": "cloud-samples-tests:test_dataset_java", | ||
"kind": "bigquery#dataset" | ||
} | ||
], | ||
"etag": "\"ZduQht1tG1odVP6IPm66xfuN2eI/HmGRlylAN_zCB6N4JDeX_XDO0R0\"", | ||
"kind": "bigquery#datasetList" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Copyright 2015 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
import re | ||
|
||
from apiclient.http import HttpMock | ||
|
||
from bigquery.samples.appengine_auth import main | ||
|
||
import mock | ||
|
||
import tests | ||
|
||
import webapp2 | ||
|
||
|
||
RESOURCE_PATH = os.path.join( | ||
os.path.abspath(os.path.dirname(__file__)), 'resources') | ||
|
||
|
||
class TestAuthSample(tests.DatastoreTestbedCase, tests.CloudBaseTest): | ||
|
||
def setUp(self): | ||
tests.DatastoreTestbedCase.setUp(self) | ||
tests.CloudBaseTest.setUp(self) | ||
|
||
self.testbed.init_user_stub() | ||
|
||
def loginUser(self, email='user@example.com', id='123', is_admin=False): | ||
self.testbed.setup_env( | ||
user_email=email, | ||
user_id=id, | ||
user_is_admin='1' if is_admin else '0', | ||
overwrite=True) | ||
|
||
def test_anonymous_get(self): | ||
request = webapp2.Request.blank('/') | ||
response = request.get_response(main.app) | ||
|
||
# Should redirect to login | ||
self.assertEqual(response.status_int, 302) | ||
self.assertRegexpMatches(response.headers['Location'], | ||
r'.*accounts.*Login.*') | ||
|
||
def test_loggedin_get(self): | ||
self.loginUser() | ||
|
||
request = webapp2.Request.blank('/') | ||
response = request.get_response(main.app) | ||
|
||
# Should redirect to login | ||
self.assertEqual(response.status_int, 302) | ||
self.assertRegexpMatches(response.headers['Location'], r'.*oauth2.*') | ||
|
||
@mock.patch.object(main.decorator, 'has_credentials', return_value=True) | ||
def test_oauthed_get(self, *args): | ||
self.loginUser() | ||
|
||
request = webapp2.Request.blank('/') | ||
|
||
mock_http = HttpMock( | ||
os.path.join(RESOURCE_PATH, 'datasets-list.json'), | ||
{'status': '200'}) | ||
with mock.patch.object(main.decorator, 'http', return_value=mock_http): | ||
original_projectid = main.PROJECTID | ||
try: | ||
main.PROJECTID = self.constants['projectId'] | ||
response = request.get_response(main.app) | ||
finally: | ||
main.PROJECTID = original_projectid | ||
|
||
# Should make the api call | ||
self.assertEqual(response.status_int, 200) | ||
self.assertRegexpMatches( | ||
response.body, | ||
re.compile(r'.*datasets.*datasetReference.*etag.*', re.DOTALL)) |