Akron | ca9bd98 | 2016-12-06 16:59:57 +0100 | [diff] [blame] | 1 | import logging |
| 2 | |
| 3 | from flask import flash |
| 4 | import jwt |
| 5 | import requests.exceptions |
| 6 | from requests.auth import AuthBase |
| 7 | |
| 8 | import config |
| 9 | |
| 10 | |
| 11 | __author__ = 'hanl' |
| 12 | |
| 13 | logging.basicConfig(level=logging.DEBUG) |
| 14 | |
| 15 | |
| 16 | def get(path="", **kwargs): |
| 17 | path = config.URIBuilder().addPath(path).build() |
| 18 | if kwargs and path is not "": |
| 19 | try: |
| 20 | response = requests.get(path, **kwargs) |
| 21 | except requests.exceptions.RequestException as e: |
| 22 | print e |
| 23 | flash("Connection to API server could not be established", "danger") |
| 24 | return None |
| 25 | print "the response %i:%s" % (response.status_code, str(response.content)) |
| 26 | return response |
| 27 | return None |
| 28 | |
| 29 | |
| 30 | def post(path="", **kwargs): |
| 31 | path = config.URIBuilder().addPath(path).build() |
| 32 | if 'headers' in kwargs: |
| 33 | headers = kwargs['headers'] |
| 34 | headers["Content-Type"] = "application/x-www-form-urlencoded" |
| 35 | else: |
| 36 | headers = {"Content-Type": "application/x-www-form-urlencoded"} |
| 37 | kwargs['headers'] = headers |
| 38 | |
| 39 | if kwargs and path is not "": |
| 40 | try: |
| 41 | response = requests.post(path, **kwargs) |
| 42 | except requests.exceptions.RequestException as e: |
| 43 | print e |
| 44 | flash("Connection to API server could not be established", "danger") |
| 45 | return None |
| 46 | print "the response %i:%s" % (response.status_code, str(response.content)) |
| 47 | return response |
| 48 | return None |
| 49 | |
| 50 | |
| 51 | def trace(path="", **kwargs): |
| 52 | path = config.URIBuilder().addPath(path).build() |
| 53 | if path is not "": |
| 54 | try: |
| 55 | return requests.request("trace", path, **kwargs) |
| 56 | except requests.exceptions.RequestException as e: |
| 57 | print e |
| 58 | flash("Connection to API server could not be established", "danger") |
| 59 | return None |
| 60 | return None |
| 61 | |
| 62 | |
| 63 | def decrypt_openid(secret=config.SECRET_KEY, token=None): |
| 64 | values = jwt.decode(jwt=token, key=secret) |
| 65 | print "decoded values %s" % str(values) |
| 66 | return values |
| 67 | |
| 68 | |
| 69 | def encrypt_string(secret=config.SECRET_KEY, *args): |
| 70 | jwt_string = jwt.encode(args, key=secret) |
| 71 | print "encoded string %s" % str(jwt_string) |
| 72 | return jwt_string |
| 73 | |
| 74 | |
| 75 | class Oauth2Auth(AuthBase): |
| 76 | def __init__(self, token=None): |
| 77 | self.token = token |
| 78 | |
| 79 | def __call__(self, r): |
| 80 | if self.token is not None: |
| 81 | r.headers['Authorization'] = "OAuth2 Bearer " + self.token |
| 82 | return r |
| 83 | |
| 84 | |
| 85 | class CustomAuth(AuthBase): |
| 86 | def __init__(self, token=None): |
| 87 | self.token = token |
| 88 | |
| 89 | def __call__(self, r): |
| 90 | if self.token is not None: |
| 91 | r.headers['Authorization'] = "api_token " + self.token |
| 92 | return r |