<commit_before>"""
    A python abstraction of the Exosite Portals API endpoints.
"""
# pylint: disable=W0312
import requests, json
from pyonep.portals.constants import HTTP_STATUS
from pyonep.portals.utils import dictify_device_meta
from pyonep.portals.__version__ import __version__ as VERSION
from requests.auth import HTTPBasicAuth

try:
    # python3
    import http.client as http_client
except ImportError:
    # Python 2
    import httplib as http_client


class Domain(object):
    """
        This module contains the Domain() class from which
        the Portals() class is meant to be based on.
    """
    def __init__(   self,
                    domain,
                    user,
                    auth,
                    use_token=False):
        """
            Abstract the whitelabel/domain
        """
        self.__domain = domain
        self.__user = user
        if use_token:
            self.__headers = {'Authorization': 'Token '+ auth}
            self.__auth = None
        else:
            self.__headers = {}
            self.__auth = HTTPBasicAuth(user, auth)
        self.__durl = 'https://'+self.__domain
        self.__user_agent = 'Portals-Bindings-v{0}'.format(VERSION)
        self.__content_type = 'application/json; charset=utf-8'

    def domain(self):
        return self.__domain
    def user(self):
        return self.__user
    def auth(self):
        return self.__auth
    def domain_url(self):
        return self.__durl
    def user_agent(self):
        return self.__user_agent
    def content_type(self):
        return self.__content_type
    def headers(self):
        return self.__headers

class Endpoints(Domain):
    """
        The Endpoints() class should be as stateless as possible.

    """
    def __init__(   self,
                    domain,
                    portal_name,
                    user,
                    auth,
                    portal_id=None,
                    # portal_rid=None,
                    use_token=False,
                    debug=False
                    ):
        Domain.__init__(self, domain=domain, user=user, auth=auth, use_token=use_token)
        self.__purl = self.domain_url()+'/api/portals/v1'
        self.__vendor = self.domain().split('.')[0]
        self.__portal_name = portal_name
        self.__portal_id = portal_id
        # self.__portal_rid = portal_rid
        self.__portal_cik = None
        http_client.HTTPConnection.debuglevel = int(debug)


    # #####################################################
    #   Endpoints class member setters and getters.
    # #####################################################
    def set_portals_url(self, url):
        """ Member variable setter for  Portals API Base url. """
        self.__purl = url
    def set_vendor(self, vendor):
        """ Member variable setter for Exosite Vendor. """
        self.__vendor = vendor
    def set_portal_id(self, _id):
        """ Member variable setter for numerical Portal ID. """
        self.__portal_id = _id
    # def set_portal_rid(self, rid):
    #   self.__portal_rid = rid
    def set_portal_cik(self, cik):
        """ Member variable setter for Portal CIK. """
        self.__portal_cik = cik
    def set_portal_name(self, name):
        """ Member variable setter for Portal name. """
        self.__portal_name = name
    def portals_url(self):
        """ Returns Portals API Base url. """
        return self.__purl
    def vendor(self):
        """ Returns Exosite Vendor. """
        return self.__vendor
    def portal_id(self):
        """ Returns numerical Portal ID. """
        return self.__portal_id
    # def portal_rid(self):
    #   return self.__portal_rid
    def portal_cik(self):
        """ Returns numerical Portal CIK. """
        return self.__portal_cik
    def portal_name(self):
        """ Returns numerical Portal name. """
        return self.__portal_name

    # #####################################################
    #   Portals API method abstractions.
    # #####################################################
    def get_user_token(self):
        """
            Gets a authorization token for session reuse.

            http://docs.exosite.com/portals/#get-user-token-for-openid-user
        """
        headers = { 'User-Agent': self.user_agent(),
                'Host': self.domain(),
                'Accept': '*/*',
        }
        headers.update(self.headers())
        r = requests.get(   self.portals_url()+'/users/_this/token',
                            headers=headers,
                            auth=self.auth())
        if HTTP_STATUS.OK == r.status_code:
            return r.text
        else:
            print("get_user_token: Something went wrong: <{0}>: {1}".format(
                        r.status_code, r.reason))
            r.get_domain_portal_ids()

    def get_domain_portal_ids(self):
        """
            Gets the list of Portal ids.

            http://docs.exosite.com/portals/#list-portal-by-domain
        """
        headers = { 'User-Agent': self.user_agent(),
                'Host': self.domain(),
                'Accept': '*/*'
        }
        headers.update(self.headers())

        r = requests.get(   self.portals_url()+'/portals',
                            headers=headers,
                            auth=self.auth())
        if HTTP_STATUS.OK == r.status_code:
            return [ _id['id'] for _id in r.json() ]
        else:
            print("get_user_portals: Something went wrong: <{0}>: {1}".format(
                        r.status_code, r.reason))
            r.get_domain_portal_ids()<commit_msg>Fixed copy-paste error when trying to raise for status a different, incorrect function was getting called.<commit_after>"""
    A python abstraction of the Exosite Portals API endpoints.
"""
# pylint: disable=W0312
import requests, json
from pyonep.portals.constants import HTTP_STATUS
from pyonep.portals.utils import dictify_device_meta
from pyonep.portals.__version__ import __version__ as VERSION
from requests.auth import HTTPBasicAuth

try:
    # python3
    import http.client as http_client
except ImportError:
    # Python 2
    import httplib as http_client


class Domain(object):
    """
        This module contains the Domain() class from which
        the Portals() class is meant to be based on.
    """
    def __init__(   self,
                    domain,
                    user,
                    auth,
                    use_token=False):
        """
            Abstract the whitelabel/domain
        """
        self.__domain = domain
        self.__user = user
        if use_token:
            self.__headers = {'Authorization': 'Token '+ auth}
            self.__auth = None
        else:
            self.__headers = {}
            self.__auth = HTTPBasicAuth(user, auth)
        self.__durl = 'https://'+self.__domain
        self.__user_agent = 'Portals-Bindings-v{0}'.format(VERSION)
        self.__content_type = 'application/json; charset=utf-8'

    def domain(self):
        return self.__domain
    def user(self):
        return self.__user
    def auth(self):
        return self.__auth
    def domain_url(self):
        return self.__durl
    def user_agent(self):
        return self.__user_agent
    def content_type(self):
        return self.__content_type
    def headers(self):
        return self.__headers

class Endpoints(Domain):
    """
        The Endpoints() class should be as stateless as possible.

    """
    def __init__(   self,
                    domain,
                    portal_name,
                    user,
                    auth,
                    portal_id=None,
                    # portal_rid=None,
                    use_token=False,
                    debug=False
                    ):
        Domain.__init__(self, domain=domain, user=user, auth=auth, use_token=use_token)
        self.__purl = self.domain_url()+'/api/portals/v1'
        self.__vendor = self.domain().split('.')[0]
        self.__portal_name = portal_name
        self.__portal_id = portal_id
        # self.__portal_rid = portal_rid
        self.__portal_cik = None
        http_client.HTTPConnection.debuglevel = int(debug)


    # #####################################################
    #   Endpoints class member setters and getters.
    # #####################################################
    def set_portals_url(self, url):
        """ Member variable setter for  Portals API Base url. """
        self.__purl = url
    def set_vendor(self, vendor):
        """ Member variable setter for Exosite Vendor. """
        self.__vendor = vendor
    def set_portal_id(self, _id):
        """ Member variable setter for numerical Portal ID. """
        self.__portal_id = _id
    # def set_portal_rid(self, rid):
    #   self.__portal_rid = rid
    def set_portal_cik(self, cik):
        """ Member variable setter for Portal CIK. """
        self.__portal_cik = cik
    def set_portal_name(self, name):
        """ Member variable setter for Portal name. """
        self.__portal_name = name
    def portals_url(self):
        """ Returns Portals API Base url. """
        return self.__purl
    def vendor(self):
        """ Returns Exosite Vendor. """
        return self.__vendor
    def portal_id(self):
        """ Returns numerical Portal ID. """
        return self.__portal_id
    # def portal_rid(self):
    #   return self.__portal_rid
    def portal_cik(self):
        """ Returns numerical Portal CIK. """
        return self.__portal_cik
    def portal_name(self):
        """ Returns numerical Portal name. """
        return self.__portal_name

    # #####################################################
    #   Portals API method abstractions.
    # #####################################################
    def get_user_token(self):
        """
            Gets a authorization token for session reuse.

            http://docs.exosite.com/portals/#get-user-token-for-openid-user
        """
        headers = { 'User-Agent': self.user_agent(),
                'Host': self.domain(),
                'Accept': '*/*',
        }
        headers.update(self.headers())
        r = requests.get(   self.portals_url()+'/users/_this/token',
                            headers=headers,
                            auth=self.auth())
        if HTTP_STATUS.OK == r.status_code:
            return r.text
        else:
            print("get_user_token: Something went wrong: <{0}>: {1}".format(
                        r.status_code, r.reason))
            r.raise_for_status()

    def get_domain_portal_ids(self):
        """
            Gets the list of Portal ids.

            http://docs.exosite.com/portals/#list-portal-by-domain
        """
        headers = { 'User-Agent': self.user_agent(),
                'Host': self.domain(),
                'Accept': '*/*'
        }
        headers.update(self.headers())

        r = requests.get(   self.portals_url()+'/portals',
                            headers=headers,
                            auth=self.auth())
        if HTTP_STATUS.OK == r.status_code:
            return [ _id['id'] for _id in r.json() ]
        else:
            print("get_user_portals: Something went wrong: <{0}>: {1}".format(
                        r.status_code, r.reason))
            r.raise_for_status()
{'fraction_non_alphanumeric': 0.13807383910476695, 'fraction_numerical': 0.0024671777249096836, 'mean_word_length': 1.9072745901639345, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '3073038', 'n_tokens_mistral': 3136, 'n_tokens_neox': 3074, 'n_words': 740}