From a70a39dc342eb6bff452981f88ea571e6a6a775e Mon Sep 17 00:00:00 2001 From: troyraen Date: Sun, 14 Jan 2024 03:18:25 -0800 Subject: [PATCH] add projectid property --- pittgoogle/bigquery.py | 25 ++++++++++++++----------- pittgoogle/pubsub.py | 14 +++++++++----- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/pittgoogle/bigquery.py b/pittgoogle/bigquery.py index 04cbf79..541dd59 100644 --- a/pittgoogle/bigquery.py +++ b/pittgoogle/bigquery.py @@ -50,20 +50,19 @@ class Table: Name of the BigQuery dataset this table belongs to. projectid : `str`, optional - The topic owner's Google Cloud project ID. Either this or `auth` is required. Use this - if you are connecting to a subscription owned by a different project than this topic. Note: + The table owner's Google Cloud project ID. Either this or `auth` is required. Note: :attr:`pittgoogle.utils.ProjectIds` is a registry containing Pitt-Google's project IDs. auth : :class:`pittgoogle.auth.Auth`, optional - Credentials for the Google Cloud project that owns this topic. If not provided, + Credentials for the Google Cloud project that owns this table. If not provided, it will be created from environment variables when needed. - client : `pubsub_v1.PublisherClient`, optional - Pub/Sub client that will be used to access the topic. If not provided, a new client will + client : `bigquery.Client`, optional + BigQuery client that will be used to access the table. If not provided, a new client will be created (using `auth`) the first time it is requested. """ name: str = field() dataset: str = field() - projectid: str = field(default=None) + _projectid: str = field(default=None) _auth: Auth = field(default=None, validator=optional(instance_of(Auth))) _client: Optional[bigquery.Client] = field( default=None, validator=optional(instance_of(bigquery.Client)) @@ -122,20 +121,24 @@ def auth(self) -> Auth: if self._auth is None: self._auth = Auth() - if (self.projectid != self._auth.GOOGLE_CLOUD_PROJECT) and (self.projectid is not None): + if (self._projectid != self._auth.GOOGLE_CLOUD_PROJECT) and (self._projectid is not None): LOGGER.warning(f"setting projectid to match auth: {self._auth.GOOGLE_CLOUD_PROJECT}") - self.projectid = self._auth.GOOGLE_CLOUD_PROJECT + self._projectid = self._auth.GOOGLE_CLOUD_PROJECT return self._auth @property def id(self) -> str: """Fully qualified table ID.""" - # make sure we have a projectid. if it needs to be set, call auth - if self.projectid is None: - self.auth return f"{self.projectid}.{self.dataset}.{self.name}" + @property + def projectid(self) -> str: + """The table owner's Google Cloud project ID.""" + if self._projectid is None: + self._projectid = self.auth.GOOGLE_CLOUD_PROJECT + return self._projectid + @property def table(self) -> bigquery.Table: """Return a BigQuery Table object that's connected to the table. Makes a get request if necessary.""" diff --git a/pittgoogle/pubsub.py b/pittgoogle/pubsub.py index 4f8ecbe..95709da 100644 --- a/pittgoogle/pubsub.py +++ b/pittgoogle/pubsub.py @@ -232,20 +232,24 @@ def auth(self) -> Auth: if self._auth is None: self._auth = Auth() - if (self.projectid != self._auth.GOOGLE_CLOUD_PROJECT) and (self.projectid is not None): + if (self._projectid != self._auth.GOOGLE_CLOUD_PROJECT) and (self._projectid is not None): LOGGER.warning(f"setting projectid to match auth: {self._auth.GOOGLE_CLOUD_PROJECT}") - self.projectid = self._auth.GOOGLE_CLOUD_PROJECT + self._projectid = self._auth.GOOGLE_CLOUD_PROJECT return self._auth @property def path(self) -> str: """Fully qualified path to the topic.""" - # make sure we have a projectid. if it needs to be set, call auth - if self.projectid is None: - self.auth return f"projects/{self.projectid}/topics/{self.name}" + @property + def projectid(self) -> str: + """The topic owner's Google Cloud project ID.""" + if self._projectid is None: + self._projectid = self.auth.GOOGLE_CLOUD_PROJECT + return self._projectid + @property def client(self) -> pubsub_v1.PublisherClient: """Pub/Sub client for topic access.