| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """This module is used to set up Remote API to use services on App Engine. | 5 """This module is used to set up Remote API to use services on App Engine. |
| 6 | 6 |
| 7 After setup, available services include datastore, task queue, etc. | 7 After setup, available services include datastore, task queue, etc. |
| 8 You may be prompted for credentials during the remote query or the like. | 8 You may be prompted for credentials during the remote query or the like. |
| 9 And you could use Remote API only when you are one of the project members. | 9 And you could use Remote API only when you are one of the project members. |
| 10 | 10 |
| 11 For detail on usage of Remote API, please refer to: | 11 For detail on usage of Remote API, please refer to: |
| 12 https://cloud.google.com/appengine/docs/python/tools/remoteapi | 12 https://cloud.google.com/appengine/docs/python/tools/remoteapi |
| 13 """ | 13 """ |
| 14 | 14 |
| 15 import os | |
| 16 import socket | 15 import socket |
| 17 import sys | |
| 18 | 16 |
| 19 | 17 import script_util |
| 20 _FINDIT_ROOT_DIR = os.path.join(os.path.dirname(__file__), os.path.pardir) | 18 script_util.SetUpSystemPaths() |
| 21 _THIRD_PARTY_DIR = os.path.join(_FINDIT_ROOT_DIR, 'third_party') | |
| 22 _APPNGINE_SDK_DIR = os.path.join(_FINDIT_ROOT_DIR, os.path.pardir, | |
| 23 os.path.pardir, os.path.pardir, | |
| 24 'google_appengine') | |
| 25 | |
| 26 | |
| 27 # Add App Engine SDK dir to sys.path. | |
| 28 sys.path.insert(1, _APPNGINE_SDK_DIR) | |
| 29 sys.path.insert(1, _THIRD_PARTY_DIR) | |
| 30 import dev_appserver | |
| 31 dev_appserver.fix_sys_path() | |
| 32 | |
| 33 | |
| 34 # Add Findit root dir to sys.path so that modules in Findit would be available. | |
| 35 sys.path.insert(1, _FINDIT_ROOT_DIR) | |
| 36 | |
| 37 | 19 |
| 38 from google.appengine.api import urlfetch | 20 from google.appengine.api import urlfetch |
| 39 from google.appengine.ext import ndb | 21 from google.appengine.ext import ndb |
| 40 from google.appengine.ext.remote_api import remote_api_stub | 22 from google.appengine.ext.remote_api import remote_api_stub |
| 41 | 23 |
| 42 | 24 |
| 43 def SetTimeoutForUrlOperations(url_blocking_operations_timeout=600): | 25 def SetTimeoutForUrlOperations(url_blocking_operations_timeout=600): |
| 44 """Set timeout for url operations (socket, appengine db).""" | 26 """Set timeout for url operations (socket, appengine db).""" |
| 45 socket.setdefaulttimeout(url_blocking_operations_timeout) | 27 socket.setdefaulttimeout(url_blocking_operations_timeout) |
| 46 urlfetch.set_default_fetch_deadline(url_blocking_operations_timeout) | 28 urlfetch.set_default_fetch_deadline(url_blocking_operations_timeout) |
| 47 | 29 |
| 48 | 30 |
| 49 def EnableRemoteApi(app_id='findit-for-me'): | 31 def EnableRemoteApi(app_id='findit-for-me'): |
| 50 """Enable appengine services through remote API. | 32 """Enable appengine services through remote API. |
| 51 | 33 |
| 52 Args: | 34 Args: |
| 53 app_id (str): The appengine ID without '.appspot.com', eg. findit-for-me. | 35 app_id (str): The appengine ID without '.appspot.com', eg. findit-for-me. |
| 54 """ | 36 """ |
| 55 if hasattr(EnableRemoteApi, app_id): | 37 if hasattr(EnableRemoteApi, app_id): |
| 56 return | 38 return |
| 57 | 39 |
| 58 SetTimeoutForUrlOperations() | 40 SetTimeoutForUrlOperations() |
| 59 | 41 |
| 60 remote_api_stub.ConfigureRemoteApiForOAuth( | 42 remote_api_stub.ConfigureRemoteApiForOAuth( |
| 61 '%s.appspot.com' % app_id, | 43 '%s.appspot.com' % app_id, |
| 62 '/_ah/remote_api', | 44 '/_ah/remote_api', |
| 63 secure=True, | 45 secure=True, |
| 64 save_cookies=True) | 46 save_cookies=True) |
| 65 setattr(EnableRemoteApi, app_id, True) | 47 setattr(EnableRemoteApi, app_id, True) |
| OLD | NEW |