###################################################################################
# LAVA QA tool
# Copyright (C) 2015 Collabora Ltd.

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  US
###################################################################################

from xmlrpclib import ServerProxy

class Connection(object):
    """Class handling the connection to the LAVA server."""

    def __init__(self, api_url):
        self._api_url = api_url
        self._conn = ServerProxy(self._api_url, use_datetime=True, allow_none=True)

    def submit_job(self, data):
        return self._conn.scheduler.submit_job(data)

    def cancel_job(self, job_id):
        return self._conn.scheduler.cancel_job(job_id)

    def resubmit_job(self, job_id):
        return self._conn.scheduler.resubmit_job(job_id)

    def streams(self):
        return self._conn.dashboard.streams()

    def make_stream(self, pathname, description):
        return self._conn.dashboard.make_stream(pathname, description)

    def job_details(self, job_id):
        return self._conn.scheduler.job_details(job_id)

    def job_output(self, job_id, offset=0):
        return self._conn.scheduler.job_output(job_id, offset).data

    def job_status(self, job_id):
        return self._conn.scheduler.job_status(job_id)

    def get_bundle(self, sha1):
        return self._conn.dashboard.get(sha1)

    def get_filter_results(self, filter_name, count=10, offset=0):
        return self._conn.dashboard.get_filter_results(filter_name, count, offset)

    def get_filter_results_since(self, filter_name, since):
        return \
            self._conn.dashboard.get_filter_results_since(filter_name, since)

    def bundles(self, pathname):
        return self._conn.dashboard.bundles(pathname)

    def all_jobs(self):
        return self._conn.scheduler.all_jobs()

    def all_devices(self):
        return self._conn.scheduler.all_devices()

    def whoami(self):
        return self._conn.system.whoami()

    def version(self):
        return self._conn.dashboard.version()
