Esempio n. 1
0
def init():
    """Explicitly (re-)initialize _client(). This is useful for testing."""
    # We discard the project from the service account credentials, as it may be
    # different from the Datastore project we wish to connect to.
    creds = credentials.get_default()[0]
    _local.client = datastore.Client(project=utils.get_application_id(),
                                     credentials=creds)
Esempio n. 2
0
def get_api_client():
  """Return an api client for bigquery."""
  return discovery.build(
      'bigquery',
      'v2',
      cache_discovery=False,
      credentials=credentials.get_default()[0])
Esempio n. 3
0
  def __init__(self, project_id, zone):
    self.project_id = project_id
    self.zone = zone

    creds = credentials.get_default(scopes=_SCOPES)[0]
    http = google_auth_httplib2.AuthorizedHttp(
        creds, http=httplib2.Http(timeout=REQUEST_TIMEOUT))

    self.compute = build('compute', 'v1', http=http, cache_discovery=False)
Esempio n. 4
0
def initialize():
    """Initialize if monitoring is enabled for this bot."""
    global _monitoring_v3_client
    global _flusher_thread

    if environment.get_value('LOCAL_DEVELOPMENT'):
        return

    if not local_config.ProjectConfig().get('monitoring.enabled'):
        return

    if check_module_loaded(monitoring_v3):
        _initialize_monitored_resource()
        _monitoring_v3_client = monitoring_v3.MetricServiceClient(
            credentials=credentials.get_default()[0])
        _flusher_thread = _FlusherThread()
        _flusher_thread.start()
Esempio n. 5
0
    def _api_client(self):
        """Get the client for the current thread."""
        if hasattr(self._local, 'api_client'):
            return self._local.api_client

        emulator_host = environment.get_value('PUBSUB_EMULATOR_HOST')
        creds = credentials.get_default()[0]
        if emulator_host:
            # Replace real discovery document's root url with the emulator.
            _, discovery_doc = httplib2.Http().request(
                googleapiclient.discovery.DISCOVERY_URI.format(
                    api='pubsub', apiVersion='v1'))
            discovery_doc = json.loads(discovery_doc)
            discovery_doc['rootUrl'] = 'http://{}/'.format(emulator_host)

            self._local.api_client = googleapiclient.discovery.build_from_document(
                discovery_doc, credentials=creds)
        else:
            self._local.api_client = googleapiclient.discovery.build(
                'pubsub', 'v1', cache_discovery=False, credentials=creds)

        return self._local.api_client