<commit_before>import json
import os
import requests
import logging
from pyCGA.Exceptions import LoginException, ServerResponseException
from pathlib import Path

__author__ = 'antonior'


class WS:

    def __init__(self, token=None, version="v1", instance="opencga"):

        if token is None:
            HOME = Path(os.getenv("HOME"))
            opencga_dir = HOME.joinpath(".opencga", "openCGA.json")
            if not Path.exists(opencga_dir):
                raise LoginException()
            fd = open(opencga_dir.as_posix())
            session = json.load(fd)
        else:
            session = token
        self.session_id = session["sid"]
        self.host = session["host"]
        if "instance" in session:
            self.instance = session["instance"]
        else:
            self.instance = instance
        self.pre_url = os.path.join(self.host, self.instance, "webservices", "rest", version)

    @staticmethod
    def check_server_response(response):
        if response == 200 or response == 500:
            return True
        else:
            return False

    def get_result(self, response):
        if response["response"][0]["numResults"] == -1:
            logging.error(response["error"])
            raise ServerResponseException(response["response"][0]["errorMsg"])
        else:
            return response["response"][0]["result"]

    def run_ws(self, url):
        """

        :param url:
        :return: :raise StandardError:
        """

        response = requests.get(url)
        if self.check_server_response(response.status_code):
            return self.get_result(response.json())
        else:
            logging.error("WS Failed, status: " + str(response.status_code))
            raise ServerResponseException("WS Failed, status: " + str(response.status_code))

    def run_ws_post(self, url, data):
        """

        :param url:
        :return:
        """
        response = requests.post(url, json=data)
        if self.check_server_response(response.status_code):
            return self.get_result(response.json())
        else:
            raise Exception("WS Failed, status: " + str(response.status_code))

    def general_method(self, ws_category, method_name, item_id=None, data=None, **options):
        """
        This is a wildcard method, if some of the ws in catalog are not implemented in this python wrapper you can
        always use the general method of the corresponding class.

        :rtype : list of dict
        :param ws_category: category of ws, i.e: Users, projects, studies...
        :param method name: name of the method to be called
        :param item_id: item_id
        :param options: this argument is a dictionary with parameters to be used in the ws
        :return: list of results
        """

        if "limit" not in options:
            options["limit"] = -1

        options_string = ""
        if options:
            options_string = "&".join([option_name + "=" + options[option_name] for option_name in options])

        if item_id:
            url = os.path.join(self.pre_url, ws_category, item_id, method_name, "?sid=" + self.session_id + "&" + options_string)
        else:
            url = os.path.join(self.pre_url, ws_category, method_name, "?sid=" + self.session_id + "&" + options_string)
        print(url)

        if data:
            return self.run_ws_post(url, data)
        else:
            result = self.run_ws(url)
        return result

<commit_msg>All options are transformed in strings<commit_after>import json
import os
import requests
import logging
from pyCGA.Exceptions import LoginException, ServerResponseException
from pathlib import Path

__author__ = 'antonior'


class WS:

    def __init__(self, token=None, version="v1", instance="opencga"):

        if token is None:
            HOME = Path(os.getenv("HOME"))
            opencga_dir = HOME.joinpath(".opencga", "openCGA.json")
            if not Path.exists(opencga_dir):
                raise LoginException()
            fd = open(opencga_dir.as_posix())
            session = json.load(fd)
        else:
            session = token
        self.session_id = session["sid"]
        self.host = session["host"]
        if "instance" in session:
            self.instance = session["instance"]
        else:
            self.instance = instance
        self.pre_url = os.path.join(self.host, self.instance, "webservices", "rest", version)

    @staticmethod
    def check_server_response(response):
        if response == 200 or response == 500:
            return True
        else:
            return False

    def get_result(self, response):
        if response["response"][0]["numResults"] == -1:
            logging.error(response["error"])
            raise ServerResponseException(response["response"][0]["errorMsg"])
        else:
            return response["response"][0]["result"]

    def run_ws(self, url):
        """

        :param url:
        :return: :raise StandardError:
        """

        response = requests.get(url)
        if self.check_server_response(response.status_code):
            return self.get_result(response.json())
        else:
            logging.error("WS Failed, status: " + str(response.status_code))
            raise ServerResponseException("WS Failed, status: " + str(response.status_code))

    def run_ws_post(self, url, data):
        """

        :param url:
        :return:
        """
        response = requests.post(url, json=data)
        if self.check_server_response(response.status_code):
            return self.get_result(response.json())
        else:
            raise Exception("WS Failed, status: " + str(response.status_code))

    def general_method(self, ws_category, method_name, item_id=None, data=None, **options):
        """
        This is a wildcard method, if some of the ws in catalog are not implemented in this python wrapper you can
        always use the general method of the corresponding class.

        :rtype : list of dict
        :param ws_category: category of ws, i.e: Users, projects, studies...
        :param method name: name of the method to be called
        :param item_id: item_id
        :param options: this argument is a dictionary with parameters to be used in the ws
        :return: list of results
        """
        # TODO: Add pagination
        if "limit" not in options:
            options["limit"] = -1

        options_string = ""
        if options:
            options_string = "&".join([option_name + "=" + str(options[option_name]) for option_name in options])

        if item_id:
            url = os.path.join(self.pre_url, ws_category, item_id, method_name, "?sid=" + self.session_id + "&" + options_string)
        else:
            url = os.path.join(self.pre_url, ws_category, method_name, "?sid=" + self.session_id + "&" + options_string)
        print(url)

        if data:
            return self.run_ws_post(url, data)
        else:
            result = self.run_ws(url)
        return result


{'fraction_non_alphanumeric': 0.10813887307911212, 'fraction_numerical': 0.003414911781445646, 'mean_word_length': 2.276923076923077, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1785769', 'n_tokens_mistral': 2009, 'n_tokens_neox': 1938, 'n_words': 536}