# 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 : RetargetingListService
Operation: mutate(ADD)
API Reference: https://github.com/yahoojp-marketing/sponsored-search-api-documents/blob/201901/docs/en/api_reference/services/RetargetingListService.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 = 'RetargetingListService'
OPERATION = 'mutate(ADD)'
OPERAND = {
  "operator": "ADD", 
  "accountId": "SAMPLE-ACCOUNT-ID", 
  "operand": [
    {
      "xsi_type": "DefaultTargetList", 
      "accountId": "SAMPLE-ACCOUNT-ID", 
      "targetListType": "DEFAULT", 
      "targetListName": "TargetList Default", 
      "targetListDescription": "TargetList Default", 
      "reachStorageStatus": "OPEN", 
      "reachStorageSpan": "180"
    }, 
    {
      "xsi_type": "RuleBaseTargetList", 
      "accountId": "SAMPLE-ACCOUNT-ID", 
      "targetListType": "RULE", 
      "targetListName": "TargetList RULE", 
      "targetListDescription": "TargetList RULE", 
      "reachStorageStatus": "OPEN", 
      "reachStorageSpan": "180", 
      "rules": [
        {
          "ruleItems": {
            "xsi_type": "UrlRuleItem", 
            "ruleType": "URL_RULE", 
            "operator": "EQUALS", 
            "value": "http://yahoo.co.jp", 
            "urlKey": "URL"
          }
        }, 
        {
          "ruleItems": {
            "xsi_type": "UrlRuleItem", 
            "ruleType": "URL_RULE", 
            "operator": "NOT_EQUAL", 
            "value": "http://not.equal.yahoo.co.jp", 
            "urlKey": "REFFER_URL"
          }
        }, 
        {
          "ruleItems": {
            "xsi_type": "UrlRuleItem", 
            "ruleType": "URL_RULE", 
            "operator": "CONTAINS", 
            "value": "http://contains.yahoo.co.jp", 
            "urlKey": "REFFER_URL"
          }
        }, 
        {
          "ruleItems": {
            "xsi_type": "UrlRuleItem", 
            "ruleType": "URL_RULE", 
            "operator": "NOT_CONTAIN", 
            "value": "http://not.contain.yahoo.co.jp", 
            "urlKey": "REFFER_URL"
          }
        }, 
        {
          "ruleItems": {
            "xsi_type": "UrlRuleItem", 
            "ruleType": "URL_RULE", 
            "operator": "STARTS_WITH", 
            "value": "http://starts.with.yahoo.co.jp", 
            "urlKey": "REFFER_URL"
          }
        }, 
        {
          "ruleItems": {
            "xsi_type": "UrlRuleItem", 
            "ruleType": "URL_RULE", 
            "operator": "NOT_START_WITH", 
            "value": "http://not.start.with.yahoo.co.jp", 
            "urlKey": "REFFER_URL"
          }
        }, 
        {
          "ruleItems": {
            "xsi_type": "UrlRuleItem", 
            "ruleType": "URL_RULE", 
            "operator": "ENDS_WITH", 
            "value": "http://ends.with.yahoo.co.jp", 
            "urlKey": "REFFER_URL"
          }
        }, 
        {
          "ruleItems": {
            "xsi_type": "UrlRuleItem", 
            "ruleType": "URL_RULE", 
            "operator": "NOT_END_WITH", 
            "value": "http://not.end.with.yahoo.co.jp", 
            "urlKey": "REFFER_URL"
          }
        }, 
        {
          "ruleItems": {
            "xsi_type": "CustomKeyRuleItem", 
            "ruleType": "CUSTOM_KEY_RULE", 
            "operator": "NOT_END_WITH", 
            "value": "textValue", 
            "textKey": "textKey"
          }
        }
      ], 
      "isAllVisitor": "TRUE", 
      "isDateSpecific": "FALSE"
    }, 
    {
      "xsi_type": "LogicalTargetList", 
      "accountId": "SAMPLE-ACCOUNT-ID", 
      "targetListType": "LOGICAL", 
      "targetListName": "TargetList LOGICAL", 
      "targetListDescription": "TargetList LOGICAL", 
      "reachStorageStatus": "OPEN", 
      "reachStorageSpan": "180", 
      "logicalGroup": [
        {
          "condition": "AND", 
          "logicalOperand": [
            {
              "targetListId": "1111111"
            }, 
            {
              "targetListId": "1111112"
            }, 
            {
              "targetListId": "1111113"
            }
          ]
        }, 
        {
          "condition": "OR", 
          "logicalOperand": [
            {
              "targetListId": "2111111"
            }, 
            {
              "targetListId": "2111112"
            }
          ]
        }, 
        {
          "condition": "NOT", 
          "logicalOperand": {
            "targetListId": "3111111"
          }
        }
      ]
    }
  ]
}

"""
SAMPLE RESPONSE = {
  "rval": {
    "ListReturnValue.Type": "RetargetingListReturnValue", 
    "Operation.Type": "ADD", 
    "values": [
      {
        "operationSucceeded": "true", 
        "targetList": {
          "xsi_type": "DefaultTargetList", 
          "accountId": "SAMPLE-ACCOUNT-ID", 
          "owner": "OWNER", 
          "retargetingAccountStatus": {
            "agreeDate": "20170529", 
            "reviewStatus": "APPROVED"
          }, 
          "targetListId": "2222", 
          "targetListTrackId": "3333", 
          "targetListType": "DEFAULT", 
          "targetListName": "TargetList Default", 
          "targetListDescription": "TargetList Default", 
          "reachStorageStatus": "OPEN", 
          "reachStorageSpan": "180", 
          "reach": "0", 
          "tag": ""
        }
      }, 
      {
        "operationSucceeded": "true", 
        "targetList": {
          "xsi_type": "RuleBaseTargetList", 
          "accountId": "SAMPLE-ACCOUNT-ID", 
          "owner": "SHARED", 
          "retargetingAccountStatus": {
            "agreeDate": "20170629", 
            "reviewStatus": "APPROVED"
          }, 
          "targetListId": "12222", 
          "targetListTrackId": "13333", 
          "targetListType": "RULE", 
          "targetListName": "TargetList RULE", 
          "targetListDescription": "TargetList RULE", 
          "reachStorageStatus": "OPEN", 
          "reachStorageSpan": "180", 
          "reach": "0", 
          "rules": [
            {
              "ruleItems": {
                "xsi_type": "UrlRuleItem", 
                "ruleType": "URL_RULE", 
                "operator": "EQUALS", 
                "value": "http://yahoo.co.jp", 
                "urlKey": "URL"
              }
            }, 
            {
              "ruleItems": {
                "xsi_type": "UrlRuleItem", 
                "ruleType": "URL_RULE", 
                "operator": "NOT_EQUAL", 
                "value": "http://not.equal.yahoo.co.jp", 
                "urlKey": "REFFER_URL"
              }
            }, 
            {
              "ruleItems": {
                "xsi_type": "UrlRuleItem", 
                "ruleType": "URL_RULE", 
                "operator": "CONTAINS", 
                "value": "http://contains.yahoo.co.jp", 
                "urlKey": "REFFER_URL"
              }
            }, 
            {
              "ruleItems": {
                "xsi_type": "UrlRuleItem", 
                "ruleType": "URL_RULE", 
                "operator": "NOT_CONTAIN", 
                "value": "http://not.contain.yahoo.co.jp", 
                "urlKey": "REFFER_URL"
              }
            }, 
            {
              "ruleItems": {
                "xsi_type": "UrlRuleItem", 
                "ruleType": "URL_RULE", 
                "operator": "STARTS_WITH", 
                "value": "http://starts.with.yahoo.co.jp", 
                "urlKey": "REFFER_URL"
              }
            }, 
            {
              "ruleItems": {
                "xsi_type": "UrlRuleItem", 
                "ruleType": "URL_RULE", 
                "operator": "NOT_START_WITH", 
                "value": "http://not.start.with.yahoo.co.jp", 
                "urlKey": "REFFER_URL"
              }
            }, 
            {
              "ruleItems": {
                "xsi_type": "UrlRuleItem", 
                "ruleType": "URL_RULE", 
                "operator": "ENDS_WITH", 
                "value": "http://ends.with.yahoo.co.jp", 
                "urlKey": "REFFER_URL"
              }
            }, 
            {
              "ruleItems": {
                "xsi_type": "UrlRuleItem", 
                "ruleType": "URL_RULE", 
                "operator": "NOT_END_WITH", 
                "value": "http://not.end.with.yahoo.co.jp", 
                "urlKey": "REFFER_URL"
              }
            }, 
            {
              "ruleItems": {
                "xsi_type": "CustomKeyRuleItem", 
                "ruleType": "CUSTOM_KEY_RULE", 
                "operator": "NOT_END_WITH", 
                "value": "textValue", 
                "textKey": "textKey"
              }
            }
          ], 
          "isAllVisitor": "TRUE", 
          "isDateSpecific": "FALSE"
        }
      }, 
      {
        "operationSucceeded": "true", 
        "targetList": {
          "xsi_type": "LogicalTargetList", 
          "accountId": "SAMPLE-ACCOUNT-ID", 
          "owner": "SHARED", 
          "retargetingAccountStatus": {
            "agreeDate": "20170629", 
            "reviewStatus": "APPROVED"
          }, 
          "targetListId": "912222", 
          "targetListTrackId": "913333", 
          "targetListType": "LOGICAL", 
          "targetListName": "TargetList LOGICAL", 
          "targetListDescription": "TargetList LOGICAL", 
          "reachStorageStatus": "OPEN", 
          "reachStorageSpan": "180", 
          "reach": "0", 
          "logicalGroup": [
            {
              "condition": "AND", 
              "logicalOperand": [
                {
                  "targetListId": "1111111"
                }, 
                {
                  "targetListId": "1111112"
                }, 
                {
                  "targetListId": "1111113"
                }
              ]
            }, 
            {
              "condition": "OR", 
              "logicalOperand": [
                {
                  "targetListId": "2111111"
                }, 
                {
                  "targetListId": "2111112"
                }
              ]
            }, 
            {
              "condition": "NOT", 
              "logicalOperand": {
                "targetListId": "3111111"
              }
            }
          ]
        }
      }
    ]
  }
}
"""


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