#!/usr/bin/env python
# coding:utf-8

import requests
import json
import urllib3

class clear_cdn:
    def __init__(self, x_app_id, domain_list):
        self.url = "https://saas.castbox.fm/tool/api/v1/operate/batch/clearCdn"
        self.x_app_id = x_app_id
        self.domain_list = domain_list
        self.clear_file_list = []

    def appendClearFile(self, path):
        self.clear_file_list.append(path)

    def clearCDN(self):
        try:
            if self.x_app_id == "":
                print("clearCDN failed!!! x_app_id is None")
                return

            if len(self.domain_list) == 0:
                print("clearCDN failed!!! domain_list len is 0")
                return

            if len(self.clear_file_list) == 0:
                print("clearCDN failed!!! clear_file_list len is 0")
                return

            params = {
                "domainList": self.domain_list,
                "pathSuffixList": self.clear_file_list
            }
            urllib3.disable_warnings()
            r = requests.post(self.url,
                              headers={"X-APP-ID": self.x_app_id, "Content-Type": "application/json"},
                              data=json.dumps(params),
                              verify=False
                              )

            if r.status_code == 200:
                print(f"{str(self.clear_file_list)} clearCDN OK!!! ")
        except Exception as e:
            print("clearCDN failed!!! error: " + repr(e))

