Akron | ca9bd98 | 2016-12-06 16:59:57 +0100 | [diff] [blame^] | 1 | import logging |
| 2 | |
| 3 | from flask import flash |
| 4 | import requests.exceptions |
| 5 | |
| 6 | from config import URIBuilder |
| 7 | |
| 8 | __author__ = 'hanl' |
| 9 | |
| 10 | logging.basicConfig(level=logging.DEBUG) |
| 11 | |
| 12 | |
| 13 | def get(path="", **kwargs): |
| 14 | return request('get', path, **kwargs) |
| 15 | |
| 16 | |
| 17 | def post(path="", **kwargs): |
| 18 | path = URIBuilder().addPath(path).build() |
| 19 | if 'headers' in kwargs: |
| 20 | headers = kwargs['headers'] |
| 21 | headers["Content-Type"] = "application/x-www-form-urlencoded" |
| 22 | else: |
| 23 | headers = {"Content-Type": "application/x-www-form-urlencoded"} |
| 24 | kwargs['headers'] = headers |
| 25 | return request('post', path, **kwargs) |
| 26 | |
| 27 | |
| 28 | def request(method, path="", **kwargs): |
| 29 | path = URIBuilder().addPath(path).build() |
| 30 | if kwargs and path is not "": |
| 31 | try: |
| 32 | response = requests.request(method, path, **kwargs) |
| 33 | except requests.exceptions.RequestException as e: |
| 34 | print e |
| 35 | flash("Connection to API server could not be established", "danger") |
| 36 | return None |
| 37 | print "the response %i:%s" % (response.status_code, str(response.content)) |
| 38 | return response |
| 39 | return None |