import logging

from flask import flash
import jwt
import requests.exceptions
from requests.auth import AuthBase

import config


__author__ = 'hanl'

logging.basicConfig(level=logging.DEBUG)


def get(path="", **kwargs):
    path = config.URIBuilder().addPath(path).build()
    if kwargs and path is not "":
        try:
            response = requests.get(path, **kwargs)
        except requests.exceptions.RequestException as e:
            print e
            flash("Connection to API server could not be established", "danger")
            return None
        print "the response %i:%s" % (response.status_code, str(response.content))
        return response
    return None


def post(path="", **kwargs):
    path = config.URIBuilder().addPath(path).build()
    if 'headers' in kwargs:
        headers = kwargs['headers']
        headers["Content-Type"] = "application/x-www-form-urlencoded"
    else:
        headers = {"Content-Type": "application/x-www-form-urlencoded"}
    kwargs['headers'] = headers

    if kwargs and path is not "":
        try:
            response = requests.post(path, **kwargs)
        except requests.exceptions.RequestException as e:
            print e
            flash("Connection to API server could not be established", "danger")
            return None
        print "the response %i:%s" % (response.status_code, str(response.content))
        return response
    return None


def trace(path="", **kwargs):
    path = config.URIBuilder().addPath(path).build()
    if path is not "":
        try:
            return requests.request("trace", path, **kwargs)
        except requests.exceptions.RequestException as e:
            print e
            flash("Connection to API server could not be established", "danger")
            return None
    return None


def decrypt_openid(secret=config.SECRET_KEY, token=None):
    values = jwt.decode(jwt=token, key=secret)
    print "decoded values %s" % str(values)
    return values


def encrypt_string(secret=config.SECRET_KEY, *args):
    jwt_string = jwt.encode(args, key=secret)
    print "encoded string %s" % str(jwt_string)
    return jwt_string


class Oauth2Auth(AuthBase):
    def __init__(self, token=None):
        self.token = token

    def __call__(self, r):
        if self.token is not None:
            r.headers['Authorization'] = "OAuth2 Bearer " + self.token
        return r


class CustomAuth(AuthBase):
    def __init__(self, token=None):
        self.token = token

    def __call__(self, r):
        if self.token is not None:
            r.headers['Authorization'] = "api_token " + self.token
        return r
