import traceback
import logging
import requests
import sys

ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
dl = logging.getLogger(__name__)
dl.addHandler(ch)

CONF = {
    'baseurl': 'https://localhost:6500',
    'token': None,
    'prefix': '',
    'set':'raw',
    'headers':{},
    'logger': dl,
    'verify': False,
    'timeout':15
}

class kvdn_client:
    kvdn_py_version = 1.6
    def __init__(self, **kwargs):
        for key,value in kwargs.iteritems():
            CONF[key]=value
        if CONF['token'] is not None:
            CONF['headers'].update({'Authorization': 'Bearer %s' % CONF['token']})

    def set(self, straddr, key, data, **kwargs):
        if CONF['set'] == 'raw':
            return self.setRaw(straddr,key,data,**kwargs)
        elif CONF['set'] == 'json':
            return self.setJson(straddr,key,data,**kwargs)

    def get(self, straddr, key, **kwargs):
        resp, content = kvdn_req(CONF['baseurl'] +CONF['prefix'] +'/X/'+ straddr +'/' +key, headers=CONF['headers'])
        return content

    def setRaw(self, straddr, key, data, **kwargs):
        resp, content = kvdn_req(CONF['baseurl'] +CONF['prefix'] +'/R/'+ straddr +'/' +key, 'PUT', data=data, headers=CONF['headers'])
        return content
    def setJson(self, straddr, key, data, **kwargs):
        resp, content = kvdn_req(CONF['baseurl'] +CONF['prefix'] +'/X/'+ straddr +'/' +key, 'PUT', data=data, headers=CONF['headers'])
        return content

    def getKeys(self, straddr, **kwargs):
        resp, content = kvdn_req(CONF['baseurl'] +CONF['prefix'] +'/KEYS/'+ straddr +'/' ,  headers=CONF['headers'])
        return content

    def delete(self, straddr, key, **kwargs):
        resp, content = kvdn_req(CONF['baseurl'] +CONF['prefix'] +'/X/'+ straddr +'/' +key, 'DELETE', data='', headers=CONF['headers'])
        return content

    def submit_cas(self, straddr, data, **kwargs):
        resp, content = kvdn_req(CONF['baseurl'] +CONF['prefix'] +'/X/'+ straddr , 'POST', data=data, headers=CONF['headers'])
        return content #the returned content should be the hash of data as a key

    def submit_uuid(self, straddr, data, **kwargs):
        resp, content=kvdn_req(CONF['baseurl'] +CONF['prefix'] +'/U/'+ straddr , 'POST', data=data, headers=CONF['headers'])
        return content # the returned content should be a uuid key


def kvdn_req(url, method=None, **kwargs):
    if method is None:
        method = 'GET'
    req = requests.Request(method,url,**kwargs)
    try:
        p = req.prepare()
        s=requests.Session()
        resp=s.send(p,verify=CONF['verify'],timeout=CONF['timeout'])
        return resp, resp.text
    except :
        CONF['logger'].error("could not make kvdn request " +  traceback.format_exc() )
        return "", ""
