aboutsummaryrefslogtreecommitdiffstats
path: root/python/client.py
blob: 14814512819b65e74e16fada484fcacf3bd8e626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3

import sys
import argparse
from fatcat.api_client import FatCatApiClient
from fatcat.orcid_importer import FatcatOrcidImporter

def run_import_crossref(args):
    fcc = FatCatApiClient(args.host_url)
    fcc.import_crossref_file(args.json_file,
        create_containers=args.create_containers)

def run_import_orcid(args):
    foi = FatcatOrcidImporter(args.host_url)
    foi.process_batch(args.json_file, size=args.batch_size)

def health(args):
    fcc = FatCatApiClient(args.host_url)
    print(fcc.health())

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--debug',
        action='store_true',
        help="enable debugging interface")
    parser.add_argument('--host-url',
        default="http://localhost:9411/v0",
        help="connect to this host/port")
    subparsers = parser.add_subparsers()

    sub_import_crossref = subparsers.add_parser('import-crossref')
    sub_import_crossref.set_defaults(func=run_import_crossref)
    sub_import_crossref.add_argument('json_file',
        help="crossref JSON file to import from")
    sub_import_crossref.add_argument('--create-containers',
        action='store_true',
        help="if true, create containers based on ISSN")

    sub_import_orcid = subparsers.add_parser('import-orcid')
    sub_import_orcid.set_defaults(func=run_import_orcid)
    sub_import_orcid.add_argument('json_file',
        help="orcid JSON file to import from (or stdin)",
        default=sys.stdin, type=argparse.FileType('r'))
    sub_import_orcid.add_argument('--batch-size',
        help="size of batch to send",
        default=50, type=int)

    sub_health = subparsers.add_parser('health')
    sub_health.set_defaults(func=health)

    args = parser.parse_args()
    if not args.__dict__.get("func"):
        print("tell me what to do!")
        sys.exit(-1)
    args.func(args)

if __name__ == '__main__':
    main()