blob: 8be3073233c342e953322aa7a02d84f3fde6fb66 [file] [log] [blame]
Akronca9bd982016-12-06 16:59:57 +01001import logging
2
3from flask import flash
4import requests.exceptions
5
6from config import URIBuilder
7
8__author__ = 'hanl'
9
10logging.basicConfig(level=logging.DEBUG)
11
12
13def get(path="", **kwargs):
14 return request('get', path, **kwargs)
15
16
17def 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
28def 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