text: # Copyright 2017 Become Corp. All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #      http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  """Example code for
  Service : AdGroupAdService
  Operation: mutate (SET)
  API Reference: https://github.com/yahoojp-marketing/sponsored-search-api-documents/blob/201901/docs/en/api_reference/services/AdGroupAdService.md
  
  Generated by 'api_reference_example_generator.py' using code template 'examples/sample_template.py.template'
  """
  
  import logging
  import json
  
  from yahooads import promotionalads
  
  logging.basicConfig(level=logging.INFO)
  # logging.getLogger('suds.client').setLevel(logging.DEBUG)
  # logging.getLogger('suds.transport').setLevel(logging.DEBUG)
  
  
  SERVICE = 'AdGroupAdService'
  OPERATION = 'mutate (SET)'
  OPERAND = {
    "operator": "SET", 
    "accountId": "SAMPLE-ACCOUNT-ID", 
    "operand": [
      {
        "campaignId": "SAMPLE-CAMPAIN-ID", 
        "adGroupId": "SAMPLE-ADGROUP-ID", 
        "adGroupName": "sample text ad.", 
        "adId": "SAMPLE-AD-ID", 
        "userStatus": "ACTIVE"
      }, 
      {
        "campaignId": "SAMPLE-CAMPAIN-ID", 
        "adGroupId": "SAMPLE-ADGROUP-ID", 
        "adId": "SAMPLE-AD-ID", 
        "adName": "sample app ad for ios.", 
        "userStatus": "ACTIVE"
      }, 
      {
        "campaignId": "SAMPLE-CAMPAIN-ID", 
        "adGroupId": "SAMPLE-ADGROUP-ID", 
        "adId": "SAMPLE-AD-ID", 
        "adName": "sample app ad for android.", 
        "userStatus": "ACTIVE"
      }, 
      {
        "campaignId": "SAMPLE-CAMPAIN-ID", 
        "adGroupId": "SAMPLE-ADGROUP-ID", 
        "adId": "SAMPLE-AD-ID", 
        "adName": "sample extended text ad.", 
        "userStatus": "ACTIVE"
      }, 
      {
        "campaignId": "SAMPLE-CAMPAIN-ID", 
        "adGroupId": "SAMPLE-ADGROUP-ID", 
        "adId": "SAMPLE-AD-ID", 
        "adName": "sample dynamic search linked ad.", 
        "userStatus": "ACTIVE"
      }
    ]
  }
  
  """
  SAMPLE RESPONSE = {
    "rval": {
      "ListReturnValue.Type": "AdGroupAdReturnValue", 
      "Operation.Type": "SET", 
      "values": [
        {
          "operationSucceeded": "true", 
          "adGroupAd": {
            "accountId": "SAMPLE-ACCOUNT-ID", 
            "campaignId": "SAMPLE-CAMPAIN-ID", 
            "campaignTrackId": "200000001", 
            "campaignName": "sample campaign.", 
            "adGroupId": "SAMPLE-ADGROUP-ID", 
            "adGroupTrackId": "300000001", 
            "adGroupName": "sample adGroup.", 
            "adId": "SAMPLE-AD-ID", 
            "adTrackId": "400000001", 
            "adName": "sample text ad.", 
            "userStatus": "ACTIVE", 
            "trademarkStatus": "NO_RESTRICTION", 
            "approvalStatus": "APPROVED", 
            "ad": {
              "xsi_type": "TextAd2", 
              "type": "TEXT_AD2", 
              "advancedUrl": "http://yahoo.co.jp", 
              "additionalAdvancedUrls": {
                "advancedUrl": "http://yahoo.co.jp/url1"
              }, 
              "advancedMobileUrl": "http://mobile.yahoo.co.jp", 
              "additionalAdvancedMobileUrls": {
                "advancedMobileUrl": "http://mobile.yahoo.co.jp/url1"
              }, 
              "trackingUrl": "http://yahoo.co.jp?url={lpurl}&c={campaignid}&g={adgroupid}&a={creative}&type={_site}&pid={_id1}&vid={_id2}", 
              "customParameters": {
                "parameters": [
                  {
                    "key": "site", 
                    "value": "yahoo"
                  }, 
                  {
                    "key": "id1", 
                    "value": "1234"
                  }, 
                  {
                    "key": "id2", 
                    "value": "a7h59A98yu"
                  }
                ]
              }, 
              "displayUrl": "yahoo.co.jp", 
              "headline": "sample ad title.", 
              "description": "sample ad description.", 
              "devicePreference": "SMART_PHONE", 
              "description2": "{=COUNTDOWN(\"2016/01/01 00:00:00\",\"ja\")}"
            }
          }
        }, 
        {
          "operationSucceeded": "true", 
          "adGroupAd": {
            "accountId": "SAMPLE-ACCOUNT-ID", 
            "campaignId": "SAMPLE-CAMPAIN-ID", 
            "campaignTrackId": "200000002", 
            "campaignName": "sample campaign.", 
            "adGroupId": "SAMPLE-ADGROUP-ID", 
            "adGroupTrackId": "300000002", 
            "adGroupName": "sample adGroup.", 
            "adId": "SAMPLE-AD-ID", 
            "adTrackId": "400000002", 
            "adName": "sample app ad for ios.", 
            "userStatus": "ACTIVE", 
            "trademarkStatus": "NO_RESTRICTION", 
            "approvalStatus": "APPROVED", 
            "ad": {
              "xsi_type": "AppAd", 
              "type": "APP_AD", 
              "advancedUrl": "http://www.apple.com/jp/itunes/app/appname/appid1234567890", 
              "displayUrl": "itunes.apple.com", 
              "headline": "sample ad title.", 
              "description": "sample ad description.", 
              "description2": "sample ad description2.", 
              "appStore": "IOS", 
              "appId": "appid1234567890"
            }
          }
        }, 
        {
          "operationSucceeded": "true", 
          "adGroupAd": {
            "accountId": "SAMPLE-ACCOUNT-ID", 
            "campaignId": "SAMPLE-CAMPAIN-ID", 
            "campaignTrackId": "200000003", 
            "campaignName": "sample campaign.", 
            "adGroupId": "SAMPLE-ADGROUP-ID", 
            "adGroupTrackId": "300000003", 
            "adGroupName": "sample adGroup.", 
            "adId": "SAMPLE-AD-ID", 
            "adTrackId": "400000003", 
            "adName": "sample app ad for android.", 
            "userStatus": "ACTIVE", 
            "trademarkStatus": "NO_RESTRICTION", 
            "approvalStatus": "APPROVED", 
            "ad": {
              "xsi_type": "AppAd", 
              "type": "APP_AD", 
              "advancedUrl": "https://play.google.com/store/apps/details?id=appid1234567890", 
              "displayUrl": "play.google.com/store", 
              "headline": "sample ad title.", 
              "description": "sample ad description.", 
              "description2": "sample ad description2.", 
              "appStore": "ANDROID", 
              "appId": "appid1234567890"
            }
          }
        }, 
        {
          "operationSucceeded": "true", 
          "adGroupAd": {
            "accountId": "SAMPLE-ACCOUNT-ID", 
            "campaignId": "SAMPLE-CAMPAIN-ID", 
            "campaignTrackId": "200000004", 
            "campaignName": "sample campaign.", 
            "adGroupId": "SAMPLE-ADGROUP-ID", 
            "adGroupTrackId": "300000004", 
            "adGroupName": "sample adGroup.", 
            "adId": "SAMPLE-AD-ID", 
            "adTrackId": "400000004", 
            "adName": "sample extended text ad.", 
            "userStatus": "ACTIVE", 
            "trademarkStatus": "NO_RESTRICTION", 
            "approvalStatus": "APPROVED", 
            "ad": {
              "xsi_type": "ExtendedTextAd", 
              "type": "EXTENDED_TEXT_AD", 
              "advancedUrl": "http://yahoo.co.jp", 
              "displayUrl": "yahoo.co.jp/path1/path2", 
              "headline": "sample ad title.", 
              "description": "sample ad description.", 
              "devicePreference": "SMART_PHONE", 
              "headline2": "sample ad title2.", 
              "path1": "path1", 
              "path2": "path2"
            }
          }
        }, 
        {
          "operationSucceeded": "true", 
          "adGroupAd": {
            "accountId": "SAMPLE-ACCOUNT-ID", 
            "campaignId": "SAMPLE-CAMPAIN-ID", 
            "campaignTrackId": "200000005", 
            "campaignName": "sample campaign.", 
            "adGroupId": "SAMPLE-ADGROUP-ID", 
            "adGroupTrackId": "300000005", 
            "adGroupName": "sample adGroup.", 
            "adId": "SAMPLE-AD-ID", 
            "adTrackId": "400000005", 
            "adName": "sample dynamic search linked ad.", 
            "userStatus": "ACTIVE", 
            "trademarkStatus": "NO_RESTRICTION", 
            "approvalStatus": "APPROVED", 
            "ad": {
              "xsi_type": "DynamicSearchLinkedAd", 
              "type": "DYNAMIC_SEARCH_LINKED_AD", 
              "description": "sample ad description."
            }
          }
        }
      ]
    }
  }
  """
  
  
  def main():
      client = promotionalads.PromotionalAdsClient.LoadFromConfiguration()
      service = client.GetService(SERVICE)
      print("REQUEST : {}.{}\n{}".format(SERVICE, OPERATION, json.dumps(OPERAND, indent=2)))
      try:
          if OPERATION == "get":
              response = service.get(OPERAND)
          elif OPERATION.startswith("get"):
              get_method = getattr(service, OPERATION)
              response = get_method(OPERAND)
          elif OPERATION.startswith("mutate"):
              response = service.mutate(OPERAND)
          else:
              raise("Unknown Operation '{}'".format(OPERATION))
          print("RESPONSE :\n{}".format(response))
      except Exception as e:
          print("Exception at '{}' operations \n{}".format(SERVICE, e))
          raise e
  
  
  if __name__ == '__main__':
      main()
  