Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manipulate an object of Google Ads API's Enum class - python

I am using the python client library to connect to Google Ads's API.

    ga_service = client_service.get_service('GoogleAdsService')
    query = ('SELECT campaign.id, campaign.name, campaign.advertising_channel_type '
            'FROM campaign WHERE date BETWEEN \''+fecha+'\' AND \''+fecha+'\'')

    response = ga_service.search(<client_id>, query=query,page_size=1000)
    result = {}
    result['campanas'] = []

    try:
        for row in response:
            print row
            info = {}
            info['id'] = row.campaign.id.value
            info['name'] = row.campaign.name.value
            info['type'] = row.campaign.advertising_channel_type

When I parse the values this is the result I get:

{
  "campanas": [
    {
      "id": <campaign_id>, 
      "name": "Lanzamiento SIKU", 
      "type": 2
    }, 
    {
      "id": <campaign_id>, 
      "name": "lvl1 - website traffic", 
      "type": 2
    }, 
    {
      "id": <campaign_id>, 
      "name": "Lvl 2 - display", 
      "type": 3
    }
  ]
}

Why am I getting an integer for result["type"] ? When I check the traceback call I can see a string:

campaign {
  resource_name: "customers/<customer_id>/campaigns/<campaign_id>"
  id {
    value: 397083380
  }
  name {
    value: "Lanzamiento SIKU"
  }
  advertising_channel_type: SEARCH
}

campaign {
  resource_name: "customers/<customer_id>/campaigns/<campaign_id>"
  id {
    value: 1590766475
  }
  name {
    value: "lvl1 - website traffic"
  }
  advertising_channel_type: SEARCH
}

campaign {
  resource_name: "customers/<customer_id>/campaigns/<campaign_id>"
  id {
    value: 1590784940
  }
  name {
    value: "Lvl 2 - display"
  }
  advertising_channel_type: DISPLAY
}

I've searched on the Documentation for the API and found out that it's because the field: advertising_channel_type is of Data Type: Enum. How can I manipulate this object of the Enum class to get the string value? There is no helpful information about this on their Documentation.

Please help !!

like image 840
mateocam Avatar asked Oct 31 '18 17:10

mateocam


1 Answers

The Enum's come with some methods to translate between index and string

channel_types = client_service.get_type('AdvertisingChannelTypeEnum')

channel_types.AdvertisingChannelType.Value('SEARCH')
# => 2
channel_types.AdvertisingChannelType.Name(2)
# => 'SEARCH'

This was found by looking at docstrings, e.g.

channel_types.AdvertisingChannelType.__doc__
# => 'A utility for finding the names of enum values.'
like image 180
Heath Winning Avatar answered Oct 05 '22 23:10

Heath Winning