Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/googleapiclient/discovery_cache/__init__.py: 52%
29 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
1# Copyright 2014 Google Inc. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
15"""Caching utility for the discovery document."""
17from __future__ import absolute_import
19import logging
20import os
22LOGGER = logging.getLogger(__name__)
24DISCOVERY_DOC_MAX_AGE = 60 * 60 * 24 # 1 day
25DISCOVERY_DOC_DIR = os.path.join(
26 os.path.dirname(os.path.realpath(__file__)), "documents"
27)
30def autodetect():
31 """Detects an appropriate cache module and returns it.
33 Returns:
34 googleapiclient.discovery_cache.base.Cache, a cache object which
35 is auto detected, or None if no cache object is available.
36 """
37 if "GAE_ENV" in os.environ:
38 try:
39 from . import appengine_memcache
41 return appengine_memcache.cache
42 except Exception:
43 pass
44 try:
45 from . import file_cache
47 return file_cache.cache
48 except Exception:
49 LOGGER.info(
50 "file_cache is only supported with oauth2client<4.0.0", exc_info=False
51 )
52 return None
55def get_static_doc(serviceName, version):
56 """Retrieves the discovery document from the directory defined in
57 DISCOVERY_DOC_DIR corresponding to the serviceName and version provided.
59 Args:
60 serviceName: string, name of the service.
61 version: string, the version of the service.
63 Returns:
64 A string containing the contents of the JSON discovery document,
65 otherwise None if the JSON discovery document was not found.
66 """
68 content = None
69 doc_name = "{}.{}.json".format(serviceName, version)
71 try:
72 with open(os.path.join(DISCOVERY_DOC_DIR, doc_name), "r") as f:
73 content = f.read()
74 except FileNotFoundError:
75 # File does not exist. Nothing to do here.
76 pass
78 return content