from dataclasses import dataclass, asdict

from domainmaster.commands.base import BaseCommand
from domainmaster.errors import DomainmasterError
from domainmaster.models.keyset import Keyset


class ShowKeyset(BaseCommand):
    COMMAND = 'show keyset'

    @dataclass
    class Params:
        keyset: str

    def execute(self, keyset: str, params: Params) -> Keyset:
        payload = {
            'command': self.COMMAND % keyset,
            'params': asdict(params)
        }

        response = self.requestor.request(payload)

        if response['status'] != 'success':
            raise DomainmasterError(message=response['message'])

        response = Keyset(
            id=response['id'],
            contact_tech=response['contact_tech'],
            dns_key=response['dns_key']
        )
        return response
