コード例 #1
0
ファイル: gro_client.py プロジェクト: eyvindn/api-client
def main():
    """Basic Gro API command line interface.

    Note that results are chosen randomly from matching selections, and so results are not
    deterministic. This tool is useful for simple queries, but anything more complex should be done
    using the provided Python packages.

    Usage examples:
        gro_client --item=soybeans  --region=brazil --partner_region china --metric export
        gro_client --item=sesame --region=ethiopia
        gro_client [email protected]  --print_token
    For more information use --help
    """
    parser = argparse.ArgumentParser(
        description="Gro API command line interface")
    parser.add_argument("--user_email")
    parser.add_argument("--user_password")
    parser.add_argument("--item")
    parser.add_argument("--metric")
    parser.add_argument("--region")
    parser.add_argument("--partner_region")
    parser.add_argument(
        "--print_token",
        action='store_true',
        help="Ouput API access token for the given user email and password. "
        "Save it in GROAPI_TOKEN environment variable.")
    parser.add_argument("--token",
                        default=os.environ.get('GROAPI_TOKEN'),
                        help="Defaults to GROAPI_TOKEN environment variable.")
    args = parser.parse_args()

    assert args.user_email or args.token, "Need --token, or --user_email, or $GROAPI_TOKEN"
    access_token = None

    if args.token:
        access_token = args.token
    else:
        if not args.user_password:
            args.user_password = getpass.getpass()
        access_token = lib.get_access_token(API_HOST, args.user_email,
                                            args.user_password)
    if args.print_token:
        print(access_token)
        sys.exit(0)
    client = GroClient(API_HOST, access_token)

    if not args.metric and not args.item and not args.region and not args.partner_region:
        ds = client.pick_random_data_series(client.pick_random_entities())
    else:
        ds = next(
            client.find_data_series(item=args.item,
                                    metric=args.metric,
                                    region=args.region,
                                    partner_region=args.partner_region))
    client.print_one_data_series(ds, OUTPUT_FILENAME)
コード例 #2
0
def main():
    parser = argparse.ArgumentParser(
        description="Gro API command line interface")
    parser.add_argument("--user_email")
    parser.add_argument("--user_password")
    parser.add_argument("--item")
    parser.add_argument("--metric")
    parser.add_argument("--region")
    parser.add_argument("--partner_region")
    parser.add_argument(
        "--print_token",
        action='store_true',
        help="Ouput API access token for the given user email and password. "
        "Save it in GROAPI_TOKEN environment variable.")
    parser.add_argument("--token",
                        default=os.environ.get('GROAPI_TOKEN'),
                        help="Defaults to GROAPI_TOKEN environment variable.")
    args = parser.parse_args()

    assert args.user_email or args.token, "Need --token, or --user_email, or $GROAPI_TOKEN"
    access_token = None

    if args.token:
        access_token = args.token
    else:
        if not args.user_password:
            args.user_password = getpass.getpass()
        access_token = lib.get_access_token(API_HOST, args.user_email,
                                            args.user_password)
    if args.print_token:
        print(access_token)
        sys.exit(0)
    client = GroClient(API_HOST, access_token)

    selected_entities = {}
    if args.item:
        selected_entities['item_id'] = client.search_for_entity(
            'items', args.item)
    if args.metric:
        selected_entities['metric_id'] = client.search_for_entity(
            'metrics', args.metric)
    if args.region:
        selected_entities['region_id'] = client.search_for_entity(
            'regions', args.region)
    if args.partner_region:
        selected_entities['partner_region_id'] = client.search_for_entity(
            'regions', args.partner_region)
    if not selected_entities:
        selected_entities = client.pick_random_entities()

    data_series = client.pick_random_data_series(selected_entities)
    print("Data series example:")
    client.print_one_data_series(data_series, OUTPUT_FILENAME)