服務和型別 Getter

在 Python 中擷取使用 API 所需的所有各種 proto 類別參照,可能會很冗長,而且您必須對 API 有內在瞭解,或經常切換結構定義來參照 proto 或說明文件。

用戶端的 get_service 和 get_type 方法

這兩種 getter 方法可讓您擷取 API 中的任何服務或類型物件。get_service 方法用於擷取服務用戶端。get_type 則用於其他物件。服務用戶端類別是在版本路徑 google/ads/googleads/v*/services/ 下的程式碼中定義,所有類型則是在各種物件類別 google/ads/googleads/v*/common|enums|errors|resources|services/types/ 下定義。版本目錄下的所有程式碼都會產生,因此最佳做法是使用這些方法,而不是直接匯入物件,以免程式碼集結構變更。

以下範例說明如何使用 get_service 方法擷取 GoogleAdsService 用戶端的例項 (或 google-ads v28.4.0 及以上版本中,透過傳遞 is_async=True 取得非同步 GoogleAdsServiceAsyncClient):

from google.ads.googleads.client import GoogleAdsClient

# "load_from_storage" loads your API credentials from disk so they
# can be used for service initialization. Providing the optional `version`
# parameter means that the v26 version of GoogleAdsService will
# be returned.
client = GoogleAdsClient.load_from_storage(version="v26")
googleads_service = client.get_service("GoogleAdsService")

# Supported in google-ads v28.4.0 and later: retrieve an async service client.
googleads_async_service = client.get_service("GoogleAdsService", is_async=True)

以下範例說明如何使用 get_type 方法擷取 Campaign 執行個體:

from google.ads.googleads.client import GoogleAdsClient

client = GoogleAdsClient.load_from_storage(version="v26")
campaign = client.get_type("Campaign")

列舉

雖然您可以使用 get_type 方法擷取列舉,但每個 GoogleAdsClient 執行個體也都有 enums 屬性,可使用與 get_type 方法相同的機制動態載入列舉。這個介面比使用 get_type 更簡單,也更容易閱讀:

from google.ads.googleads.client import GoogleAdsClient

client = GoogleAdsClient.load_from_storage(version="v26")

campaign = client.get_type("Campaign")
campaign.status = client.enums.CampaignStatusEnum.PAUSED

Proto 物件欄位如果是列舉,在 Python 中會以內建的 enum 類型表示。也就是說,您可以直接讀取成員的值。在 Python REPL 中使用上一個範例的 campaign 例項:

>>> print(campaign.status)
CampaignStatus.PAUSED
>>> type(campaign.status)
<enum 'CampaignStatus'>
>>> print(campaign.status.value)
3

有時,瞭解與列舉值對應的欄位名稱會很有幫助。您可以使用 name 屬性存取這項資訊:

>>> print(campaign.status.name)
'PAUSED'
>>> type(campaign.status.name)
<class 'str'>

與列舉互動的方式會因 use_proto_plus 設定設為 true 或 false 而有所不同。如要進一步瞭解這兩個介面,請參閱 protobuf 訊息說明文件。

版本管理

系統會同時維護多個版本的 API。雖然 v26 是最新版本,但舊版本在停用前仍可存取。程式庫包含獨立的 proto 訊息類別,對應至每個有效的 API 版本。如要存取特定版本的訊息類別,請在初始化用戶端時提供 version 關鍵字參數,讓用戶端一律傳回該版本的例項:

from google.ads.googleads.client import GoogleAdsClient

client = GoogleAdsClient.load_from_storage(version="v26")
# The Campaign instance will be from the v26 version of the API.
campaign = client.get_type("Campaign")

如果在初始化用戶端時未指定 version,則可在呼叫 get_service 和 get_type 方法時,為每次呼叫指定版本 (請注意,如果在初始化 GoogleAdsClient 時設定 version,則會覆寫傳遞至 get_service 或 get_type 的任何 version 引數):

from google.ads.googleads.client import GoogleAdsClient

client = GoogleAdsClient.load_from_storage()
# This loads the v26 version of the GoogleAdsService.
googleads_service = client.get_service(
    "GoogleAdsService", version="v26"
)

# This loads a specific supported API version (such as v23) of a Campaign.
campaign = client.get_type("Campaign", version="v23")

如果未提供 version 關鍵字參數,程式庫會預設為已安裝 google-ads 套件支援的最高 API 版本 (最新版本為 "v26")。請注意,如要存取次要 API 版本 (例如 v25.1),請使用主要版本字串 (version="v25")。如需最新版本和其他可用版本的更新清單,請參閱 API 參考資料說明文件的左側導覽部分。