서비스 및 유형 getter

Python에서 API를 사용하는 데 필요한 다양한 프로토 클래스를 모두 가져오는 것은 장황할 수 있으며 API에 대한 내재적 이해가 필요하거나 프로토 또는 문서를 참조하기 위해 자주 컨텍스트를 전환해야 합니다.

클라이언트의 get_serviceget_type 메서드

이 두 getter 메서드를 사용하면 API에서 서비스 또는 유형 객체를 가져올 수 있습니다. get_service 메서드는 서비스 클라이언트를 검색하는 데 사용됩니다. get_type는 다른 객체에 사용됩니다. 서비스 클라이언트 클래스는 버전 경로 google/ads/googleads/v*/services/services/ 아래의 코드에 정의되고 모든 유형은 다양한 객체 카테고리 google/ads/googleads/v*/common|enums|errors|resources|services/types/ 아래에 정의됩니다. 버전 디렉터리 아래의 모든 코드는 생성되므로 코드베이스의 구조가 변경될 경우 객체를 직접 가져오는 대신 이러한 메서드를 사용하는 것이 좋습니다.

다음은 get_service 메서드를 사용하여 GoogleAdsService 클라이언트의 인스턴스를 가져오는 방법의 예입니다.

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 v20 version of GoogleAdsService will
# be returned.
client = GoogleAdsClient.load_from_storage(version="v20")
googleads_service = client.get_service("GoogleAdsService")

다음은 get_type 메서드를 사용하여 Campaign 인스턴스를 가져오는 방법을 보여주는 예입니다.

from google.ads.googleads.client import GoogleAdsClient

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

열거형

get_type 메서드를 사용하여 Enum을 가져올 수 있지만 각 GoogleAdsClient 인스턴스에는 get_type 메서드와 동일한 메커니즘을 사용하여 Enum을 동적으로 로드하는 enums 속성도 있습니다. 이 인터페이스는 get_type를 사용하는 것보다 더 간단하고 읽기 쉽습니다.

client = GoogleAdsClient.load_from_storage(version=v20)

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

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

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

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

버전 관리

API의 여러 버전이 동시에 유지됩니다. v20가 최신 버전일 수 있지만 이전 버전은 지원이 중단될 때까지 계속 액세스할 수 있습니다. 라이브러리에는 활성 API 버전별로 별도의 프로토 메시지 클래스가 포함됩니다. 특정 버전의 메시지 클래스에 액세스하려면 클라이언트를 초기화할 때 version 키워드 매개변수를 제공하여 항상 지정된 버전의 인스턴스를 반환하도록 합니다.

client = GoogleAdsService.load_from_storage(version="/google-ads/api/reference/rpc/v20/")
# The Campaign instance will be from the v20 version of the API.
campaign = client.get_type("Campaign")

get_serviceget_type 메서드를 호출할 때 버전을 지정할 수도 있습니다. 이렇게 하면 클라이언트를 초기화할 때 제공된 버전이 재정의됩니다.

client = GoogleAdsService.load_from_storage()
# This will load the v20 version of the GoogleAdsService.
googleads_service = client.get_service(
    "GoogleAdsService", version="v20")

client = GoogleAdsService.load_from_storage(version="v20")
# This will load the v18 version of a Campaign.
campaign = client.get_type("Campaign", version="v18")

version 키워드 매개변수가 제공되지 않으면 라이브러리에서 기본적으로 최신 버전을 사용합니다. 최신 버전 및 사용 가능한 기타 버전의 업데이트된 목록은 API 참조 문서의 왼쪽 탐색 섹션에서 확인할 수 있습니다.