Example #1
0
def response(uri, method, res, token='', keyword='',
             content='', raw_flag=False):
    """Response of tonicdns_client request

    Arguments:

        uri:      TonicDNS API URI
        method:   TonicDNS API request method
        res:      Response of against request to TonicDNS API
        token:    TonicDNS API token
        keyword:  Processing keyword
        content:  JSON data
        raw_flag: True is return responsed raw data, False is pretty print
    """
    if method == 'GET' or (method == 'PUT' and not token):
        # response body
        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

        if keyword == 'serial':
            # filtering with keyword
            record = search_record(datas, 'SOA')[0]

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

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

            c = JSONConverter(content['domain'])
            new_record = c.get_soa(record, content)
            return record, new_record

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

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

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

            else:
                # when get all templates
                for data in datas:
                    #print_formatted(data)
                    utils.pretty_print(datas)

        else:
            # 'get' subcommand
            if raw_flag:
                return datas
            else:
                #print_formatted(datas)
                if len(uri.split('zone/')) > 1:
                    domain = uri.split('zone/')[1]
                else:
                    domain = ''
                utils.pretty_print(datas, keyword, domain)

    else:
        # response non JSON data
        data = res.read()
        print(data)
Example #2
0
def response(uri,
             method,
             res,
             token='',
             keyword='',
             content='',
             raw_flag=False):
    """Response of tonicdns_client request

    Arguments:

        uri:      TonicDNS API URI
        method:   TonicDNS API request method
        res:      Response of against request to TonicDNS API
        token:    TonicDNS API token
        keyword:  Processing keyword
        content:  JSON data
        raw_flag: True is return responsed raw data, False is pretty print
    """
    if method == 'GET' or (method == 'PUT' and not token):
        # response body
        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

        if keyword == 'serial':
            # filtering with keyword
            record = search_record(datas, 'SOA')[0]

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

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

            c = JSONConverter(content['domain'])
            new_record = c.get_soa(record, content)
            return record, new_record

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

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

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

            else:
                # when get all templates
                for data in datas:
                    #print_formatted(data)
                    utils.pretty_print(datas)

        else:
            # 'get' subcommand
            if raw_flag:
                return datas
            else:
                #print_formatted(datas)
                if len(uri.split('zone/')) > 1:
                    domain = uri.split('zone/')[1]
                else:
                    domain = ''
                utils.pretty_print(datas, keyword, domain)

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