import requests

from utils import logger


class Schedule:
    def __init__(self, token, url):
        self.url = url
        self.token = token

    def schedule_data(self, cluster_id, package_id):
        headers = {
            "Content-Type": "application/json",
            "Authorization": self.token
        }
        data = {
            "info": {
                "type": "dataset",
                "packageID": package_id,
                "clusterIDs": [
                    cluster_id
                ]
            }
        }

        target_url = self.url + '/v2/bindings/do/sync'
        resp = requests.post(target_url, headers=headers, json=data)

        if resp.status_code == 200:
            logger.info(resp.json())
            binding_id = resp.json().get('data', {}).get('bindingID', [])
        else:
            logger.info(resp.text)
            return False, ""

        if binding_id == 0:
            return False, ""

        # 调用/v2/bindings/detail接口，get请求，参数为id
        target_url = self.url + '/v2/bindings/detail?id=' + str(binding_id)
        resp = requests.get(target_url, headers=headers)
        if resp.status_code == 200:
            logger.info(resp.json())
            return True, resp.json().get('data', {}).get('detail', [])[0]
        else:
            logger.info(resp.text)
            return False, ""
