|
| 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