| 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 |