#!/usr/bin/env python

import socket
import json

from dm.common.utility.loggingManager import LoggingManager
from dm.common.client.sessionManager import SessionManager
from dm.common.utility.configurationManager import ConfigurationManager 
from dm.common.exceptions.authorizationError import AuthorizationError
from dm.common.api.dmApi import DmApi

class DmRestApi(DmApi):
    """ Base dm REST api class. """

    def __init__(self, username=None, password=None, host=None, port=None, protocol=None):
        DmApi.__init__(self)
        self.configurationManager = ConfigurationManager.getInstance()
        if protocol == None:
            protocol = self.configurationManager.getServiceProtocol()
        self.username = username
        self.password = password
        self.host = host
        self.port = port
        self.protocol = protocol
        self.sessionManager = None

    @classmethod
    def toJson(cls, o):
        return json.dumps(o)

    @classmethod
    def fromJson(cls, s):
        return json.loads(s)

    def __getWebServiceUrl(self, url):
        if url.find('://') < 0:
            return '%s://%s:%s' % (self.protocol, socket.gethostbyname(self.host), self.port)

        # Break the URL down into component pieces
        from urlparse import urlparse
        o = urlparse(url)
        wsUrl = '%s://%s' % (o[0], o[1])
        return wsUrl

    def getContextRoot(self):
        return self.configurationManager.getContextRoot()

    def setUsername(self, username):
        self.username = username

    def getUsername(self):
        return self.username

    def setPassword(self, password):
        self.password = password

    def getPassword(self):
        return self.password

    def setHost(self, host):
        self.host = host

    def getHost(self):
        return self.host

    def setPort(self, port):
        self.port = port

    def getPort(self):
        return self.port

    def setProtocol(self, protocol):
        self.protocol = protocol

    def getProtocol(self):
        return self.protocol

    def getSessionManager(self):
        if not self.sessionManager:
            self.sessionManager = SessionManager()
        return self.sessionManager

    def getConfigManager(self):
        return self.configurationManager

    def sendSessionRequest(self, url, method, contentType='html', data={}):
        """ Send authorized session request. """
        sm = self.getSessionManager()
        if not sm.hasSession():
            #if self.username == None:
            #    raise AuthorizationError('Username not supplied.')
            #if self.password == None:
            #    raise AuthorizationError('Password not supplied.')
            wsUrl = self.__getWebServiceUrl(url)
            # establishSession() sets the 'wsUrl' so the explicit call
            # to setHost() is not required
            sm.establishSession(wsUrl, self.username, self.password)
        (response, responseData) = sm.sendSessionRequest(url, method, contentType, data)
        return json.loads(responseData)

    def sendRequest(self, url, method, contentType='html', data={}):
        """ Send non-authorized request. """
        sm = self.getSessionManager()
        # Because there's no call to establishSession(), explicitly call
        # setHost()
        sm.setHost(self.__getWebServiceUrl(url))
        (response, responseData) = self.getSessionManager().sendRequest(url, method, contentType, data)
        return json.loads(responseData)

#######################################################################
# Testing.

if __name__ == '__main__':
    api = DmRestApi('sveseli', 'sveseli')
    #api.sendRequest('https://zagreb.svdev.net:10232/dm/directory/list', 'GET', data='')
    import urllib
    from dm.common.utility.configurationManager import ConfigurationManager
    cm = ConfigurationManager.getInstance()
    cm.setSessionCacheFile('/tmp/session')
    #print 'Non-session request'
    #print api.sendRequest('https://zagreb.svdev.net:10232/dm/directory/list?path=/tmp', 'GET')
    print 'Session request'
    data = { 'path' : '/tmp/xyz' }
    #print api.sendSessionRequest('https://zagreb.svdev.net:10232/dm/file/write?path=/tmp/xyz&content=xyz', 'POST', contentType='application/x-www-form-urlencoded', data=urllib.urlencode(data))
    #print api.sendSessionRequest('https://zagreb.svdev.net:10232/dm/file/write', 'POST', data=data)
    postdata='path=/tmp/xyz'
    postdata+='&content=%s' % urllib.quote_plus('Hey there')
    print api.sendSessionRequest('https://zagreb.svdev.net:10232/dm/file/write', 'POST', contentType='application/x-www-form-urlencoded', data=postdata)