Beispiel #1
0
def main():

    parser = argparse.ArgumentParser(description='Generate Postman collections from Apiary API/Blueprint', formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog='Examples:\n\t' + argv[0] + ' --pretty api awattgarde\n\t' 
            + argv[0] + ' --only-collection --output awattgarde_collection.postman api awattgarde\n\t'
            + argv[0] + ' --only-collection json blueprint.json\n\t'
            +'cat blueprint.json | ' + argv[0] + ' json > awattgarde_collection.postman\n\t'  )


    subparsers = parser.add_subparsers(help='',title='subcommands', 
                                   description='valid subcommands')

    parser_blueprint = subparsers.add_parser('blueprint', description='blueprint: Read Blueprint API markup, then convert it using drafter, then generate Postman collection JSON.',
        help='Read Blueprint API markup, then convert it using drafter, then generate Postman collection JSON. Use "blueprint -h" for more help.')

    parser_json = subparsers.add_parser('json', description='json: Use prepared JSON to generate the Postman collection.',help='Use prepared JSON. Use "json -h" for more help.')
    
    parser_api = subparsers.add_parser('api', description='api: Use the Apiary API to fetch the Blueprint markup, then convert it using drafter, then generate Postman collection JSON.',
        help='Use the Apiary API to fetch the JSON. Use "api -h" for more help.')

    parser.add_argument('--pretty', dest='pretty', action='store_true', default=False,
                        help='generate pretty JSON')
    parser.add_argument('--only-collection', dest='only_collection', action='store_const', 
                        const=True, default=False,
                        help='generate Postman JSON for the first collection only.')
    parser_api.add_argument('key', metavar='api-key', nargs='?',
                        help='the Apiary API token. If not supplied APIARY_API_KEY environment variable is used.') 
    parser_api.add_argument('name', metavar='api-name', nargs=1,
                        help='the name of the api on apiary. I.e. testapi311 for http://docs.testapi311.apiary.io/') 
    parser_json.add_argument('input', metavar='input', type=file, nargs='?', default=stdin,
                        help='input file, formatted as JSON. If not supplied, stdin is used.') 
    parser_blueprint.add_argument('blueprint_input', metavar='input', type=file, nargs='?', default=stdin,
                        help='input file, formatted as Blueprint API Markup. If not supplied, stdin is used.') 
    parser.add_argument('--output', metavar='output', type=argparse.FileType('w'), nargs=1, default=stdout,
                        help='output file. Outputs Postman collection JSON. If not supplied, stdout is used.')

    args = parser.parse_args()

    input = ''
    if hasattr(args, 'input'):
        # JSON mode
        input = args.input.read()
    elif hasattr(args, 'blueprint_input'):
        # blueprint mode
        check_drafter()
        input = blueprint2json(args.blueprint_input.read())
    else:
        # API mode
        check_drafter()

        apikey = None
        if args.key != None:
          apikey = args.key
        else:
            apikey = os.environ.get('APIARY_API_KEY')

        if apikey == None:
            print 'Please provide an api-key or set APIARY_API_KEY'
            exit(4)

        blueprint = fetch_blueprint(args.name[0], apikey)
        input = blueprint2json(blueprint)
        
    output = args.output    
    if args.output != stdout:
        output = output[0]

    write(input, output, args.only_collection, args.pretty)
Beispiel #2
0
def main():

    parser = argparse.ArgumentParser(
        description='Generate Postman collections from Apiary API/Blueprint',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog='Examples:\n\t' + argv[0] + ' --pretty api awattgarde\n\t' +
        argv[0] +
        ' --only-collection --output awattgarde_collection.postman api awattgarde\n\t'
        + argv[0] + ' --only-collection json blueprint.json\n\t' +
        'cat blueprint.json | ' + argv[0] +
        ' json > awattgarde_collection.postman\n\t')

    subparsers = parser.add_subparsers(help='',
                                       title='subcommands',
                                       description='valid subcommands')

    parser_blueprint = subparsers.add_parser(
        'blueprint',
        description=
        'blueprint: Read Blueprint API markup, then convert it using drafter, then generate Postman collection JSON.',
        help=
        'Read Blueprint API markup, then convert it using drafter, then generate Postman collection JSON. Use "blueprint -h" for more help.'
    )

    parser_json = subparsers.add_parser(
        'json',
        description=
        'json: Use prepared JSON to generate the Postman collection.',
        help='Use prepared JSON. Use "json -h" for more help.')

    parser_api = subparsers.add_parser(
        'api',
        description=
        'api: Use the Apiary API to fetch the Blueprint markup, then convert it using drafter, then generate Postman collection JSON.',
        help='Use the Apiary API to fetch the JSON. Use "api -h" for more help.'
    )

    parser.add_argument('--pretty',
                        dest='pretty',
                        action='store_true',
                        default=False,
                        help='generate pretty JSON')
    parser.add_argument(
        '--only-collection',
        dest='only_collection',
        action='store_const',
        const=True,
        default=False,
        help='generate Postman JSON for the first collection only.')
    parser_api.add_argument(
        'key',
        metavar='api-key',
        nargs='?',
        help=
        'the Apiary API token. If not supplied APIARY_API_KEY environment variable is used.'
    )
    parser_api.add_argument(
        'name',
        metavar='api-name',
        nargs=1,
        help=
        'the name of the api on apiary. I.e. testapi311 for http://docs.testapi311.apiary.io/'
    )
    parser_json.add_argument(
        'input',
        metavar='input',
        type=file,
        nargs='?',
        default=stdin,
        help='input file, formatted as JSON. If not supplied, stdin is used.')
    parser.add_argument(
        '--combine',
        dest='combine',
        help='combine all collections into a a single top-level collection')
    parser.add_argument(
        '--exclude',
        dest='exclude',
        nargs='+',
        help=
        'exclude collections containing the provided (case-insensitive) substrings'
    )
    parser_blueprint.add_argument(
        'blueprint_input',
        metavar='input',
        type=file,
        nargs='?',
        default=stdin,
        help=
        'input file, formatted as Blueprint API Markup. If not supplied, stdin is used.'
    )
    parser.add_argument(
        '--output',
        metavar='output',
        type=argparse.FileType('w'),
        nargs=1,
        default=stdout,
        help=
        'output file. Outputs Postman collection JSON. If not supplied, stdout is used.'
    )

    args = parser.parse_args()

    input = ''
    if hasattr(args, 'input'):
        # JSON mode
        input = args.input.read()
    elif hasattr(args, 'blueprint_input'):
        # blueprint mode
        check_drafter()
        input = blueprint2json(args.blueprint_input.read())
    else:
        # API mode
        check_drafter()

        apikey = None
        if args.key != None:
            apikey = args.key
        else:
            apikey = os.environ.get('APIARY_API_KEY')

        if apikey == None:
            print 'Please provide an api-key or set APIARY_API_KEY'
            exit(4)

        blueprint = fetch_blueprint(args.name[0], apikey)
        input = blueprint2json(blueprint)

    output = args.output
    if args.output != stdout:
        output = output[0]

    # unmarshal from json string
    json_obj = json.loads(input)

    if args.only_collection:
        # return only the first collection
        json_obj = converter.first_collection(json_obj)
    else:
        json_obj = converter.full_response(json_obj)

    # exclude collections by name
    if args.exclude != None and len(args.exclude) > 0:
        converter.filter_collections(json_obj, args.exclude)

    # combine all collections into a top-level one, removing existing folders
    if args.combine != '':
        converter.combine_collections(json_obj, args.combine)

    # write json object out to configured destination, perhaps with whitespace
    converter.write(json_obj, out=output, pretty=args.pretty)
def main():

    parser = argparse.ArgumentParser(description='Generate Postman collections from Apiary API/Blueprint', formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog='Examples:\n\t' + argv[0] + ' --pretty api awattgarde\n\t' 
            + argv[0] + ' --only-collection --output awattgarde_collection.postman api awattgarde\n\t'
            + argv[0] + ' --only-collection json blueprint.json\n\t'
            +'cat blueprint.json | ' + argv[0] + ' json > awattgarde_collection.postman\n\t'  )


    subparsers = parser.add_subparsers(help='',title='subcommands', 
                                   description='valid subcommands')

    parser_blueprint = subparsers.add_parser('blueprint', description='blueprint: Read Blueprint API markup, then convert it using drafter, then generate Postman collection JSON.',
        help='Read Blueprint API markup, then convert it using drafter, then generate Postman collection JSON. Use "blueprint -h" for more help.')

    parser_json = subparsers.add_parser('json', description='json: Use prepared JSON to generate the Postman collection.',help='Use prepared JSON. Use "json -h" for more help.')
    
    parser_api = subparsers.add_parser('api', description='api: Use the Apiary API to fetch the Blueprint markup, then convert it using drafter, then generate Postman collection JSON.',
        help='Use the Apiary API to fetch the JSON. Use "api -h" for more help.')

    parser.add_argument('--pretty', dest='pretty', action='store_true', default=False,
                        help='generate pretty JSON')
    parser.add_argument('--only-collection', dest='only_collection', action='store_const', 
                        const=True, default=False,
                        help='generate Postman JSON for the first collection only.')
    parser_api.add_argument('key', metavar='api-key', nargs='?',
                        help='the Apiary API token. If not supplied APIARY_API_KEY environment variable is used.') 
    parser_api.add_argument('name', metavar='api-name', nargs=1,
                        help='the name of the api on apiary. I.e. testapi311 for http://docs.testapi311.apiary.io/') 
    parser_json.add_argument('input', metavar='input', type=file, nargs='?', default=stdin,
                        help='input file, formatted as JSON. If not supplied, stdin is used.') 
    parser.add_argument('--combine', dest='combine',
                        help='combine all collections into a a single top-level collection')
    parser.add_argument('--exclude', dest='exclude', nargs='+',
                        help='exclude collections containing the provided (case-insensitive) substrings')
    parser_blueprint.add_argument('blueprint_input', metavar='input', type=file, nargs='?', default=stdin,
                        help='input file, formatted as Blueprint API Markup. If not supplied, stdin is used.') 
    parser.add_argument('--output', metavar='output', type=argparse.FileType('w'), nargs=1, default=stdout,
                        help='output file. Outputs Postman collection JSON. If not supplied, stdout is used.')

    args = parser.parse_args()

    input = ''
    if hasattr(args, 'input'):
        # JSON mode
        input = args.input.read()
    elif hasattr(args, 'blueprint_input'):
        # blueprint mode
        check_drafter()
        input = blueprint2json(args.blueprint_input.read())
    else:
        # API mode
        check_drafter()

        apikey = None
        if args.key != None:
          apikey = args.key
        else:
            apikey = os.environ.get('APIARY_API_KEY')

        if apikey == None:
            print 'Please provide an api-key or set APIARY_API_KEY'
            exit(4)

        blueprint = fetch_blueprint(args.name[0], apikey)
        input = blueprint2json(blueprint)

        
    output = args.output    
    if args.output != stdout:
        output = output[0]

    # unmarshal from json string
    json_obj = json.loads(input)

    if args.only_collection:
      # return only the first collection
      json_obj = converter.first_collection(json_obj)
    else:
      json_obj = converter.full_response(json_obj)

    # exclude collections by name
    if args.exclude != None and len(args.exclude) > 0:
      converter.filter_collections(json_obj, args.exclude)

    # combine all collections into a top-level one, removing existing folders
    if args.combine != '':
      converter.combine_collections(json_obj, args.combine)

    # write json object out to configured destination, perhaps with whitespace
    converter.write(json_obj, out=output, pretty=args.pretty)
Beispiel #4
0
def main():

    parser = argparse.ArgumentParser(
        description='Generate Postman collections from Apiary API/Blueprint',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog='Examples:\n\t' + argv[0] + ' --pretty api awattgarde\n\t' +
        argv[0] +
        ' --only-collection --output awattgarde_collection.postman api awattgarde\n\t'
        + argv[0] + ' --only-collection json blueprint.json\n\t' +
        'cat blueprint.json | ' + argv[0] +
        ' json > awattgarde_collection.postman\n\t')

    subparsers = parser.add_subparsers(help='',
                                       title='subcommands',
                                       description='valid subcommands')

    parser_blueprint = subparsers.add_parser(
        'blueprint',
        description=
        'blueprint: Read Blueprint API markup, then convert it using snowcrash, then generate Postman collection JSON.',
        help=
        'Read Blueprint API markup, then convert it using snowcrash, then generate Postman collection JSON. Use "blueprint -h" for more help.'
    )

    parser_json = subparsers.add_parser(
        'json',
        description=
        'json: Use prepared JSON to generate the Postman collection.',
        help='Use prepared JSON. Use "json -h" for more help.')

    parser_api = subparsers.add_parser(
        'api',
        description=
        'api: Use the Apiary API to fetch the Blueprint markup, then convert it using snowcrash, then generate Postman collection JSON.',
        help='Use the Apiary API to fetch the JSON. Use "api -h" for more help.'
    )

    parser.add_argument('--pretty',
                        dest='pretty',
                        action='store_true',
                        default=False,
                        help='generate pretty JSON')
    parser.add_argument(
        '--only-collection',
        dest='only_collection',
        action='store_const',
        const=True,
        default=False,
        help='generate Postman JSON for the first collection only.')
    parser_api.add_argument(
        'key',
        metavar='api-key',
        nargs='?',
        help=
        'the Apiary API token. If not supplied APIARY_API_KEY environment variable is used.'
    )
    parser_api.add_argument(
        'name',
        metavar='api-name',
        nargs=1,
        help=
        'the name of the api on apiary. I.e. testapi311 for http://docs.testapi311.apiary.io/'
    )
    parser_json.add_argument(
        'input',
        metavar='input',
        type=file,
        nargs='?',
        default=stdin,
        help='input file, formatted as JSON. If not supplied, stdin is used.')
    parser_blueprint.add_argument(
        'blueprint_input',
        metavar='input',
        type=file,
        nargs='?',
        default=stdin,
        help=
        'input file, formatted as Blueprint API Markup. If not supplied, stdin is used.'
    )
    parser.add_argument(
        '--output',
        metavar='output',
        type=argparse.FileType('w'),
        nargs=1,
        default=stdout,
        help=
        'output file. Outputs Postman collection JSON. If not supplied, stdout is used.'
    )

    args = parser.parse_args()

    input = ''
    if hasattr(args, 'input'):
        # JSON mode
        input = args.input.read()
    elif hasattr(args, 'blueprint_input'):
        # blueprint mode
        check_snowcrash()
        input = blueprint2json(args.blueprint_input.read())
    else:
        # API mode
        check_snowcrash()

        apikey = None
        if args.key != None:
            apikey = args.key
        else:
            apikey = os.environ.get('APIARY_API_KEY')

        if apikey == None:
            print 'Please provide an api-key or set APIARY_API_KEY'
            exit(4)

        blueprint = fetch_blueprint(args.name[0], apikey)
        input = blueprint2json(blueprint)

    output = args.output
    if args.output != stdout:
        output = output[0]

    write(input, output, args.only_collection, args.pretty)