summaryrefslogtreecommitdiffstats
path: root/pykolab/wap_client/request.py
blob: 06133336cddd56290f3922d65a4df2e907b75dff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def request(method, api_uri, params=None, headers={}):
    global session_id

    if not session_id == None:
        headers["X-Session-Token"] = session_id

    conn = connect()
    conn.request(method.upper(), "%s/%s" % (API_BASE,api_uri), params, headers)
    response = conn.getresponse()
    data = response.read()

    print method, api_uri, params
    print data

    try:
        response_data = json.loads(data)
    except ValueError, e:
        # Some data is not JSON
        print "Response data is not JSON"
        sys.exit(1)

    print response_data

    if response_data['status'] == "OK":
        del response_data['status']
        return response_data['result']
    else:
        return response_data['result']