Exemplo n.º 1
0
def main():
    parser = argparse.ArgumentParser(
        description='Creates and adds an attachment to a beacon')
    parser.add_argument(
        '--access-token',
        required=True,
        help=
        'Access token for interacting with the API. Must be tied to a project that has activated the Proximity Beacon API'
    )
    parser.add_argument(
        '--beacon-name',
        required=True,
        help=
        'Name of the beacon to attach to. Format is "beacons/N!<beacon ID>"')
    parser.add_argument('--attachment-data',
                        required=True,
                        help='Un-encoded data for the attachment')
    parser.add_argument(
        '--namespaced-type',
        required=True,
        help=
        'In the from of "namespace/type" for the attachment. Namespace is most likely your project ID'
    )
    parser.add_argument('--project-id',
                        required=False,
                        help='ID for the project ')

    args = parser.parse_args()

    access_token = args.access_token
    pb_client = pbapi.build_client_from_access_token(access_token)

    pb_client.add_attachment(args.beacon_name, args.namespaced_type,
                             args.attachment_data, args.project_id)
Exemplo n.º 2
0
def main():
    parser = argparse.ArgumentParser(description='CLI wrapper for the Proximity Beacon API',
                                     add_help=False)
    parser.add_argument('command',
                        nargs='?', metavar='command', choices=aliases.keys(),
                        help='Name of the Proximity Beacon API method to execute. Supported options are: '
                             + str(aliases.keys()))
    parser.add_argument('--help', '-h',
                        nargs='?', default=False, const=True, metavar='command',
                        help='This help message or the help message for the given command')
    parser.add_argument('--list-commands',
                        action='store_true',
                        help='Lists the Proximity Beacon API methods available to run')
    parser.add_argument('--service-account-creds',
                        help='Path to file containing credentials for a service account. If this is a p12 file, you ' +
                             'must also supply --service-account-email. Otherwise, this is expected to be JSON.')
    parser.add_argument('--service-account-email',
                        help='Client email of the service account to use if --service-account-creds is a p12. Not ' +
                             'needed if using an access token or a JSON service account file.')
    parser.add_argument('--access-token',
                        help='OAuth2 access token ')
    parser.add_argument('--client-secret',
                        help='Path to a JSON file containing oauth client ID secrets.')
    parser.add_argument('--print-results',
                        action='store_true', default=False, help='Print the command\'s return value to stdout.')

    args, extra_args = parser.parse_known_args()

    if args.help:
        handle_help(parser, args)
        exit(0)

    if args.list_commands:
        list_commands()
        exit(0)

    if args.command is None:
        print('[ERROR] command name is required')
        parser.print_help()
        exit(1)

    # TODO: support specifying creds via env vars
    pb_client = None
    if args.service_account_creds is not None and args.service_account_email is not None:
        pb_client = pbapi.build_client_from_p12(args.service_account_creds, args.service_account_email)
    elif args.service_account_creds is not None:
        pb_client = pbapi.build_client_from_json(args.service_account_creds)
    elif args.access_token is not None:
        pb_client = pbapi.build_client_from_access_token(args.access_token)
    elif args.client_secret is not None:
        pb_client = pbapi.build_client_from_client_id_json(args.client_secret)
    else:
        try:
            pb_client = pbapi.build_client_from_app_default()
        except Exception, err:
            # TODO: if no creds found, attempt web-based oauth flow
            print('[ERROR] No usable access credentials specified. Cannot create API client: {}'.format(err.message))
            exit(1)
Exemplo n.º 3
0
def main():
    parser = argparse.ArgumentParser(description='CLI wrapper for the Proximity Beacon API',
                                     add_help=False)
    parser.add_argument('command',
                        nargs='?', metavar='command', choices=aliases.keys(),
                        help='Name of the Proximity Beacon API method to execute. Supported options are: '
                             + str(aliases.keys()))
    parser.add_argument('--help', '-h',
                        nargs='?', default=False, const=True, metavar='command',
                        help='This help message or the help message for the given command')
    parser.add_argument('--list-commands',
                        action='store_true',
                        help='Lists the Proximity Beacon API methods available to run')
    parser.add_argument('--service-account-creds',
                        help='Path to file containing credentials for a service account. If this is a p12 file, you ' +
                             'must also supply --service-account-email. Otherwise, this is expected to be JSON.')
    parser.add_argument('--service-account-email',
                        help='Client email of the service account to use if --service-account-creds is a p12. Not ' +
                             'needed if using an access token or a JSON service account file.')
    parser.add_argument('--access-token',
                        help='OAuth2 access token ')
    parser.add_argument('--client-secret',
                        help='Path to a JSON file containing oauth client ID secrets.')

    args, extra_args = parser.parse_known_args()

    if args.help:
        handle_help(parser, args)
        exit(0)

    if args.list_commands:
        list_commands()
        exit(0)

    if args.command is None:
        print('[ERROR] command name is required')
        parser.print_help()
        exit(1)

    # TODO: support specifying creds via env vars
    pb_client = None
    if args.service_account_creds is not None and args.service_account_email is not None:
        pb_client = pbapi.build_client_from_p12(args.service_account_creds, args.service_account_email)
    elif args.service_account_creds is not None:
        pb_client = pbapi.build_client_from_json(args.service_account_creds)
    elif args.access_token is not None:
        pb_client = pbapi.build_client_from_access_token(args.access_token)
    elif args.client_secret is not None:
        pb_client = pbapi.build_client_from_client_id_json(args.client_secret)
    else:
        try:
            pb_client = pbapi.build_client_from_app_default()
        except Exception, err:
            # TODO: if no creds found, attempt web-based oauth flow
            print('[ERROR] No usable access credentials specified. Cannot create API client: {}'.format(err.message))
            exit(1)
Exemplo n.º 4
0
def main():
    parser = argparse.ArgumentParser(
        description='Lists known attachments for this beacon/project.')
    parser.add_argument(
        'creds',
        nargs='?',
        help=
        'Path to JSON file containing service account credentials authorized to call the Google Proximity Beacon API'
    )
    parser.add_argument(
        '--beacon-name',
        required=False,
        help=
        'Optional beacon name to list attachments for. Otherwise, lists all attachments for all beacons under this project.'
    )
    parser.add_argument('--project-id', required=False, help='')
    parser.add_argument('--access-token', required=False, help='')

    args = parser.parse_args()
    pb_client = None

    if args.creds is not None:
        pb_client = pbapi.build_client_from_json(args.creds)
    elif args.access_token is not None:
        pb_client = pbapi.build_client_from_access_token(args.access_token)
    else:
        print(
            '[ERROR] No usable access credentials specified. Cannot create API client.'
        )
        exit(1)

    if args.beacon_name is None:
        beacons = pb_client.list_beacons()
        beacon_names = map(lambda b: b['beaconName'], beacons)
    else:
        beacon_names = [args.beacon_name]

    for beacon in beacon_names:
        attachments = pb_client.list_attachments(beacon, '*/*',
                                                 args.project_id)
        print("Attachments for beacon '%s':" % beacon)

        if attachments is not None:
            for attachment in attachments:
                attachment['data'] = base64.b64decode(attachment['data'])
                print("\t%s" % json.dumps(attachment))
        else:
            print('\tNone')
Exemplo n.º 5
0
def main():
    parser = argparse.ArgumentParser(description='Lists all beacon ')
    parser.add_argument(
        'creds',
        nargs='?',
        help=
        'Path to JSON file containing service account credentials authorized to call the Google Proximity Beacon API'
    )
    parser.add_argument('--names-only',
                        action='store_true',
                        help='Only output names, rather than full JSON')
    parser.add_argument('--project-id', help='Project ID run this command on.')
    parser.add_argument(
        '--access-token',
        help='OAuth Access token to use. Incompatible with specifying creds.')
    args = parser.parse_args()

    project = args.project_id

    pb_client = None
    if args.creds is not None:
        pb_client = pbapi.build_client_from_json(args.creds)
    elif args.access_token is not None:
        pb_client = pbapi.build_client_from_access_token(args.access_token)
    else:
        print(
            '[ERROR] No usable access credentials specified. Cannot create API client.'
        )
        exit(1)

    beacons = pb_client.list_beacons(project)

    if args.names_only:
        beacon_names = map(lambda b: b['beaconName'], beacons)
        for beacon in beacon_names:
            print(beacon)
    else:
        for beacon in beacons:
            print(json.dumps(beacon))