Request to TonicDNS API.

    Arguments:

        uri:     TonicDNS API URI
        method:  TonicDNS API request method
        data:    Post data to TonicDNS API
        token:   TonicDNS API authentication token

def request(uri, method, data, token=''):
    """Request to TonicDNS API.

    Arguments:

        uri:     TonicDNS API URI
        method:  TonicDNS API request method
        data:    Post data to TonicDNS API
        token:   TonicDNS API authentication token
    """
    socket.setdefaulttimeout(__timeout__)

    obj = urllib.build_opener(urllib.HTTPHandler)

    # encoding json
    encoded = json.JSONEncoder(object).encode(data)

    # encoding utf8
    data_utf8 = encoded.encode('utf-8')
    req = urllib.Request(uri, data=data_utf8)

    # When encoded(=data) is False, retrieve data as GET method.
    if encoded:
        req.add_header('Content-Type', 'application/json')

    if token:
        req.add_header('x-authentication-token', token)

    req.get_method = lambda: method

    try:
        res = obj.open(req)
        return res

    except urllib.URLError as e:
        sys.stderr.write("ERROR: %s\n" % e)
        exit(1)

    except urllib.HTTPError as e:
        sys.stderr.write("ERROR: %s\n" % e)
        exit(1)