서비스 및 유형 가져오기

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 메서드를 사용하여 enum을 가져올 수 있지만 각 GoogleAdsClient 인스턴스에는 get_type 메서드와 동일한 메커니즘을 사용하여 enum을 동적으로 로드하는 enums 속성도 있습니다. 이 인터페이스는 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

enum인 proto 객체 필드는 Python에서 기본 제공되는 enum 유형으로 표현됩니다. 즉, 멤버의 값을 직접 읽을 수 있습니다. Python REPL에서 이전 예시의 campaign 인스턴스로 작업합니다.

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

enum 값에 해당하는 필드의 이름을 아는 것이 유용할 때가 있습니다. name 속성을 사용하여 이 정보에 액세스할 수 있습니다.

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

enum과의 상호작용은 use_proto_plus 구성이 true 또는 false로 설정되어 있는지에 따라 다릅니다. 두 인터페이스에 관한 자세한 내용은 protobuf 메시지 문서를 참고하세요.

버전 관리

API의 여러 버전이 동시에 유지됩니다. v26가 최신 버전이지만 이전 버전도 지원이 종료될 때까지 계속 액세스할 수 있습니다. 라이브러리에는 각 활성 API 버전에 해당하는 별도의 proto 메시지 클래스가 포함되어 있습니다. 특정 버전의 메시지 클래스에 액세스하려면 클라이언트를 초기화할 때 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 참조 문서의 왼쪽 탐색 섹션에서 확인할 수 있습니다.