aboutsummaryrefslogtreecommitdiff
blob: af8dab2422124e35f4ca76954f3b6c2df3124b0f (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
30
31
32
33
34
35
36
37
import json
try:
    import http.client as httplib
except ImportError:
    import httplib

# json headers for gentoostats-cli
headers = {'Accept': 'application/json'}

def GET(server, url, headers, https=True):
    """
    Get url from server using headers 
    """
    if https:
        conn = httplib.HTTPSConnection(server)
    else:
        conn = httplib.HTTPConnection(server)
    try:
        conn.request('GET', url=url, headers=headers)
        data = conn.getresponse().read().decode("utf-8")
    except httplib.HTTPException:
        return None
    finally:
        if conn:
            conn.close()
    return data

def deserialize(object):
    """
    Decode json object
    """
    try:
        decoded = json.JSONDecoder().decode(object)
    except (ValueError, TypeError):
        return None
    return decoded