Esempio n. 1
0
def response(uri, method, res, token="", keyword="", domain=""):
    import json

    # response body
    if method == "GET" or (method == "PUT" and not token):

        data = res.read()
        data_utf8 = data.decode("utf-8")
        if token:
            datas = json.loads(data_utf8)
        else:
            token = json.loads(data_utf8)["hash"]
            return token

        # filtering with keyword
        if keyword == "serial":
            from converter import JSONConvert

            record = searchRecord(datas, "SOA")[0]

            # if SOA record, remove priority unnecessary
            del record["priority"]

            # override ttl
            record["ttl"] = int(record["ttl"])

            c = JSONConvert(domain)
            new_record = c.getSOA(record)
            return record, new_record

        # '--search' option of 'get' subcommand
        elif keyword:
            records = searchRecord(datas, keyword)
            datas.update({"records": records})

        # 'tmpl_get' subcommand
        if uri.split("/")[3] == "template":

            # when specify template identfier
            if len(uri.split("/")) == 5:
                formattedPrint(datas)

            # when get all templates
            else:
                for data in datas:
                    formattedPrint(data)

        # 'get' subcommand
        else:
            formattedPrint(datas)

    # response non JSON data
    else:
        data = res.read()
        print(data)
Esempio n. 2
0
def tonicDNSClient(uri, method, token, data, keyword='', domain=''):
    import sys
    import json
    if sys.version_info > (2, 6) and sys.version_info < (2, 8):
        import urllib2 as urllib
    elif sys.version_info > (3, 0):
        import urllib.request as urllib

    encoded = json.JSONEncoder().encode(data)
    o = urllib.build_opener(urllib.HTTPHandler)
    r = urllib.Request(uri, data=encoded.encode('utf-8'))

    r.add_header('x-authentication-token', token)

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

    r.get_method = lambda: method
    try:
        url = o.open(r)
    except urllib.HTTPError as e:
        sys.stderr.write("ERROR: %s\n" % e)
        exit(1)

    # response body
    if method == 'GET':
        datas = json.loads(url.read().decode('utf-8'))
        # filtering with keyword
        if keyword == 'serial':
            from converter import JSONConvert
            record = searchRecord(datas, 'SOA')[0]
            del record['priority']
            record['ttl'] = int(record['ttl'])
            c = JSONConvert(domain)
            new_record = c.getSOA(record)
            return record, new_record
        elif keyword:
            records = searchRecord(datas, keyword)
            datas.update({"records": records})
        if uri.split('/')[3] == 'template':
            if len(uri.split('/')) == 5:
                formattedPrint(datas)
            else:
                for data in datas:
                    formattedPrint(data)
        else:
            formattedPrint(datas)
    else:
        data = url.read()
        print(data)
Esempio n. 3
0
def tonicDNSClient(uri, method, token, data, keyword='', domain=''):
    import sys
    import json
    if sys.version_info > (2, 6) and sys.version_info < (2, 8):
        import urllib2 as urllib
    elif sys.version_info > (3, 0):
        import urllib.request as urllib

    encoded = json.JSONEncoder(object).encode(data)
    obj = urllib.build_opener(urllib.HTTPHandler)
    req = urllib.Request(uri, data=encoded.encode('utf-8'))
    req.add_header('x-authentication-token', token)

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

    req.get_method = lambda: method

    try:
        res = obj.open(req)

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

    # response body
    if method == 'GET':

        data = res.read()
        datas = json.loads(data.decode('utf-8'))

        # filtering with keyword
        if keyword == 'serial':
            from converter import JSONConvert
            record = searchRecord(datas, 'SOA')[0]

            # if SOA record, remove priority unnecessary
            del record['priority']

            # override ttl
            record['ttl'] = int(record['ttl'])

            c = JSONConvert(domain)
            new_record = c.getSOA(record)
            return record, new_record

        # '--search' option of 'get' subcommand
        elif keyword:
            records = searchRecord(datas, keyword)
            datas.update({"records": records})

        # 'tmpl_get' subcommand
        if uri.split('/')[3] == 'template':

            # when specify template identfier
            if len(uri.split('/')) == 5:
                formattedPrint(datas)

            # when get all templates
            else:
                for data in datas:
                    formattedPrint(data)

        # 'get' subcommand
        else:
            formattedPrint(datas)

    # response non JSON data
    else:
        data = res.read()
        print(data)