コード例 #1
0
def main():
    parser = argparse.ArgumentParser(description='Lists known attachments for this beacon/project.')
    parser.add_argument('creds',
                        help='Path to JSON file containing service account credentials authorized to call the Google Proximity Beacon API')
    parser.add_argument('beacon_name', nargs='?',
                        help='Optional beacon name to list attachments for. Otherwise, lists all attachments for all beacons under this project.')

    args = parser.parse_args()

    creds = args.creds
    pb_client = pbapi.build_client_from_json(creds)

    beacon_name = args.beacon_name

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

    for beacon in beacon_names:
        attachments = pb_client.list_attachments(beacon, '*/*')
        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')
コード例 #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)
コード例 #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)
コード例 #4
0
def main():
    parser = argparse.ArgumentParser(description='Deactivates the named beacon')
    parser.add_argument('creds',
                        help='Path to JSON file containing service account credentials authorized to call the Google Proximity Beacon API')
    parser.add_argument('beacon_name',
                        help='Name of the beacon to deactivate. Format is "beacons/N!<beacon ID>"')

    args = parser.parse_args()

    creds = args.creds
    pb_client = pbapi.build_client_from_json(creds)

    pb_client.deactivate_beacon(args.beacon_name)
コード例 #5
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')
コード例 #6
0
def main():
    parser = argparse.ArgumentParser(description=
                                     'Deletes the named attachment')
    parser.add_argument('creds',
                        help='Path to JSON file containing service account credentials authorized to call the Google Proximity Beacon API')
    parser.add_argument('attachment_name',
                        help='Name of attachment in PBAPI to delete. Format is "beacons/N!<beacon ID>/attachments/UUID"')

    args = parser.parse_args()

    creds = args.creds
    pb_client = pbapi.build_client_from_json(creds)

    attachment_name = args.attachment_name
    print('Deleting attachment {}'.format(attachment_name))
    response = pb_client.delete_attachment(attachment_name)

    print(response)
コード例 #7
0
def main():
    parser = argparse.ArgumentParser(
        description='Deactivates the named beacon')
    parser.add_argument(
        'creds',
        help=
        'Path to JSON file containing service account credentials authorized to call the Google Proximity Beacon API'
    )
    parser.add_argument(
        'beacon_name',
        help=
        'Name of the beacon to deactivate. Format is "beacons/N!<beacon ID>"')

    args = parser.parse_args()

    creds = args.creds
    pb_client = pbapi.build_client_from_json(creds)

    pb_client.deactivate_beacon(args.beacon_name)
コード例 #8
0
def main():
    parser = argparse.ArgumentParser(description='Registers a beacon to the authenticated project')
    parser.add_argument('creds',
                        help='Path to JSON file containing service account credentials authorized to call the Google Proximity Beacon API')
    parser.add_argument('beacon_file',
                        help='Path to JSON file containing the beacon description. See the following for a full list of fields: https://developers.google.com/beacons/proximity/reference/rest/v1beta1/beacons#Beacon')

    args = parser.parse_args()

    creds = args.creds
    pb_client = pbapi.build_client_from_json(creds)

    with open(args.beacon_file, 'r') as beacon_file:
        beacon = json.load(beacon_file)

    response = pb_client.register_beacon(beacon)

    if 'beaconName' in response:
        print('Registered beacon: {}'.format(response['beaconName']))
    else:
        print('Unable to register beacon')
コード例 #9
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))
コード例 #10
0
def main():
    parser = argparse.ArgumentParser(
        description='Lists known attachments for this beacon/project.')
    parser.add_argument(
        'creds',
        help=
        'Path to JSON file containing service account credentials authorized to call the Google Proximity Beacon API'
    )
    parser.add_argument(
        'beacon_name',
        nargs='?',
        help=
        'Optional beacon name to list attachments for. Otherwise, lists all attachments for all beacons under this project.'
    )

    args = parser.parse_args()

    creds = args.creds
    pb_client = pbapi.build_client_from_json(creds)

    beacon_name = args.beacon_name

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

    for beacon in beacon_names:
        attachments = pb_client.list_attachments(beacon, '*/*')
        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')
コード例 #11
0
def main():
    parser = argparse.ArgumentParser(description='Lists all beacon ')
    parser.add_argument(
        'creds',
        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')
    args = parser.parse_args()

    creds = args.creds
    pb_client = pbapi.build_client_from_json(creds)

    beacons = pb_client.list_beacons()

    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))
コード例 #12
0
def main():
    parser = argparse.ArgumentParser(
        description='Deletes the named attachment')
    parser.add_argument(
        'creds',
        help=
        'Path to JSON file containing service account credentials authorized to call the Google Proximity Beacon API'
    )
    parser.add_argument(
        'attachment_name',
        help=
        'Name of attachment in PBAPI to delete. Format is "beacons/N!<beacon ID>/attachments/UUID"'
    )

    args = parser.parse_args()

    creds = args.creds
    pb_client = pbapi.build_client_from_json(creds)

    attachment_name = args.attachment_name
    print('Deleting attachment {}'.format(attachment_name))
    response = pb_client.delete_attachment(attachment_name)

    print(response)
コード例 #13
0
def main():
    parser = argparse.ArgumentParser(description=
                                     'Creates and adds an attachment to a beacon and verifies it to be a valid Nearby Notifications attachment')
    parser.add_argument('creds',
                        help='Path to JSON file containing service account credentials authorized to call the Google Proximity Beacon API')
    parser.add_argument('beacon_name',
                        help='Name of the beacon to attach to. Format is "beacons/N!<beacon ID>"')
    parser.add_argument('attachment',
                        help='Path to the JSON file of the attachment to add (data only)')

    args = parser.parse_args()

    creds = args.creds
    pb_client = pbapi.build_client_from_json(creds)

    print('Checking that "{}" is registered with current project'
          .format(args.beacon_name))
    beacons = pb_client.list_beacons()
    beacon_names = map(lambda b: b['beaconName'], beacons)
    if args.beacon_name not in beacon_names:
        print('Beacon name {} not registered yet. Please register it first.'
              .format(args.beacon_name))
        exit(1)

    attachment_file = args.attachment
    print('Reading attachment from "{}" and verifying fields'
          .format(attachment_file))
    with open(args.attachment, 'r') as data_file:
        attachment = json.load(data_file)

    print('Checking attachment for required fields.')
    for field in REQUIRED_FIELDS:
        if field not in attachment:
            print('[ERROR] Nearby requires "{}" field in attachment json, but was not found.'
                  .format(field))
            exit(1)

    print('Checking attachment for recommended fields.')
    for field in RECOMMENDED_FIELDS:
        if field not in attachment:
            print('[WARN] "{}" is recommended to have in a Nearby attachment, but was not found.'
                  .format(field))

    print('Checking title + description length')
    title_desc_len = 0
    if 'title' in attachment:
        title_desc_len += len(attachment['title'])
    if 'description' in attachment:
        title_desc_len += len(attachment['description'])

    if title_desc_len > HARD_TITLE_DESC_MAX:
        print('[ERROR] Title + Description length surpassed hard max of {}. Values given: "{} - {}" (length: {})'
              .format(HARD_TITLE_DESC_MAX, attachment['title'], attachment['description'], title_desc_len))
        exit(1)

    if title_desc_len > IDEAL_TITLE_DESC_MAX:
        print('[WARN] Title + Description length greater than soft max of {}. Values given: "{} - {}" (length: {})'
              .format(IDEAL_TITLE_DESC_MAX, attachment['title'], attachment['description'], title_desc_len))

    # Add attachment to beacon
    print('Adding attachment to "' + args.beacon_name + '"')
    pb_client.add_attachment(args.beacon_name, NEARBY_NS_TYPE, json.dumps(attachment))