import abc
import requests
import urllib
from requests.packages import urllib3
urllib3.disable_warnings()


class ConfigValue(object):

    def getattr(self, attr):
        try:
            return self.__getattribute__(attr)
        except:
            return None

    def setattr(self, attr, value):
        self.__setattr__(attr, value)


class Workitem(ConfigValue):
    def __repr__(self):
        return "<Workitem {0.id}>".format(self)

    @property
    def id(self):
        return self.identifier


class RTCBase(object):
    __metaclass__ = abc.ABCMeta

    CONTENT_XML = "text/xml"
    CONTENT_JSON = "application/json"
    CONTENT_URL_ENCODED = 'application/x-www-form-urlencoded'
    OSLC_CR_XML = "application/x-oslc-cm-change-request+xml"
    OSLC_CR_JSON = "application/x-oslc-cm-change-request+json"

    def __init__(self, baseurl, username, password):
        self.baseurl = baseurl
        self.username = username
        self.password = password
        self.headers = self._get_headers()

    def _get_headers(self):
        _headers = {'Content-type': RTCBase.CONTENT_XML}
        resp = requests.get(self.baseurl + "/authenticated/identity",
                            verify=False,
                            headers=_headers,
                            timeout=60)

        _headers['Content-type'] = RTCBase.CONTENT_URL_ENCODED
        _headers['Cookie'] = resp.headers.get('set-cookie')
        credentials = urllib.urlencode({'j_username': self.username,
                                        'j_password': self.password})

        resp = requests.post(self.baseurl+'/authenticated/j_security_check',
                             data=credentials,
                             verify=False,
                             headers=_headers)

        resp = requests.get(self.baseurl + "/authenticated/identity",
                            verify=False,
                            headers=_headers,
                            timeout=60)

        _headers['Cookie'] = resp.headers.get('set-cookie')
        _headers['Accept'] = RTCBase.CONTENT_XML
        return _headers

    @abc.abstractproperty
    def workitems(self):
        pass

    @abc.abstractmethod
    def get(self, req_url):
        pass

    @abc.abstractmethod
    def post(self, req_url):
        pass

import abc
import logging


class RTCBase(object):
    __metaclass__ = abc.ABCMeta

    CONTENT_XML = "text/xml"
    CONTENT_JSON = "application/json"
    CONTENT_URL_ENCODED = 'application/x-www-form-urlencoded'
    OSLC_CR_XML = "application/x-oslc-cm-change-request+xml"
    OSLC_CR_JSON = "application/x-oslc-cm-change-request+json"

    def __init__(self, baseurl):
        self.baseurl = self.validate_url(baseurl)

    def __repr__(self):
        return "<%s %s>" % (self.__class__.__name__,
                            str(self))

    @abc.abstractmethod
    def __str__(self):
        pass

    def __eq__(self, other):
        """
        identify whether the other one represents a connection to the server
        """
        if not isinstance(other, self.__class__):
            return False
        if not other.baseurl == self.baseurl:
            return False
        return True

    def getattr(self, attr):
        try:
            return self.__getattribute__(attr)
        except:
            return None

    def setattr(self, attr, value):
        self.__setattr__(attr, value)

    @abc.abstractmethod
    def get_rtc_obj(self):
        pass

    def get_response(self, req_url, ssl_verify=False, params=None):
        requester = self.get_rtc_obj().requester
        response = requester.get_url(req_url, ssl_verify, params)
        if response.status_code != 200:
            logging.error('Failed request at <%s> with params: %s and ssl %s',
                          req_url,
                          params,
                          "enabled" if ssl_verify else "disabled")
            response.raise_for_status()
        return response

    @classmethod
    def validate_url(cls, url):
        """
        strip and trailing slash to validate a url
        """
        url = url.strip()
        while url.endswith('/'):
            url = url[:-1]
        return url

refactor base class