# 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 : ReportDefinitionService
Operation: getReportFields
API Reference: https://github.com/yahoojp-marketing/sponsored-search-api-documents/blob/201901/docs/en/api_reference/services/ReportDefinitionService.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 = 'ReportDefinitionService'
OPERATION = 'getReportFields'
OPERAND = {
  "reportType": "LANDING_PAGE_URL"
}

"""
SAMPLE RESPONSE = {
  "rval": {
    "fields": [
      {
        "fieldName": "CAMPAIGN_ID", 
        "displayFieldNameJA": "\u30ad\u30e3\u30f3\u30da\u30fc\u30f3ID", 
        "displayFieldNameEN": "CampaignID", 
        "xmlAttributeName": "campaignID", 
        "fieldType": "LONG", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "ADGROUP_ID", 
        "displayFieldNameJA": "\u5e83\u544a\u30b0\u30eb\u30fc\u30d7ID", 
        "displayFieldNameEN": "Ad Group ID", 
        "xmlAttributeName": "adgroupID", 
        "fieldType": "LONG", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "CAMPAIGN_NAME", 
        "displayFieldNameJA": "\u30ad\u30e3\u30f3\u30da\u30fc\u30f3\u540d", 
        "displayFieldNameEN": "Campaign Name", 
        "xmlAttributeName": "campaignName", 
        "fieldType": "STRING", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "ADGROUP_NAME", 
        "displayFieldNameJA": "\u5e83\u544a\u30b0\u30eb\u30fc\u30d7\u540d", 
        "displayFieldNameEN": "Ad Group Name", 
        "xmlAttributeName": "adgroupName", 
        "fieldType": "STRING", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "COST", 
        "displayFieldNameJA": "\u30b3\u30b9\u30c8", 
        "displayFieldNameEN": "Cost", 
        "xmlAttributeName": "cost", 
        "fieldType": "LONG", 
        "canSelect": "true", 
        "canFilter": "true", 
        "impossibleCombinationFields": [
          "CONVERSION_NAME", 
          "OBJECT_OF_CONVERSION_TRACKING"
        ]
      }, 
      {
        "fieldName": "IMPS", 
        "displayFieldNameJA": "\u30a4\u30f3\u30d7\u30ec\u30c3\u30b7\u30e7\u30f3\u6570", 
        "displayFieldNameEN": "Impressions", 
        "xmlAttributeName": "impressions", 
        "fieldType": "LONG", 
        "canSelect": "true", 
        "canFilter": "true", 
        "impossibleCombinationFields": [
          "CONVERSION_NAME", 
          "OBJECT_OF_CONVERSION_TRACKING"
        ]
      }, 
      {
        "fieldName": "CLICKS", 
        "displayFieldNameJA": "\u30af\u30ea\u30c3\u30af\u6570", 
        "displayFieldNameEN": "Clicks", 
        "xmlAttributeName": "clicks", 
        "fieldType": "LONG", 
        "canSelect": "true", 
        "canFilter": "true", 
        "impossibleCombinationFields": [
          "CONVERSION_NAME", 
          "OBJECT_OF_CONVERSION_TRACKING"
        ]
      }, 
      {
        "fieldName": "CLICK_RATE", 
        "displayFieldNameJA": "\u30af\u30ea\u30c3\u30af\u7387", 
        "displayFieldNameEN": "CTR", 
        "xmlAttributeName": "ctr", 
        "fieldType": "DOUBLE", 
        "canSelect": "true", 
        "canFilter": "true", 
        "impossibleCombinationFields": [
          "CONVERSION_NAME", 
          "OBJECT_OF_CONVERSION_TRACKING"
        ]
      }, 
      {
        "fieldName": "AVG_CPC", 
        "displayFieldNameJA": "\u5e73\u5747CPC", 
        "displayFieldNameEN": "Avg. CPC", 
        "xmlAttributeName": "averageCpc", 
        "fieldType": "DOUBLE", 
        "canSelect": "true", 
        "canFilter": "true", 
        "impossibleCombinationFields": [
          "CONVERSION_NAME", 
          "OBJECT_OF_CONVERSION_TRACKING"
        ]
      }, 
      {
        "fieldName": "AVG_DELIVER_RANK", 
        "displayFieldNameJA": "\u5e73\u5747\u63b2\u8f09\u9806\u4f4d", 
        "displayFieldNameEN": "Avg. Position", 
        "xmlAttributeName": "averagePosition", 
        "fieldType": "DOUBLE", 
        "canSelect": "true", 
        "canFilter": "true", 
        "impossibleCombinationFields": [
          "CONVERSION_NAME", 
          "OBJECT_OF_CONVERSION_TRACKING"
        ]
      }, 
      {
        "fieldName": "TRACKING_URL", 
        "displayFieldNameJA": "\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0URL", 
        "displayFieldNameEN": "Tracking URL", 
        "xmlAttributeName": "trackingURL", 
        "fieldType": "STRING", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "LANDING_PAGE_URL", 
        "displayFieldNameJA": "\u6700\u7d42\u30ea\u30f3\u30af\u5148URL", 
        "displayFieldNameEN": "Landing Page URL", 
        "xmlAttributeName": "landingPageURL", 
        "fieldType": "STRING", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "CONVERSIONS", 
        "displayFieldNameJA": "\u30b3\u30f3\u30d0\u30fc\u30b8\u30e7\u30f3\u6570", 
        "displayFieldNameEN": "Conversions", 
        "xmlAttributeName": "conversions", 
        "fieldType": "DOUBLE", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "CONV_RATE", 
        "displayFieldNameJA": "\u30b3\u30f3\u30d0\u30fc\u30b8\u30e7\u30f3\u7387", 
        "displayFieldNameEN": "Conv. Rate", 
        "xmlAttributeName": "convRate", 
        "fieldType": "DOUBLE", 
        "canSelect": "true", 
        "canFilter": "true", 
        "impossibleCombinationFields": [
          "CONVERSION_NAME", 
          "OBJECT_OF_CONVERSION_TRACKING"
        ]
      }, 
      {
        "fieldName": "CONV_VALUE", 
        "displayFieldNameJA": "\u30b3\u30f3\u30d0\u30fc\u30b8\u30e7\u30f3\u306e\u4fa1\u5024", 
        "displayFieldNameEN": "Conv. Value", 
        "xmlAttributeName": "convValue", 
        "fieldType": "DOUBLE", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "COST_PER_CONV", 
        "displayFieldNameJA": "\u30b3\u30b9\u30c8/\u30b3\u30f3\u30d0\u30fc\u30b8\u30e7\u30f3\u6570", 
        "displayFieldNameEN": "Cost Per Conv.", 
        "xmlAttributeName": "costPerConv", 
        "fieldType": "DOUBLE", 
        "canSelect": "true", 
        "canFilter": "true", 
        "impossibleCombinationFields": [
          "CONVERSION_NAME", 
          "OBJECT_OF_CONVERSION_TRACKING"
        ]
      }, 
      {
        "fieldName": "VALUE_PER_CONV", 
        "displayFieldNameJA": "\u4fa1\u5024/\u30b3\u30f3\u30d0\u30fc\u30b8\u30e7\u30f3\u6570", 
        "displayFieldNameEN": "Value Per Conv.", 
        "xmlAttributeName": "valuePerConv", 
        "fieldType": "DOUBLE", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "ALL_CONV", 
        "displayFieldNameJA": "\u3059\u3079\u3066\u306e\u30b3\u30f3\u30d0\u30fc\u30b8\u30e7\u30f3\u6570", 
        "displayFieldNameEN": "All Conv.", 
        "xmlAttributeName": "allConv", 
        "fieldType": "DOUBLE", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "ALL_CONV_RATE", 
        "displayFieldNameJA": "\u3059\u3079\u3066\u306e\u30b3\u30f3\u30d0\u30fc\u30b8\u30e7\u30f3\u7387", 
        "displayFieldNameEN": "All Conv. Rate", 
        "xmlAttributeName": "allConvRate", 
        "fieldType": "DOUBLE", 
        "canSelect": "true", 
        "canFilter": "true", 
        "impossibleCombinationFields": [
          "CONVERSION_NAME", 
          "OBJECT_OF_CONVERSION_TRACKING"
        ]
      }, 
      {
        "fieldName": "ALL_CONV_VALUE", 
        "displayFieldNameJA": "\u3059\u3079\u3066\u306e\u30b3\u30f3\u30d0\u30fc\u30b8\u30e7\u30f3\u306e\u4fa1\u5024", 
        "displayFieldNameEN": "All Conv. Value", 
        "xmlAttributeName": "allConvValue", 
        "fieldType": "DOUBLE", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "COST_PER_ALL_CONV", 
        "displayFieldNameJA": "\u30b3\u30b9\u30c8/\u3059\u3079\u3066\u306e\u30b3\u30f3\u30d0\u30fc\u30b8\u30e7\u30f3\u6570", 
        "displayFieldNameEN": "Cost Per All Conv.", 
        "xmlAttributeName": "costPerAllConv", 
        "fieldType": "DOUBLE", 
        "canSelect": "true", 
        "canFilter": "true", 
        "impossibleCombinationFields": [
          "CONVERSION_NAME", 
          "OBJECT_OF_CONVERSION_TRACKING"
        ]
      }, 
      {
        "fieldName": "VALUE_PER_ALL_CONV", 
        "displayFieldNameJA": "\u4fa1\u5024/\u3059\u3079\u3066\u306e\u30b3\u30f3\u30d0\u30fc\u30b8\u30e7\u30f3\u6570", 
        "displayFieldNameEN": "Value Per All Conv.", 
        "xmlAttributeName": "valuePerAllConv", 
        "fieldType": "DOUBLE", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "CROSS_DEVICE_CONVERSIONS", 
        "displayFieldNameJA": "\u30c7\u30d0\u30a4\u30b9\u3092\u307e\u305f\u3044\u3060\u30b3\u30f3\u30d0\u30fc\u30b8\u30e7\u30f3\u6570", 
        "displayFieldNameEN": "Conversions across devices", 
        "xmlAttributeName": "crossDeviceConversions", 
        "fieldType": "DOUBLE", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "CAMPAIGN_TRACKING_ID", 
        "displayFieldNameJA": "\u30ad\u30e3\u30f3\u30da\u30fc\u30f3\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0ID", 
        "displayFieldNameEN": "Campaign Tracking ID", 
        "xmlAttributeName": "campaignTrackingID", 
        "fieldType": "LONG", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "ADGROUP_TRACKING_ID", 
        "displayFieldNameJA": "\u5e83\u544a\u30b0\u30eb\u30fc\u30d7\u30c8\u30e9\u30c3\u30ad\u30f3\u30b0ID", 
        "displayFieldNameEN": "Ad Group Tracking ID", 
        "xmlAttributeName": "adgroupTrackingID", 
        "fieldType": "LONG", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "NETWORK", 
        "displayFieldNameJA": "\u5e83\u544a\u63b2\u8f09\u65b9\u5f0f\u306e\u6307\u5b9a", 
        "displayFieldNameEN": "Network", 
        "xmlAttributeName": "network", 
        "fieldType": "ENUM", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "DEVICE", 
        "displayFieldNameJA": "\u30c7\u30d0\u30a4\u30b9", 
        "displayFieldNameEN": "Device", 
        "xmlAttributeName": "device", 
        "fieldType": "ENUM", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "DAY", 
        "displayFieldNameJA": "\u65e5", 
        "displayFieldNameEN": "Day", 
        "xmlAttributeName": "day", 
        "fieldType": "STRING", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "DAY_OF_WEEK", 
        "displayFieldNameJA": "\u66dc\u65e5", 
        "displayFieldNameEN": "Day of week", 
        "xmlAttributeName": "dayOfWeek", 
        "fieldType": "ENUM", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "QUARTER", 
        "displayFieldNameJA": "\u56db\u534a\u671f", 
        "displayFieldNameEN": "Quarter", 
        "xmlAttributeName": "quarter", 
        "fieldType": "STRING", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "YEAR", 
        "displayFieldNameJA": "\u5e74\u9593", 
        "displayFieldNameEN": "Year", 
        "xmlAttributeName": "year", 
        "fieldType": "STRING", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "MONTH", 
        "displayFieldNameJA": "\u6bce\u6708", 
        "displayFieldNameEN": "Month", 
        "xmlAttributeName": "month", 
        "fieldType": "STRING", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "MONTH_OF_YEAR", 
        "displayFieldNameJA": "\u6708", 
        "displayFieldNameEN": "Month of Year", 
        "xmlAttributeName": "monthofYear", 
        "fieldType": "ENUM", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "WEEK", 
        "displayFieldNameJA": "\u6bce\u9031", 
        "displayFieldNameEN": "Week", 
        "xmlAttributeName": "week", 
        "fieldType": "STRING", 
        "canSelect": "true", 
        "canFilter": "true"
      }, 
      {
        "fieldName": "OBJECT_OF_CONVERSION_TRACKING", 
        "displayFieldNameJA": "\u30b3\u30f3\u30d0\u30fc\u30b8\u30e7\u30f3\u6e2c\u5b9a\u306e\u76ee\u7684", 
        "displayFieldNameEN": "Objective of Conversion Tracking", 
        "xmlAttributeName": "objectiveOfConversionTracking", 
        "fieldType": "ENUM", 
        "canSelect": "true", 
        "canFilter": "true", 
        "impossibleCombinationFields": [
          "COST", 
          "IMPS", 
          "CLICKS", 
          "CLICK_RATE", 
          "AVG_CPC", 
          "AVG_DELIVER_RANK", 
          "CONV_RATE", 
          "COST_PER_CONV", 
          "ALL_CONV_RATE", 
          "COST_PER_ALL_CONV"
        ]
      }, 
      {
        "fieldName": "CONVERSION_NAME", 
        "displayFieldNameJA": "\u30b3\u30f3\u30d0\u30fc\u30b8\u30e7\u30f3\u540d", 
        "displayFieldNameEN": "Conversion Name", 
        "xmlAttributeName": "conversionName", 
        "fieldType": "STRING", 
        "canSelect": "true", 
        "canFilter": "true", 
        "impossibleCombinationFields": [
          "COST", 
          "IMPS", 
          "CLICKS", 
          "CLICK_RATE", 
          "AVG_CPC", 
          "AVG_DELIVER_RANK", 
          "CONV_RATE", 
          "COST_PER_CONV", 
          "ALL_CONV_RATE", 
          "COST_PER_ALL_CONV"
        ]
      }
    ]
  }
}
"""


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()
