from pyopenproject.api_connection.exceptions.request_exception import RequestError
from pyopenproject.api_connection.requests.get_request import GetRequest
from pyopenproject.business.exception.business_error import BusinessError
from pyopenproject.business.services.command.root.root_command import RootCommand
from pyopenproject.model.root import Root


class Find(RootCommand):

    def __init__(self, connection):
        """Constructor for class Find, from RootCommand

        :param connection: The connection data
        """
        super().__init__(connection)

    def execute(self):
        try:
            json_obj = GetRequest(self.connection, f"{self.CONTEXT}").execute()
            return Root(json_obj)
        except RequestError as re:
            raise BusinessError("Error finding root") from re
