Skip to content

Commit 65e02be

Browse files
committed
Moved caching to product agnostic dir from bq
1 parent c58e810 commit 65e02be

File tree

4 files changed

+135
-8
lines changed

4 files changed

+135
-8
lines changed

bigquery/samples/async_query.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,8 @@ def main():
7474
query_string = raw_input("Enter the Bigquery SQL Query: ")
7575
batch = raw_input("Run query as batch (y/n)?: ") in (
7676
'True', 'true', 'y', 'Y', 'yes', 'Yes')
77-
78-
num_retries = raw_input(
79-
"Enter number of times to retry in case of 500 error: ")
77+
num_retries = int(raw_input(
78+
"Enter number of times to retry in case of 500 error: "))
8079
interval = raw_input(
8180
"Enter how often to poll the query for completion (seconds): ")
8281

bigquery/samples/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515

1616
# [START get_service]
1717
def get_service():
18-
from discovery_doc import build_and_update
19-
return build_and_update('bigquery', 'v2')
18+
from googleapiclient.discovery import build
19+
from oauth2client.client import GoogleCredentials
20+
credentials = GoogleCredentials.get_application_default()
21+
if credentials.create_scoped_required():
22+
credentials = credentials.create_scoped('https://www.googleapis.com/auth/bigquery')
23+
return build('bigquery','v2', credentials=GoogleCredentials.get_application_default())
2024
# [END get_service]
2125

2226

datastore/ndb/README.md

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,58 @@
1-
appengine-ndb-snippets
2-
======================
1+
## NDB Overview Sample
32

4-
Sample code snippets for NDB.
3+
This is a sample app for Google App Engine that exercises the [NDB Python API](https://cloud.google.com/appengine/docs/python/ndb/).
54

5+
See our other [Google Cloud Platform github
6+
repos](https://github.com/GoogleCloudPlatform) for sample applications and
7+
scaffolding for other python frameworks and use cases.
8+
9+
## Run Locally
10+
1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/), including the [gcloud tool](https://cloud.google.com/sdk/gcloud/), and [gcloud app component](https://cloud.google.com/sdk/gcloud-app).
11+
2. Setup the gcloud tool.
12+
13+
```
14+
gcloud components update app
15+
gcloud auth login
16+
gcloud config set project <your-app-id>
17+
```
18+
You don't need a valid app-id to run locally, but will need a valid id to deploy below.
19+
20+
1. Clone this repo.
21+
22+
```
23+
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
24+
cd python-docs-samples/datastore/ndb/<sub-directory>
25+
```
26+
27+
1. Run this project locally from the command line.
28+
29+
```
30+
gcloud preview app run ./
31+
```
32+
33+
1. Visit the application at [http://localhost:8080](http://localhost:8080).
34+
35+
## Deploying
36+
37+
1. Use the [Cloud Developer Console](https://console.developer.google.com) to create a project/app id. (App id and project id are identical)
38+
2. Configure gcloud with your app id.
39+
40+
```
41+
gcloud config set project <your-app-id>
42+
```
43+
1. Use the [Admin Console](https://appengine.google.com) to view data, queues, and other App Engine specific administration tasks.
44+
1. Use gcloud to deploy your app.
45+
46+
```
47+
gcloud preview app deploy ./
48+
```
49+
50+
1. Congratulations! Your application is now live at your-app-id.appspot.com
51+
52+
## Contributing changes
53+
54+
* See [CONTRIBUTING.md](../../CONTRIBUTING.md)
55+
56+
## Licensing
57+
58+
* See [LICENSE](../../LICENSE)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright 2015, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
#
14+
"""
15+
A module that takes care of caching and updating discovery docs for
16+
google-api-python-clients (until such a feature is integrated).
17+
"""
18+
19+
import json
20+
import os
21+
import time
22+
23+
import httplib2
24+
25+
# [START build_and_update]
26+
27+
RESOURCE_PATH = '..' # look for discovery docs in the parent folder
28+
MAX_AGE = 86400 # update discovery docs older than a day
29+
BIGQUERY_SCOPES = ['https://www.googleapis.com/auth/bigquery']
30+
31+
32+
def build_and_update(api, version):
33+
from oauth2client.client import GoogleCredentials
34+
from googleapiclient.discovery import build_from_document
35+
36+
path = os.path.join(RESOURCE_PATH, '{}.{}'.format(api, version))
37+
try:
38+
age = time.time() - os.path.getmtime(path)
39+
if age > MAX_AGE:
40+
_update_discovery_doc(api, version, path)
41+
except os.error:
42+
_update_discovery_doc(api, version, path)
43+
44+
credentials = GoogleCredentials.get_application_default()
45+
if credentials.create_scoped_required():
46+
credentials = credentials.create_scoped(BIGQUERY_SCOPES)
47+
with open(path, 'r') as discovery_doc:
48+
return build_from_document(discovery_doc.read(),
49+
http=httplib2.Http(),
50+
credentials=credentials)
51+
52+
53+
def _update_discovery_doc(api, version, path):
54+
from apiclient.discovery import DISCOVERY_URI
55+
from apiclient.errors import HttpError
56+
from apiclient.errors import InvalidJsonError
57+
import uritemplate
58+
59+
requested_url = uritemplate.expand(DISCOVERY_URI,
60+
{'api': api, 'apiVersion': version})
61+
resp, content = httplib2.Http().request(requested_url)
62+
if resp.status >= 400:
63+
raise HttpError(resp, content, uri=requested_url)
64+
try:
65+
with open(path, 'w') as discovery_doc:
66+
discovery_json = json.loads(content)
67+
json.dump(discovery_json, discovery_doc)
68+
except ValueError:
69+
raise InvalidJsonError(
70+
'Bad JSON: %s from %s.' % (content, requested_url))
71+
# [END build_and_update]

0 commit comments

Comments
 (0)