서비스 및 유형 getter

Python에서 API를 사용하는 데 필요한 모든 다양한 proto 클래스에 대한 참조를 가져오는 작업은 장황할 수 있으며, API에 대한 본질적인 이해가 있어야 하며, proto나 문서를 참조하기 위해 컨텍스트 전환을 자주 해야 합니다.

클라이언트의 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 v16 version of GoogleAdsService will
# be returned.
client = GoogleAdsClient.load_from_storage(version="v16")
googleads_service = client.get_service("GoogleAdsService")

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

from google.ads.googleads.client import GoogleAdsClient

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

열거형

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

client = GoogleAdsClient.load_from_storage(version=v16)

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가 동시에 유지관리됩니다. v16가 최신 버전일 수 있지만, 서비스가 종료될 때까지 이전 버전은 계속 액세스할 수 있습니다. 라이브러리에는 각 활성 API 버전에 상응하는 별도의 proto 메시지 클래스가 포함됩니다. 특정 버전의 메시지 클래스에 액세스하려면 클라이언트를 초기화할 때 version 키워드 매개변수를 제공하여 항상 지정된 버전의 인스턴스를 반환하도록 합니다.

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

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

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

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

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