dump-things-pyclient 0.2.5__py3-none-any.whl → 0.2.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dump_things_pyclient/commands/dtc_plugins/auto_curate.py +24 -11
- dump_things_pyclient/commands/dtc_plugins/clean_incoming.py +4 -1
- dump_things_pyclient/commands/dtc_plugins/{delete_record.py → delete_records.py} +13 -8
- dump_things_pyclient/commands/dtc_plugins/export.py +20 -11
- dump_things_pyclient/commands/dtc_plugins/get_records.py +16 -10
- dump_things_pyclient/commands/dtc_plugins/maintenance.py +8 -1
- dump_things_pyclient/commands/dtc_plugins/post_records.py +26 -13
- dump_things_pyclient/commands/dtc_plugins/read_pages.py +9 -2
- dump_things_pyclient/communicate.py +128 -26
- {dump_things_pyclient-0.2.5.dist-info → dump_things_pyclient-0.2.6.dist-info}/METADATA +1 -1
- {dump_things_pyclient-0.2.5.dist-info → dump_things_pyclient-0.2.6.dist-info}/RECORD +14 -14
- {dump_things_pyclient-0.2.5.dist-info → dump_things_pyclient-0.2.6.dist-info}/WHEEL +0 -0
- {dump_things_pyclient-0.2.5.dist-info → dump_things_pyclient-0.2.6.dist-info}/entry_points.txt +0 -0
- {dump_things_pyclient-0.2.5.dist-info → dump_things_pyclient-0.2.6.dist-info}/top_level.txt +0 -0
|
@@ -19,6 +19,7 @@ from rich.progress import track
|
|
|
19
19
|
from ...communicate import (
|
|
20
20
|
HTTPError,
|
|
21
21
|
curated_write_record,
|
|
22
|
+
get_session,
|
|
22
23
|
incoming_delete_record,
|
|
23
24
|
incoming_read_labels,
|
|
24
25
|
incoming_read_records,
|
|
@@ -126,10 +127,8 @@ def cli(
|
|
|
126
127
|
dry_run,
|
|
127
128
|
)
|
|
128
129
|
except HTTPError as e:
|
|
129
|
-
|
|
130
|
+
console.print(
|
|
130
131
|
f'[red]Error[/red]: {e}: {e.response.text}',
|
|
131
|
-
file=sys.stderr,
|
|
132
|
-
flush=True,
|
|
133
132
|
)
|
|
134
133
|
return 1
|
|
135
134
|
|
|
@@ -154,6 +153,9 @@ def auto_curate(
|
|
|
154
153
|
console.print(f'[red]Error[/red]: no token was provided (use --token or DTC_TOKEN environment variable)')
|
|
155
154
|
return 1
|
|
156
155
|
|
|
156
|
+
if destination_collection is None:
|
|
157
|
+
destination_collection = collection
|
|
158
|
+
|
|
157
159
|
if destination_service_url is None:
|
|
158
160
|
destination_service_url = service_url
|
|
159
161
|
|
|
@@ -173,10 +175,12 @@ def auto_curate(
|
|
|
173
175
|
if list_labels:
|
|
174
176
|
output = []
|
|
175
177
|
|
|
178
|
+
session = get_session()
|
|
176
179
|
all_labels = incoming_read_labels(
|
|
177
180
|
service_url=service_url,
|
|
178
181
|
collection=collection,
|
|
179
182
|
token=obj,
|
|
183
|
+
session=session,
|
|
180
184
|
)
|
|
181
185
|
for label in all_labels:
|
|
182
186
|
if include and label not in include:
|
|
@@ -200,6 +204,7 @@ def auto_curate(
|
|
|
200
204
|
collection=collection,
|
|
201
205
|
label=label,
|
|
202
206
|
token=obj,
|
|
207
|
+
session=session,
|
|
203
208
|
)
|
|
204
209
|
|
|
205
210
|
# Get the first entry to find the total number of records
|
|
@@ -241,19 +246,25 @@ def auto_curate(
|
|
|
241
246
|
class_name = re.search('([_A-Za-z0-9]*$)', record['schema_type']).group(0)
|
|
242
247
|
except (IndexError, KeyError):
|
|
243
248
|
global stl_info
|
|
244
|
-
console.print(f'[yellow]Warning[/yellow]: ignoring record with pid {record["pid"]} because `schema_type` attribute is missing.')
|
|
249
|
+
console.print(f'[yellow]Warning[/yellow]: ignoring record with pid [yellow]{record["pid"]}[/yellow] because `schema_type` attribute is missing.')
|
|
245
250
|
if not stl_info:
|
|
246
251
|
console.print(
|
|
247
|
-
' Please ensure that `schema_type` is stored in the records '
|
|
248
|
-
'
|
|
249
|
-
'"
|
|
252
|
+
' [yellow]Please ensure that `schema_type` is stored in the records. Note: '
|
|
253
|
+
'if the incoming area store has a backend with a "Schema Type Layer", i.e., '
|
|
254
|
+
'"record_dir+stl" or "sqlite+stl", `schema_type` will not be stored on persistent '
|
|
255
|
+
'storage and will not be returned when retrieving records from the incoming area. '
|
|
256
|
+
'dump-things-service <= 5.4.0 circumvented the "Schema Type Layer", therefore they '
|
|
257
|
+
'will return records without `schema_type` attributes on curator access to '
|
|
258
|
+
'incoming areas or curated areas. Therefore it might be a good idea to NOT use a '
|
|
259
|
+
'"Schema Type Layer" in collections that shall be auto-curated, when using '
|
|
260
|
+
'dump-things-service <= 5.4.0.[/yellow]',
|
|
250
261
|
)
|
|
251
262
|
stl_info = True
|
|
252
263
|
continue
|
|
253
264
|
|
|
254
265
|
if dry_run:
|
|
255
|
-
console.print(f'WRITE record [green]"{record["pid"]}"[/green] of class "{class_name}" to "{destination_collection}
|
|
256
|
-
console.print(f'DELETE record [green]"{record["pid"]}"[/green] from inbox "{label}" of "{collection}
|
|
266
|
+
console.print(f'WRITE record [green]"{record["pid"]}"[/green] of class "{class_name}" to collection "{destination_collection}" on "{destination_service_url}"')
|
|
267
|
+
console.print(f'DELETE record [green]"{record["pid"]}"[/green] from inbox "{label}" of collection "{collection}" on "{service_url}"')
|
|
257
268
|
continue
|
|
258
269
|
|
|
259
270
|
# Store record in destination collection
|
|
@@ -264,12 +275,13 @@ def auto_curate(
|
|
|
264
275
|
class_name=class_name,
|
|
265
276
|
record=record,
|
|
266
277
|
token=destination_token,
|
|
278
|
+
session=session,
|
|
267
279
|
)
|
|
268
280
|
except HTTPError as e:
|
|
269
281
|
console.print(
|
|
270
282
|
f'[red]Error[/red]: writing record with pid {record["pid"]} failed: {e}: {e.response.text}',
|
|
271
283
|
)
|
|
272
|
-
|
|
284
|
+
return 1
|
|
273
285
|
|
|
274
286
|
# Delete record from incoming area
|
|
275
287
|
try:
|
|
@@ -279,12 +291,13 @@ def auto_curate(
|
|
|
279
291
|
label=label,
|
|
280
292
|
pid=record['pid'],
|
|
281
293
|
token=curator_token,
|
|
294
|
+
session=session,
|
|
282
295
|
)
|
|
283
296
|
except HTTPError as e:
|
|
284
297
|
console.print(
|
|
285
298
|
f'[red]ERROR[/red]: deleting record with pid {record["pid"]} failed: {e}: {e.response.text}',
|
|
286
299
|
)
|
|
287
|
-
|
|
300
|
+
return 1
|
|
288
301
|
|
|
289
302
|
if output is not None:
|
|
290
303
|
rprint(json.dumps(output, ensure_ascii=False))
|
|
@@ -4,6 +4,7 @@ import rich_click as click
|
|
|
4
4
|
|
|
5
5
|
from ...communicate import (
|
|
6
6
|
HTTPError,
|
|
7
|
+
get_session,
|
|
7
8
|
incoming_delete_record,
|
|
8
9
|
incoming_read_records,
|
|
9
10
|
)
|
|
@@ -71,11 +72,13 @@ def clean_incoming(
|
|
|
71
72
|
click.echo('ERROR: token not provided', err=True)
|
|
72
73
|
return 1
|
|
73
74
|
|
|
75
|
+
session = get_session()
|
|
74
76
|
for record, _, _, _, _ in incoming_read_records(
|
|
75
77
|
service_url=service_url,
|
|
76
78
|
collection=collection,
|
|
77
79
|
label=inbox_label,
|
|
78
80
|
token=token,
|
|
81
|
+
session=session,
|
|
79
82
|
):
|
|
80
83
|
if list_only:
|
|
81
84
|
click.echo(json.dumps(record, ensure_ascii=False))
|
|
@@ -88,6 +91,6 @@ def clean_incoming(
|
|
|
88
91
|
label=inbox_label,
|
|
89
92
|
pid=record['pid'],
|
|
90
93
|
token=token,
|
|
91
|
-
|
|
94
|
+
session=session,
|
|
92
95
|
)
|
|
93
96
|
return 0
|
|
@@ -4,9 +4,12 @@ import sys
|
|
|
4
4
|
from functools import partial
|
|
5
5
|
|
|
6
6
|
import rich_click as click
|
|
7
|
+
from rich.progress import track
|
|
8
|
+
from rich.console import Console
|
|
7
9
|
|
|
8
10
|
from ...communicate import (
|
|
9
11
|
HTTPError,
|
|
12
|
+
get_session,
|
|
10
13
|
collection_delete_record,
|
|
11
14
|
curated_delete_record,
|
|
12
15
|
incoming_delete_record,
|
|
@@ -18,6 +21,8 @@ subcommand_name = 'delete-records'
|
|
|
18
21
|
|
|
19
22
|
logger = logging.getLogger('delete-records')
|
|
20
23
|
|
|
24
|
+
console = Console(file=sys.stderr)
|
|
25
|
+
|
|
21
26
|
|
|
22
27
|
@click.command(short_help='Delete records from a dump-things collection')
|
|
23
28
|
@click.pass_obj
|
|
@@ -83,7 +88,7 @@ def cli(
|
|
|
83
88
|
ignore_errors,
|
|
84
89
|
)
|
|
85
90
|
except HTTPError as e:
|
|
86
|
-
|
|
91
|
+
console.print(f'[red]Error[/red]: {e}: {e.response.text}')
|
|
87
92
|
return 1
|
|
88
93
|
|
|
89
94
|
|
|
@@ -102,16 +107,15 @@ def delete_records(
|
|
|
102
107
|
click.echo(f'WARNING: no token provided', err=True)
|
|
103
108
|
|
|
104
109
|
if incoming and curated:
|
|
105
|
-
|
|
106
|
-
'ERROR: -i/--incoming and -c/--curated are mutually exclusive',
|
|
107
|
-
err=True,
|
|
108
|
-
)
|
|
110
|
+
console.print('[red]Error[/red]: -i/--incoming and -c/--curated are mutually exclusive')
|
|
109
111
|
return 1
|
|
110
112
|
|
|
113
|
+
session = get_session()
|
|
111
114
|
kwargs = dict(
|
|
112
115
|
service_url=service_url,
|
|
113
116
|
collection=collection,
|
|
114
117
|
token=token,
|
|
118
|
+
session=session,
|
|
115
119
|
)
|
|
116
120
|
|
|
117
121
|
if incoming == '-':
|
|
@@ -134,17 +138,18 @@ def delete_records(
|
|
|
134
138
|
if not pids:
|
|
135
139
|
pids = sys.stdin
|
|
136
140
|
|
|
137
|
-
for pid in pids:
|
|
141
|
+
for pid in track(pids, console=console):
|
|
138
142
|
try:
|
|
139
143
|
operation(
|
|
140
144
|
service_url=service_url,
|
|
141
145
|
collection=collection,
|
|
142
146
|
pid=pid.strip(),
|
|
143
147
|
token=token,
|
|
148
|
+
session=session,
|
|
144
149
|
)
|
|
145
150
|
except HTTPError as e:
|
|
151
|
+
console.print(f'[red]Error[/red]: while deleting pid {pid}: {e}, {e.response.text}')
|
|
146
152
|
if ignore_errors:
|
|
147
|
-
click.echo(f'ERROR: while deleting pid {pid}: {e}', err=True)
|
|
148
153
|
continue
|
|
149
|
-
|
|
154
|
+
return 1
|
|
150
155
|
return 0
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import json
|
|
2
|
+
import sys
|
|
2
3
|
from collections import defaultdict
|
|
3
4
|
from itertools import count
|
|
4
5
|
from pathlib import Path
|
|
@@ -8,10 +9,13 @@ from typing import (
|
|
|
8
9
|
)
|
|
9
10
|
|
|
10
11
|
import rich_click as click
|
|
12
|
+
from rich.console import Console
|
|
13
|
+
from rich.progress import track
|
|
11
14
|
|
|
12
15
|
from ...communicate import (
|
|
13
16
|
HTTPError,
|
|
14
17
|
curated_read_records,
|
|
18
|
+
get_session,
|
|
15
19
|
incoming_read_labels,
|
|
16
20
|
incoming_read_records,
|
|
17
21
|
server,
|
|
@@ -20,6 +24,8 @@ from ...communicate import (
|
|
|
20
24
|
|
|
21
25
|
subcommand_name = 'export'
|
|
22
26
|
|
|
27
|
+
console = Console(file=sys.stderr)
|
|
28
|
+
|
|
23
29
|
|
|
24
30
|
@click.command(short_help='Export a collection to the file system')
|
|
25
31
|
@click.pass_obj
|
|
@@ -76,9 +82,9 @@ def cli(
|
|
|
76
82
|
ignore_errors,
|
|
77
83
|
)
|
|
78
84
|
except HTTPError as e:
|
|
79
|
-
|
|
85
|
+
console.print(f'[red]Error[/red]: {e}: {e.response.text}')
|
|
80
86
|
except ValueError as e:
|
|
81
|
-
|
|
87
|
+
console.print(f'[red]Error[/red]: {e}')
|
|
82
88
|
return 1
|
|
83
89
|
|
|
84
90
|
|
|
@@ -92,14 +98,15 @@ def export(
|
|
|
92
98
|
token = obj
|
|
93
99
|
|
|
94
100
|
if token is None:
|
|
95
|
-
|
|
101
|
+
console.print(f'[red]Error[/red]: no token provided')
|
|
96
102
|
return 1
|
|
97
103
|
|
|
98
|
-
|
|
104
|
+
session = get_session()
|
|
105
|
+
server_info = server(service_url, session=session)
|
|
99
106
|
collection_info = ([c for c in server_info['collections'] if c['name'] == collection] or None)[0]
|
|
100
107
|
|
|
101
108
|
if not collection_info:
|
|
102
|
-
|
|
109
|
+
console.print(f'[red]Error[/red]: no collection {collection} on service')
|
|
103
110
|
return 1
|
|
104
111
|
|
|
105
112
|
description = {
|
|
@@ -116,6 +123,7 @@ def export(
|
|
|
116
123
|
curated_destination = destination / 'curated'
|
|
117
124
|
curated_destination.mkdir()
|
|
118
125
|
|
|
126
|
+
console.print('Exporting records from curated area')
|
|
119
127
|
_store_records(
|
|
120
128
|
map(
|
|
121
129
|
lambda x: x[0],
|
|
@@ -123,6 +131,7 @@ def export(
|
|
|
123
131
|
service_url=service_url,
|
|
124
132
|
collection=collection,
|
|
125
133
|
token=token,
|
|
134
|
+
session=session,
|
|
126
135
|
)
|
|
127
136
|
),
|
|
128
137
|
curated_destination,
|
|
@@ -134,7 +143,9 @@ def export(
|
|
|
134
143
|
service_url=service_url,
|
|
135
144
|
collection=collection,
|
|
136
145
|
token=token,
|
|
146
|
+
session=session,
|
|
137
147
|
):
|
|
148
|
+
console.print(f'Exporting records from incoming area: {label}')
|
|
138
149
|
incoming_destination = destination / 'incoming' / label
|
|
139
150
|
incoming_destination.mkdir(parents=True, exist_ok=False)
|
|
140
151
|
_store_records(
|
|
@@ -145,6 +156,7 @@ def export(
|
|
|
145
156
|
collection=collection,
|
|
146
157
|
label=label,
|
|
147
158
|
token=token,
|
|
159
|
+
session=session,
|
|
148
160
|
)
|
|
149
161
|
),
|
|
150
162
|
incoming_destination,
|
|
@@ -162,16 +174,13 @@ def _store_records(
|
|
|
162
174
|
created_dirs = set()
|
|
163
175
|
class_counters = defaultdict(count)
|
|
164
176
|
|
|
165
|
-
for record in source:
|
|
177
|
+
for record in track(source, console=console):
|
|
166
178
|
class_name = _de_prefix(record.get('schema_type', None))
|
|
167
179
|
if class_name is None:
|
|
168
180
|
if ignore_errors:
|
|
169
|
-
|
|
170
|
-
f'WARNING: no `schema_type` in record `{record["pid"]}`',
|
|
171
|
-
err=True
|
|
172
|
-
)
|
|
181
|
+
console.print(f'[red]Error[/red]: no `schema type` in record {record["pid"]}')
|
|
173
182
|
continue
|
|
174
|
-
msg = f'no `schema_type` in record
|
|
183
|
+
msg = f'no `schema_type` in record {record["pid"]}'
|
|
175
184
|
raise ValueError(msg)
|
|
176
185
|
|
|
177
186
|
next_name_for_class = f'{next(class_counters[class_name]):09d}.json'
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import json
|
|
2
|
+
import sys
|
|
2
3
|
from functools import partial
|
|
3
4
|
|
|
4
5
|
import rich_click as click
|
|
6
|
+
from rich.console import Console
|
|
5
7
|
|
|
6
8
|
from ...communicate import (
|
|
7
9
|
HTTPError,
|
|
@@ -11,6 +13,7 @@ from ...communicate import (
|
|
|
11
13
|
curated_read_records,
|
|
12
14
|
curated_read_records_of_class,
|
|
13
15
|
curated_read_record_with_pid,
|
|
16
|
+
get_session,
|
|
14
17
|
incoming_read_labels,
|
|
15
18
|
incoming_read_records,
|
|
16
19
|
incoming_read_records_of_class,
|
|
@@ -20,6 +23,8 @@ from ...communicate import (
|
|
|
20
23
|
|
|
21
24
|
subcommand_name = 'get-records'
|
|
22
25
|
|
|
26
|
+
console = Console(file=sys.stderr)
|
|
27
|
+
|
|
23
28
|
|
|
24
29
|
@click.command(short_help='Get records from a dump-things collection')
|
|
25
30
|
@click.pass_obj
|
|
@@ -48,7 +53,7 @@ subcommand_name = 'get-records'
|
|
|
48
53
|
@click.option(
|
|
49
54
|
'--incoming', '-i',
|
|
50
55
|
metavar='LABEL',
|
|
51
|
-
help='read from the collection\'s inbox with label LABEL, if LABEL is "-",
|
|
56
|
+
help='read from the collection\'s inbox with label LABEL, if LABEL is "-", print labels of all collection inboxes and exit',
|
|
52
57
|
)
|
|
53
58
|
@click.option(
|
|
54
59
|
'--curated', '-c',
|
|
@@ -144,7 +149,7 @@ def cli(
|
|
|
144
149
|
pagination,
|
|
145
150
|
)
|
|
146
151
|
except HTTPError as e:
|
|
147
|
-
|
|
152
|
+
console.print(f'[red]Error[/red]: {e}: {e.response.text}')
|
|
148
153
|
return 1
|
|
149
154
|
|
|
150
155
|
|
|
@@ -167,19 +172,18 @@ def get_records(
|
|
|
167
172
|
token = obj
|
|
168
173
|
|
|
169
174
|
if token is None:
|
|
170
|
-
|
|
175
|
+
console.print(f'[yellow]Warning[/yellow]: no token provided')
|
|
171
176
|
|
|
172
177
|
if incoming and curated:
|
|
173
|
-
|
|
174
|
-
'ERROR: -i/--incoming and -c/--curated are mutually exclusive',
|
|
175
|
-
err=True,
|
|
176
|
-
)
|
|
178
|
+
console.print('[red]Error[/red]: -i/--incoming and -c/--curated are mutually exclusive')
|
|
177
179
|
return 1
|
|
178
180
|
|
|
181
|
+
session = get_session()
|
|
179
182
|
kwargs = dict(
|
|
180
183
|
service_url=service_url,
|
|
181
184
|
collection=collection,
|
|
182
185
|
token=token,
|
|
186
|
+
session=session,
|
|
183
187
|
)
|
|
184
188
|
|
|
185
189
|
if incoming == '-':
|
|
@@ -187,7 +191,9 @@ def get_records(
|
|
|
187
191
|
click.echo('\n'.join(
|
|
188
192
|
map(
|
|
189
193
|
partial(json.dumps, ensure_ascii=False),
|
|
190
|
-
result
|
|
194
|
+
result
|
|
195
|
+
)
|
|
196
|
+
))
|
|
191
197
|
return 0
|
|
192
198
|
|
|
193
199
|
elif pid:
|
|
@@ -251,8 +257,8 @@ def get_records(
|
|
|
251
257
|
|
|
252
258
|
if pagination:
|
|
253
259
|
for record in result:
|
|
254
|
-
|
|
260
|
+
click.echo(json.dumps(record, ensure_ascii=False))
|
|
255
261
|
else:
|
|
256
262
|
for record in result:
|
|
257
|
-
|
|
263
|
+
click.echo(json.dumps(record[0], ensure_ascii=False))
|
|
258
264
|
return 0
|
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
import logging
|
|
2
|
+
import sys
|
|
2
3
|
|
|
3
4
|
import rich_click as click
|
|
5
|
+
from rich.console import Console
|
|
4
6
|
|
|
5
7
|
from ...communicate import (
|
|
6
8
|
HTTPError,
|
|
9
|
+
get_session,
|
|
7
10
|
maintenance as communicate_maintenance,
|
|
8
11
|
)
|
|
9
12
|
|
|
10
13
|
|
|
11
14
|
logger = logging.getLogger('maintenance')
|
|
12
15
|
|
|
16
|
+
console = Console(file=sys.stderr)
|
|
17
|
+
|
|
13
18
|
subcommand_name = 'maintenance'
|
|
14
19
|
|
|
15
20
|
|
|
@@ -61,13 +66,15 @@ def maintenance(
|
|
|
61
66
|
):
|
|
62
67
|
token = obj
|
|
63
68
|
if token is None:
|
|
64
|
-
|
|
69
|
+
console.print('[red]Error[/red]: no token provided')
|
|
65
70
|
return 1
|
|
66
71
|
|
|
72
|
+
session = get_session()
|
|
67
73
|
communicate_maintenance(
|
|
68
74
|
service_url=service_url,
|
|
69
75
|
collection=collection,
|
|
70
76
|
active=active,
|
|
71
77
|
token=token,
|
|
78
|
+
session=session,
|
|
72
79
|
)
|
|
73
80
|
return 0
|
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
import json
|
|
2
2
|
import logging
|
|
3
3
|
import sys
|
|
4
|
+
from itertools import count
|
|
4
5
|
|
|
5
6
|
import rich_click as click
|
|
7
|
+
from rich.console import Console
|
|
8
|
+
from rich.progress import track
|
|
6
9
|
|
|
7
10
|
from ...communicate import (
|
|
8
11
|
HTTPError,
|
|
9
12
|
curated_write_record,
|
|
10
13
|
collection_write_record,
|
|
14
|
+
get_session,
|
|
11
15
|
)
|
|
12
16
|
|
|
13
17
|
|
|
14
18
|
logger = logging.getLogger('post-records')
|
|
15
19
|
|
|
20
|
+
console = Console(file=sys.stderr)
|
|
21
|
+
|
|
16
22
|
|
|
17
23
|
@click.command(short_help='Post records to an inbox or the curated area of a dump-things collection')
|
|
18
24
|
@click.pass_obj
|
|
@@ -81,7 +87,7 @@ def post_records(
|
|
|
81
87
|
):
|
|
82
88
|
token = obj
|
|
83
89
|
if token is None:
|
|
84
|
-
|
|
90
|
+
console.print('[red]Error[/red]: No token provided')
|
|
85
91
|
return 1
|
|
86
92
|
|
|
87
93
|
if curated:
|
|
@@ -89,9 +95,17 @@ def post_records(
|
|
|
89
95
|
else:
|
|
90
96
|
write_record = collection_write_record
|
|
91
97
|
|
|
92
|
-
|
|
93
|
-
for line in sys.stdin:
|
|
94
|
-
|
|
98
|
+
session = get_session()
|
|
99
|
+
for index, line in zip(count(), track(sys.stdin, console=console)):
|
|
100
|
+
|
|
101
|
+
try:
|
|
102
|
+
record = json.loads(line)
|
|
103
|
+
except Exception as e:
|
|
104
|
+
console.print(
|
|
105
|
+
f'[red]Error: reading JSON record #{index} failed[/red]: {e}',
|
|
106
|
+
)
|
|
107
|
+
continue
|
|
108
|
+
|
|
95
109
|
try:
|
|
96
110
|
write_record(
|
|
97
111
|
service_url=service_url,
|
|
@@ -99,17 +113,16 @@ def post_records(
|
|
|
99
113
|
class_name=cls,
|
|
100
114
|
record=record,
|
|
101
115
|
token=token,
|
|
116
|
+
session=session,
|
|
117
|
+
)
|
|
118
|
+
except HTTPError as e:
|
|
119
|
+
console.print(
|
|
120
|
+
f'[red]Error: writing record #{index} with pid {record["pid"]} failed[/red]: {e}: {e.response.text}',
|
|
102
121
|
)
|
|
103
122
|
except Exception as e:
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
click.echo('.', nl=False)
|
|
108
|
-
|
|
109
|
-
if posted:
|
|
110
|
-
# echo a final newline
|
|
111
|
-
click.echo('')
|
|
112
|
-
|
|
123
|
+
console.print(
|
|
124
|
+
f'[red]Error: writing record #{index} with pid {record["pid"]} failed[/red]: {e}',
|
|
125
|
+
)
|
|
113
126
|
return 0
|
|
114
127
|
|
|
115
128
|
|
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
import json
|
|
2
2
|
import logging
|
|
3
|
+
import sys
|
|
3
4
|
|
|
4
5
|
import rich_click as click
|
|
6
|
+
from rich.console import Console
|
|
5
7
|
|
|
6
8
|
from ...communicate import (
|
|
7
9
|
HTTPError,
|
|
8
10
|
get_paginated,
|
|
11
|
+
get_session,
|
|
9
12
|
)
|
|
10
13
|
|
|
11
14
|
|
|
12
15
|
logger = logging.getLogger('read-pages')
|
|
13
16
|
|
|
17
|
+
console = Console(file=sys.stderr)
|
|
18
|
+
|
|
14
19
|
|
|
15
20
|
@click.command(short_help='Read records from paginated dump-things endpoints')
|
|
16
21
|
@click.pass_obj
|
|
@@ -106,8 +111,9 @@ def read_pages(
|
|
|
106
111
|
token = obj
|
|
107
112
|
|
|
108
113
|
if token is None:
|
|
109
|
-
|
|
114
|
+
console.print(f'[yellow]Warning[/yellow]: no token provided')
|
|
110
115
|
|
|
116
|
+
session = get_session()
|
|
111
117
|
result = get_paginated(
|
|
112
118
|
url=url,
|
|
113
119
|
token=token,
|
|
@@ -121,7 +127,8 @@ def read_pages(
|
|
|
121
127
|
if matching is not None
|
|
122
128
|
else {}
|
|
123
129
|
),
|
|
124
|
-
}
|
|
130
|
+
},
|
|
131
|
+
session=session,
|
|
125
132
|
)
|
|
126
133
|
|
|
127
134
|
if stats:
|
|
@@ -9,6 +9,7 @@ from typing import (
|
|
|
9
9
|
)
|
|
10
10
|
|
|
11
11
|
import requests
|
|
12
|
+
from requests import Session
|
|
12
13
|
from requests.exceptions import HTTPError
|
|
13
14
|
|
|
14
15
|
from . import JSON
|
|
@@ -17,6 +18,7 @@ from . import JSON
|
|
|
17
18
|
__all__ = [
|
|
18
19
|
'HTTPError',
|
|
19
20
|
'JSON',
|
|
21
|
+
'get_session',
|
|
20
22
|
'get_paginated',
|
|
21
23
|
'get',
|
|
22
24
|
'collection_get_classes',
|
|
@@ -45,12 +47,21 @@ __all__ = [
|
|
|
45
47
|
logger = logging.getLogger('dump_things_pyclient')
|
|
46
48
|
|
|
47
49
|
|
|
50
|
+
def get_session() -> Session:
|
|
51
|
+
"""Return a session that can be used to reuse connections
|
|
52
|
+
|
|
53
|
+
:return: a Session-object that can be passed to most functions in this module
|
|
54
|
+
"""
|
|
55
|
+
return requests.Session()
|
|
56
|
+
|
|
57
|
+
|
|
48
58
|
def get_paginated(url: str,
|
|
49
59
|
token: str | None = None,
|
|
50
60
|
first_page: int = 1,
|
|
51
61
|
page_size: int = 100,
|
|
52
62
|
last_page: int | None = None,
|
|
53
63
|
parameters: dict[str, str] | None = None,
|
|
64
|
+
session: Session | None = None,
|
|
54
65
|
) -> Generator[tuple[JSON, int, int, int, int], None, None]:
|
|
55
66
|
"""Read all records from a paginated endpoint
|
|
56
67
|
|
|
@@ -62,6 +73,7 @@ def get_paginated(url: str,
|
|
|
62
73
|
:param last_page: [optional] last page to return (default: None (return all pages))
|
|
63
74
|
:param parameters: [optional] parameters to pass to the endpoint, the
|
|
64
75
|
parameter `page` is set automatically in this function
|
|
76
|
+
:param session: [optional] if set it will be used for making requests
|
|
65
77
|
|
|
66
78
|
:return: a Generator yielding tuples containing the current record, the
|
|
67
79
|
current page number, the total number of pages, the size of the pages,
|
|
@@ -72,7 +84,7 @@ def get_paginated(url: str,
|
|
|
72
84
|
return
|
|
73
85
|
|
|
74
86
|
for page in count(start=first_page):
|
|
75
|
-
result = _get_page(url, token, first_page=page, page_size=page_size, parameters=parameters)
|
|
87
|
+
result = _get_page(url, token, first_page=page, page_size=page_size, parameters=parameters, session=session)
|
|
76
88
|
total_pages, page_size, total_items = result['pages'], result['size'], result['total']
|
|
77
89
|
if total_pages == 0:
|
|
78
90
|
return
|
|
@@ -90,6 +102,7 @@ def get_paginated(url: str,
|
|
|
90
102
|
def get(url: str,
|
|
91
103
|
token: str | None = None,
|
|
92
104
|
parameters: dict[str, str] | None = None,
|
|
105
|
+
session: Session | None = None,
|
|
93
106
|
) -> JSON:
|
|
94
107
|
"""Read JSON object from a non-paginated endpoint
|
|
95
108
|
|
|
@@ -97,14 +110,16 @@ def get(url: str,
|
|
|
97
110
|
:param token: [optional] if str: token to authenticate against the endpoint,
|
|
98
111
|
if None: no token will be sent to the endpoint
|
|
99
112
|
:param parameters: [optional] parameters to pass to the endpoint
|
|
113
|
+
:param session: [optional] if set it will be used for making requests
|
|
100
114
|
|
|
101
115
|
:return: JSON object
|
|
102
116
|
"""
|
|
103
|
-
return _get_from_url(url, token, parameters)
|
|
117
|
+
return _get_from_url(url, token, parameters, session)
|
|
104
118
|
|
|
105
119
|
|
|
106
120
|
def collection_get_classes(service_url: str,
|
|
107
121
|
collection: str,
|
|
122
|
+
session: Session | None = None,
|
|
108
123
|
) -> Generator[str, None, None]:
|
|
109
124
|
"""Read classes that are supported by the collection
|
|
110
125
|
|
|
@@ -114,12 +129,17 @@ def collection_get_classes(service_url: str,
|
|
|
114
129
|
:param service_url: the base URL of the service, i.e., the URL up to
|
|
115
130
|
`/<collection>/...` or `/server`
|
|
116
131
|
:param collection: the name of the collection
|
|
132
|
+
:param session: [optional] if set it will be used for making requests
|
|
117
133
|
|
|
118
134
|
:return: a generator yielding names of the supported classes
|
|
119
135
|
"""
|
|
120
136
|
service_url = f'{service_url[:-1]}' if service_url.endswith('/') else service_url
|
|
121
137
|
matcher = re.compile(f'/{collection}/record/([A-Z][_a-zA-Z0-9]*)$')
|
|
122
|
-
open_api_spec = _get_from_url(
|
|
138
|
+
open_api_spec = _get_from_url(
|
|
139
|
+
service_url + '/openapi.json',
|
|
140
|
+
token=None,
|
|
141
|
+
session=session,
|
|
142
|
+
)
|
|
123
143
|
for path in open_api_spec['paths']:
|
|
124
144
|
match = matcher.match(path)
|
|
125
145
|
if match:
|
|
@@ -131,6 +151,7 @@ def collection_read_record_with_pid(service_url: str,
|
|
|
131
151
|
pid: str,
|
|
132
152
|
format: str = 'json',
|
|
133
153
|
token: str | None = None,
|
|
154
|
+
session: Session | None = None,
|
|
134
155
|
) -> dict | None:
|
|
135
156
|
"""Read record with the given pid from the collection on the service
|
|
136
157
|
|
|
@@ -146,13 +167,16 @@ def collection_read_record_with_pid(service_url: str,
|
|
|
146
167
|
either `json` or `ttl`
|
|
147
168
|
:param token: [optional] if set, a token to authenticate against
|
|
148
169
|
the endpoint, if None: no token will be sent to the endpoint
|
|
170
|
+
:param session: [optional] if set it will be used for making requests
|
|
149
171
|
|
|
150
172
|
:return: The record, if it exists, None otherwise.
|
|
151
173
|
"""
|
|
152
174
|
return get(
|
|
153
175
|
url=_build_url(service_url, collection, 'record'),
|
|
154
176
|
token=token,
|
|
155
|
-
parameters={'pid': pid, 'format': format}
|
|
177
|
+
parameters={'pid': pid, 'format': format},
|
|
178
|
+
session=session,
|
|
179
|
+
)
|
|
156
180
|
|
|
157
181
|
|
|
158
182
|
def collection_read_records(service_url: str,
|
|
@@ -163,6 +187,7 @@ def collection_read_records(service_url: str,
|
|
|
163
187
|
page: int = 1,
|
|
164
188
|
size: int = 100,
|
|
165
189
|
last_page: int | None = None,
|
|
190
|
+
session: Session | None = None,
|
|
166
191
|
) -> Generator[tuple[dict, int, int, int, int], None, None]:
|
|
167
192
|
"""Read records from the collection on the service
|
|
168
193
|
|
|
@@ -179,6 +204,7 @@ def collection_read_records(service_url: str,
|
|
|
179
204
|
:param size: int: the number of records in an individual pages (default: 100)
|
|
180
205
|
:param last_page: int | None: if int, the last page that should be returned
|
|
181
206
|
if None, all pages following `page` will be returned
|
|
207
|
+
:param session: [optional] if set it will be used for making requests
|
|
182
208
|
|
|
183
209
|
:return: A generator yielding tuples containing: the current record, the
|
|
184
210
|
current page number, the total number of pages, the size of the
|
|
@@ -192,7 +218,9 @@ def collection_read_records(service_url: str,
|
|
|
192
218
|
last_page=last_page,
|
|
193
219
|
parameters= {
|
|
194
220
|
'format': format,
|
|
195
|
-
**({'matching': matching} if matching else {})}
|
|
221
|
+
**({'matching': matching} if matching else {})},
|
|
222
|
+
session=session,
|
|
223
|
+
)
|
|
196
224
|
|
|
197
225
|
|
|
198
226
|
def collection_read_records_of_class(
|
|
@@ -205,6 +233,7 @@ def collection_read_records_of_class(
|
|
|
205
233
|
page: int = 1,
|
|
206
234
|
size: int = 100,
|
|
207
235
|
last_page: int | None = None,
|
|
236
|
+
session: Session | None = None,
|
|
208
237
|
) -> Generator[tuple[dict, int, int, int, int], None, None]:
|
|
209
238
|
"""Read records of the specified class from the collection on the service
|
|
210
239
|
|
|
@@ -222,6 +251,7 @@ def collection_read_records_of_class(
|
|
|
222
251
|
:param size: int: the number of records in an individual pages (default: 100)
|
|
223
252
|
:param last_page: int | None: if int, the last page that should be returned
|
|
224
253
|
if None, all pages following `page` will be returned
|
|
254
|
+
:param session: [optional] if set it will be used for making requests
|
|
225
255
|
|
|
226
256
|
:return: A generator yielding tuples containing: the current record, the
|
|
227
257
|
current page number, the total number of pages, the size of the
|
|
@@ -235,7 +265,9 @@ def collection_read_records_of_class(
|
|
|
235
265
|
last_page=last_page,
|
|
236
266
|
parameters= {
|
|
237
267
|
'format': format,
|
|
238
|
-
**({'matching': matching} if matching else {})}
|
|
268
|
+
**({'matching': matching} if matching else {})},
|
|
269
|
+
session=session,
|
|
270
|
+
)
|
|
239
271
|
|
|
240
272
|
|
|
241
273
|
def collection_write_record(
|
|
@@ -245,6 +277,7 @@ def collection_write_record(
|
|
|
245
277
|
record: dict | str,
|
|
246
278
|
format: str = 'json',
|
|
247
279
|
token: str | None = None,
|
|
280
|
+
session: Session | None = None,
|
|
248
281
|
) -> list[JSON]:
|
|
249
282
|
"""Write a record of the specified class to an inbox in the collection on the service
|
|
250
283
|
|
|
@@ -258,6 +291,7 @@ def collection_write_record(
|
|
|
258
291
|
:param token: [optional] if set, a token to authenticate against
|
|
259
292
|
the endpoint, if None: no token will be sent to the endpoint
|
|
260
293
|
The token must have write access to incoming area in the collection
|
|
294
|
+
:param session: [optional] if set it will be used for making requests
|
|
261
295
|
|
|
262
296
|
:return list[JSON]: a list of records that was written. There might be more
|
|
263
297
|
than one record due to inlined-relations extraction. The individual
|
|
@@ -268,7 +302,9 @@ def collection_write_record(
|
|
|
268
302
|
url=_build_url(service_url, collection, f'record/{class_name}'),
|
|
269
303
|
token=token,
|
|
270
304
|
params={'format': format},
|
|
271
|
-
|
|
305
|
+
session=session,
|
|
306
|
+
**(dict(json=record) if format == 'json' else dict(data=record)),
|
|
307
|
+
)
|
|
272
308
|
|
|
273
309
|
|
|
274
310
|
def collection_validate_record(
|
|
@@ -278,6 +314,7 @@ def collection_validate_record(
|
|
|
278
314
|
record: dict | str,
|
|
279
315
|
format: str = 'json',
|
|
280
316
|
token: str | None = None,
|
|
317
|
+
session: Session | None = None,
|
|
281
318
|
) -> list[JSON]:
|
|
282
319
|
"""Validate a record of the specified class in the collection on the service
|
|
283
320
|
|
|
@@ -294,6 +331,7 @@ def collection_validate_record(
|
|
|
294
331
|
:param token: [optional] if set, a token to authenticate against
|
|
295
332
|
the endpoint, if None: no token will be sent to the endpoint
|
|
296
333
|
The token must have write access to incoming area in the collection
|
|
334
|
+
:param session: [optional] if set it will be used for making requests
|
|
297
335
|
|
|
298
336
|
:return: True
|
|
299
337
|
"""
|
|
@@ -302,7 +340,9 @@ def collection_validate_record(
|
|
|
302
340
|
url=_build_url(service_url, collection, f'validate/{class_name}'),
|
|
303
341
|
token=token,
|
|
304
342
|
params={'format': format},
|
|
305
|
-
|
|
343
|
+
session=session,
|
|
344
|
+
**(dict(json=record) if format == 'json' else dict(data=record)),
|
|
345
|
+
)
|
|
306
346
|
|
|
307
347
|
|
|
308
348
|
def collection_delete_record(
|
|
@@ -310,6 +350,7 @@ def collection_delete_record(
|
|
|
310
350
|
collection: str,
|
|
311
351
|
pid: str,
|
|
312
352
|
token: str | None = None,
|
|
353
|
+
session: Session | None = None,
|
|
313
354
|
) -> bool:
|
|
314
355
|
"""Delete the record with the given pid from the collection on the service
|
|
315
356
|
|
|
@@ -319,19 +360,23 @@ def collection_delete_record(
|
|
|
319
360
|
:param pid: the PID of the record that should be deleted
|
|
320
361
|
:param token: [optional] if set, a token to authenticate against
|
|
321
362
|
the endpoint, if None: no token will be sent to the endpoint
|
|
363
|
+
:param session: [optional] if set it will be used for making requests
|
|
322
364
|
|
|
323
365
|
:return: True if the record was deleted, False otherwise
|
|
324
366
|
"""
|
|
325
367
|
return _delete_url(
|
|
326
368
|
url=_build_url(service_url, collection, 'record'),
|
|
327
369
|
token=token,
|
|
328
|
-
params={'pid': pid}
|
|
370
|
+
params={'pid': pid},
|
|
371
|
+
session=session,
|
|
372
|
+
)
|
|
329
373
|
|
|
330
374
|
|
|
331
375
|
def curated_read_record_with_pid(service_url: str,
|
|
332
376
|
collection: str,
|
|
333
377
|
pid: str,
|
|
334
378
|
token: str | None = None,
|
|
379
|
+
session: Session | None = None,
|
|
335
380
|
) -> dict | None:
|
|
336
381
|
"""Read record with the given pid from curated area of the collection on the service
|
|
337
382
|
|
|
@@ -345,13 +390,16 @@ def curated_read_record_with_pid(service_url: str,
|
|
|
345
390
|
:param token: [optional] if set, a token to authenticate against
|
|
346
391
|
the endpoint, if None: no token will be sent to the endpoint. A
|
|
347
392
|
token must have curator-rights
|
|
393
|
+
:param session: [optional] if set it will be used for making requests
|
|
348
394
|
|
|
349
395
|
:return: The record, if it exists, None otherwise
|
|
350
396
|
"""
|
|
351
397
|
return get(
|
|
352
398
|
url=_build_url(service_url, collection, 'curated/record'),
|
|
353
399
|
token=token,
|
|
354
|
-
parameters={'pid': pid}
|
|
400
|
+
parameters={'pid': pid},
|
|
401
|
+
session=session,
|
|
402
|
+
)
|
|
355
403
|
|
|
356
404
|
|
|
357
405
|
def curated_read_records(service_url: str,
|
|
@@ -361,6 +409,7 @@ def curated_read_records(service_url: str,
|
|
|
361
409
|
page: int = 1,
|
|
362
410
|
size: int = 100,
|
|
363
411
|
last_page: int | None = None,
|
|
412
|
+
session: Session | None = None,
|
|
364
413
|
) -> Generator[tuple[dict, int, int, int, int], None, None]:
|
|
365
414
|
"""Read records from the curated area the collection on the service
|
|
366
415
|
|
|
@@ -379,6 +428,7 @@ def curated_read_records(service_url: str,
|
|
|
379
428
|
:param size: int: the number of records in an individual pages (default: 100)
|
|
380
429
|
:param last_page: int | None: if int, the last page that should be returned
|
|
381
430
|
if None, all pages following `page` will be returned
|
|
431
|
+
:param session: [optional] if set it will be used for making requests
|
|
382
432
|
|
|
383
433
|
:return: A generator yielding tuples containing: the current record, the
|
|
384
434
|
current page number, the total number of pages, the size of the
|
|
@@ -390,7 +440,9 @@ def curated_read_records(service_url: str,
|
|
|
390
440
|
first_page=page,
|
|
391
441
|
page_size=size,
|
|
392
442
|
last_page=last_page,
|
|
393
|
-
parameters={'matching': matching} if matching else {}
|
|
443
|
+
parameters={'matching': matching} if matching else {},
|
|
444
|
+
session=session,
|
|
445
|
+
)
|
|
394
446
|
|
|
395
447
|
|
|
396
448
|
def curated_read_records_of_class(
|
|
@@ -402,6 +454,7 @@ def curated_read_records_of_class(
|
|
|
402
454
|
page: int = 1,
|
|
403
455
|
size: int = 100,
|
|
404
456
|
last_page: int | None = None,
|
|
457
|
+
session: Session | None = None,
|
|
405
458
|
) -> Generator[tuple[dict, int, int, int, int], None, None]:
|
|
406
459
|
"""Read records of class `class_name` from the curated area the collection on the service
|
|
407
460
|
|
|
@@ -421,6 +474,7 @@ def curated_read_records_of_class(
|
|
|
421
474
|
:param size: int: the number of records in an individual pages (default: 100)
|
|
422
475
|
:param last_page: int | None: if int, the last page that should be returned
|
|
423
476
|
if None, all pages following `page` will be returned
|
|
477
|
+
:param session: [optional] if set it will be used for making requests
|
|
424
478
|
|
|
425
479
|
:return: A generator yielding tuples containing: the current record, the
|
|
426
480
|
current page number, the total number of pages, the size of the
|
|
@@ -432,7 +486,9 @@ def curated_read_records_of_class(
|
|
|
432
486
|
first_page=page,
|
|
433
487
|
page_size=size,
|
|
434
488
|
last_page=last_page,
|
|
435
|
-
parameters={'matching': matching} if matching else {}
|
|
489
|
+
parameters={'matching': matching} if matching else {},
|
|
490
|
+
session=session,
|
|
491
|
+
)
|
|
436
492
|
|
|
437
493
|
|
|
438
494
|
def curated_write_record(
|
|
@@ -441,6 +497,7 @@ def curated_write_record(
|
|
|
441
497
|
class_name: str,
|
|
442
498
|
record: dict,
|
|
443
499
|
token: str | None = None,
|
|
500
|
+
session: Session | None = None,
|
|
444
501
|
) -> list[JSON]:
|
|
445
502
|
"""Write a record of the specified class to the curated area of the collection on the service
|
|
446
503
|
|
|
@@ -456,13 +513,16 @@ def curated_write_record(
|
|
|
456
513
|
:param token: [optional] if set, a token to authenticate against
|
|
457
514
|
the endpoint, if None: no token will be sent to the endpoint
|
|
458
515
|
A given token must have curator-rights for the collection
|
|
516
|
+
:param session: [optional] if set it will be used for making requests
|
|
459
517
|
|
|
460
518
|
:return list[JSON]: a list containing the record that was written
|
|
461
519
|
"""
|
|
462
520
|
return _post_to_url(
|
|
463
521
|
url=_build_url(service_url, collection, f'curated/record/{class_name}'),
|
|
464
522
|
token=token,
|
|
465
|
-
|
|
523
|
+
session=session,
|
|
524
|
+
json=record,
|
|
525
|
+
)
|
|
466
526
|
|
|
467
527
|
|
|
468
528
|
def curated_delete_record(
|
|
@@ -470,6 +530,7 @@ def curated_delete_record(
|
|
|
470
530
|
collection: str,
|
|
471
531
|
pid: str,
|
|
472
532
|
token: str | None = None,
|
|
533
|
+
session: Session | None = None,
|
|
473
534
|
) -> bool:
|
|
474
535
|
"""Delete the record with the given pid from the curated area of the collection on the service
|
|
475
536
|
|
|
@@ -480,17 +541,22 @@ def curated_delete_record(
|
|
|
480
541
|
:param token: [optional] if set, a token to authenticate against
|
|
481
542
|
the endpoint, if None: no token will be sent to the endpoint
|
|
482
543
|
A given token must have curator-rights for the collection
|
|
544
|
+
:param session: [optional] if set it will be used for making requests
|
|
545
|
+
|
|
483
546
|
:return: True if the record was deleted, False otherwise
|
|
484
547
|
"""
|
|
485
548
|
return _delete_url(
|
|
486
549
|
url=_build_url(service_url, collection, 'curated/record'),
|
|
487
550
|
token=token,
|
|
488
|
-
params={'pid': pid}
|
|
551
|
+
params={'pid': pid},
|
|
552
|
+
session=session,
|
|
553
|
+
)
|
|
489
554
|
|
|
490
555
|
|
|
491
556
|
def incoming_read_labels(service_url: str,
|
|
492
557
|
collection: str,
|
|
493
558
|
token: str | None = None,
|
|
559
|
+
session: Session | None = None,
|
|
494
560
|
) -> Generator[str, None, None]:
|
|
495
561
|
"""Read all incoming labels for the collection on the service.
|
|
496
562
|
|
|
@@ -500,12 +566,15 @@ def incoming_read_labels(service_url: str,
|
|
|
500
566
|
:param token: [optional] if set, a token to authenticate against
|
|
501
567
|
the endpoint, if None: no token will be sent to the endpoint
|
|
502
568
|
A given token must have curator-rights for the collection
|
|
569
|
+
:param session: [optional] if set it will be used for making requests
|
|
503
570
|
|
|
504
571
|
:return: list[str]: a list of incoming area labels
|
|
505
572
|
"""
|
|
506
573
|
yield from _get_from_url(
|
|
507
574
|
url=_build_url(service_url, collection,'incoming/'),
|
|
508
|
-
token=token
|
|
575
|
+
token=token,
|
|
576
|
+
session=session,
|
|
577
|
+
)
|
|
509
578
|
|
|
510
579
|
|
|
511
580
|
def incoming_read_record_with_pid(service_url: str,
|
|
@@ -513,6 +582,7 @@ def incoming_read_record_with_pid(service_url: str,
|
|
|
513
582
|
label: str,
|
|
514
583
|
pid: str,
|
|
515
584
|
token: str | None = None,
|
|
585
|
+
session: Session | None = None,
|
|
516
586
|
) -> dict | None:
|
|
517
587
|
"""Read record with the given pid from the specified incoming area of the collection on the service
|
|
518
588
|
|
|
@@ -527,13 +597,16 @@ def incoming_read_record_with_pid(service_url: str,
|
|
|
527
597
|
:param token: [optional] if set, a token to authenticate against
|
|
528
598
|
the endpoint, if None: no token will be sent to the endpoint. A
|
|
529
599
|
token must have curator-rights
|
|
600
|
+
:param session: [optional] if set it will be used for making requests
|
|
530
601
|
|
|
531
602
|
:return: The record, if it exists, None otherwise
|
|
532
603
|
"""
|
|
533
604
|
return get(
|
|
534
605
|
url=_build_incoming_url(service_url, collection, label, 'record'),
|
|
535
606
|
token=token,
|
|
536
|
-
parameters={'pid': pid}
|
|
607
|
+
parameters={'pid': pid},
|
|
608
|
+
session=session,
|
|
609
|
+
)
|
|
537
610
|
|
|
538
611
|
|
|
539
612
|
def incoming_read_records(service_url: str,
|
|
@@ -544,6 +617,7 @@ def incoming_read_records(service_url: str,
|
|
|
544
617
|
page: int = 1,
|
|
545
618
|
size: int = 100,
|
|
546
619
|
last_page: int | None = None,
|
|
620
|
+
session: Session | None = None,
|
|
547
621
|
) -> Generator[tuple[dict, int, int, int, int], None, None]:
|
|
548
622
|
"""Read records from the specified incoming area the collection on the service
|
|
549
623
|
|
|
@@ -563,6 +637,7 @@ def incoming_read_records(service_url: str,
|
|
|
563
637
|
:param size: int: the number of records in an individual pages (default: 100)
|
|
564
638
|
:param last_page: int | None: if int, the last page that should be returned
|
|
565
639
|
if None, all pages following `page` will be returned
|
|
640
|
+
:param session: [optional] if set it will be used for making requests
|
|
566
641
|
|
|
567
642
|
:return: A generator yielding tuples containing: the current record, the
|
|
568
643
|
current page number, the total number of pages, the size of the
|
|
@@ -574,7 +649,9 @@ def incoming_read_records(service_url: str,
|
|
|
574
649
|
first_page=page,
|
|
575
650
|
page_size=size,
|
|
576
651
|
last_page=last_page,
|
|
577
|
-
parameters={'matching': matching} if matching else {}
|
|
652
|
+
parameters={'matching': matching} if matching else {},
|
|
653
|
+
session=session,
|
|
654
|
+
)
|
|
578
655
|
|
|
579
656
|
|
|
580
657
|
def incoming_read_records_of_class(
|
|
@@ -587,6 +664,7 @@ def incoming_read_records_of_class(
|
|
|
587
664
|
page: int = 1,
|
|
588
665
|
size: int = 100,
|
|
589
666
|
last_page: int | None = None,
|
|
667
|
+
session: Session | None = None,
|
|
590
668
|
) -> Generator[tuple[dict, int, int, int, int], None, None]:
|
|
591
669
|
"""Read records of the specified class from the specified incoming area the collection on the service
|
|
592
670
|
|
|
@@ -607,6 +685,7 @@ def incoming_read_records_of_class(
|
|
|
607
685
|
:param size: int: the number of records in an individual pages (default: 100)
|
|
608
686
|
:param last_page: int | None: if int, the last page that should be returned
|
|
609
687
|
if None, all pages following `page` will be returned
|
|
688
|
+
:param session: [optional] if set it will be used for making requests
|
|
610
689
|
|
|
611
690
|
:return: A generator yielding tuples containing: the current record, the
|
|
612
691
|
current page number, the total number of pages, the size of the
|
|
@@ -618,7 +697,9 @@ def incoming_read_records_of_class(
|
|
|
618
697
|
first_page=page,
|
|
619
698
|
page_size=size,
|
|
620
699
|
last_page=last_page,
|
|
621
|
-
parameters={'matching': matching} if matching else {}
|
|
700
|
+
parameters={'matching': matching} if matching else {},
|
|
701
|
+
session=session,
|
|
702
|
+
)
|
|
622
703
|
|
|
623
704
|
|
|
624
705
|
def incoming_write_record(
|
|
@@ -628,6 +709,7 @@ def incoming_write_record(
|
|
|
628
709
|
class_name: str,
|
|
629
710
|
record: dict,
|
|
630
711
|
token: str | None = None,
|
|
712
|
+
session: Session | None = None,
|
|
631
713
|
) -> list[JSON]:
|
|
632
714
|
"""Write a record of the specified class to the specified incoming area of the collection on the service
|
|
633
715
|
|
|
@@ -644,13 +726,16 @@ def incoming_write_record(
|
|
|
644
726
|
:param token: [optional] if set, a token to authenticate against
|
|
645
727
|
the endpoint, if None: no token will be sent to the endpoint
|
|
646
728
|
A given token must have curator-rights for the collection
|
|
729
|
+
:param session: [optional] if set it will be used for making requests
|
|
647
730
|
|
|
648
731
|
:return list[JSON]: a list containing the record that was written
|
|
649
732
|
"""
|
|
650
733
|
return _post_to_url(
|
|
651
734
|
url=_build_incoming_url(service_url, collection, label, f'record/{class_name}'),
|
|
652
735
|
token=token,
|
|
653
|
-
json=record
|
|
736
|
+
json=record,
|
|
737
|
+
session=session,
|
|
738
|
+
)
|
|
654
739
|
|
|
655
740
|
|
|
656
741
|
def incoming_delete_record(
|
|
@@ -659,6 +744,7 @@ def incoming_delete_record(
|
|
|
659
744
|
label: str,
|
|
660
745
|
pid: str,
|
|
661
746
|
token: str | None = None,
|
|
747
|
+
session: Session | None = None,
|
|
662
748
|
) -> bool:
|
|
663
749
|
"""Delete the record with the given pid from the specified incoming area of the collection on the service
|
|
664
750
|
|
|
@@ -670,22 +756,27 @@ def incoming_delete_record(
|
|
|
670
756
|
:param token: [optional] if set, a token to authenticate against
|
|
671
757
|
the endpoint, if None: no token will be sent to the endpoint
|
|
672
758
|
A given token must have curator-rights for the collection
|
|
759
|
+
:param session: [optional] if set, it will be used for requests
|
|
673
760
|
|
|
674
761
|
:return: True if the record was deleted, False otherwise
|
|
675
762
|
"""
|
|
676
763
|
return _delete_url(
|
|
677
764
|
url=_build_incoming_url(service_url, collection, label,'record'),
|
|
678
765
|
token=token,
|
|
679
|
-
params={'pid': pid}
|
|
766
|
+
params={'pid': pid},
|
|
767
|
+
session=session,
|
|
768
|
+
)
|
|
680
769
|
|
|
681
770
|
|
|
682
771
|
def server(
|
|
683
772
|
service_url: str,
|
|
773
|
+
session: Session | None = None,
|
|
684
774
|
) -> JSON:
|
|
685
775
|
"""Get server-information from the service
|
|
686
776
|
|
|
687
777
|
:param service_url: the base URL of the service, i.e., the URL up to
|
|
688
778
|
`/<collection>/...` or `/server`
|
|
779
|
+
:param session: an optional requests.Session object to use for making requests
|
|
689
780
|
|
|
690
781
|
:return: information returned by the `<service_url>/server` endpoint
|
|
691
782
|
"""
|
|
@@ -693,7 +784,8 @@ def server(
|
|
|
693
784
|
(f'{service_url[:-1]}' if service_url.endswith('/') else service_url)
|
|
694
785
|
+ '/server'
|
|
695
786
|
)
|
|
696
|
-
|
|
787
|
+
method = session.get if session else requests.get
|
|
788
|
+
return _do_request(method, url=url, token=None, params=None)
|
|
697
789
|
|
|
698
790
|
|
|
699
791
|
def maintenance(
|
|
@@ -701,6 +793,7 @@ def maintenance(
|
|
|
701
793
|
collection: str,
|
|
702
794
|
active: bool,
|
|
703
795
|
token: str,
|
|
796
|
+
session: Session | None = None,
|
|
704
797
|
) -> None:
|
|
705
798
|
"""Activate or deactivate maintenance mode of a collection
|
|
706
799
|
|
|
@@ -711,6 +804,7 @@ def maintenance(
|
|
|
711
804
|
non-active (`False`).
|
|
712
805
|
:param token: a token to authenticate against the endpoint, the token
|
|
713
806
|
must have curator-rights for the collection
|
|
807
|
+
:param session: an optional requests.Session object to use for making requests
|
|
714
808
|
"""
|
|
715
809
|
url = (
|
|
716
810
|
(f'{service_url[:-1]}' if service_url.endswith('/') else service_url)
|
|
@@ -719,30 +813,37 @@ def maintenance(
|
|
|
719
813
|
_post_to_url(
|
|
720
814
|
url=url,
|
|
721
815
|
token=token,
|
|
722
|
-
|
|
816
|
+
session=session,
|
|
817
|
+
json={'collection': collection, 'active': active},
|
|
723
818
|
)
|
|
724
819
|
|
|
725
820
|
|
|
726
821
|
def _get_from_url(url: str,
|
|
727
822
|
token: str | None,
|
|
728
823
|
params: dict[str, str] | None = None,
|
|
824
|
+
session: Session | None = None,
|
|
729
825
|
) -> JSON:
|
|
730
|
-
|
|
826
|
+
method = session.get if session else requests.get
|
|
827
|
+
return _do_request(method, url, token, params=params)
|
|
731
828
|
|
|
732
829
|
|
|
733
830
|
def _post_to_url(url: str,
|
|
734
831
|
token: str | None,
|
|
735
832
|
params: dict[str, str] | None = None,
|
|
833
|
+
session: Session | None = None,
|
|
736
834
|
**kwargs,
|
|
737
835
|
) -> JSON:
|
|
738
|
-
|
|
836
|
+
method = session.post if session else requests.post
|
|
837
|
+
return _do_request(method, url, token, params=params, **kwargs)
|
|
739
838
|
|
|
740
839
|
|
|
741
840
|
def _delete_url(url: str,
|
|
742
841
|
token: str | None,
|
|
743
842
|
params: dict[str, str] | None = None,
|
|
843
|
+
session: Session | None = None,
|
|
744
844
|
) -> JSON:
|
|
745
|
-
|
|
845
|
+
method = session.delete if session else requests.delete
|
|
846
|
+
return _do_request(method, url, token, params=params)
|
|
746
847
|
|
|
747
848
|
|
|
748
849
|
def _do_request(method: Callable,
|
|
@@ -784,11 +885,12 @@ def _get_page(url_base: str,
|
|
|
784
885
|
first_page: int = 1,
|
|
785
886
|
page_size: int = 100,
|
|
786
887
|
parameters: dict | None = None,
|
|
888
|
+
session: Session | None = None,
|
|
787
889
|
) -> JSON:
|
|
788
890
|
parameters = parameters or {}
|
|
789
891
|
parameters['page'] = first_page
|
|
790
892
|
parameters['size'] = page_size
|
|
791
|
-
return _get_from_url(url_base, token, parameters)
|
|
893
|
+
return _get_from_url(url_base, token, parameters, session)
|
|
792
894
|
|
|
793
895
|
|
|
794
896
|
def _check_format_value(format: str) -> None:
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
dump_things_pyclient/__init__.py,sha256=cn-U3TRIalN6aYHp1cMBRkQm1x98XBwquLFbgFEIf_Q,113
|
|
2
|
-
dump_things_pyclient/communicate.py,sha256=
|
|
2
|
+
dump_things_pyclient/communicate.py,sha256=c46PO2TkrbGzhSg5IfWew9ecYW_Lo3xrf7PbnJGovnA,35910
|
|
3
3
|
dump_things_pyclient/commands/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
4
|
dump_things_pyclient/commands/dtc.py,sha256=dxW5RuogqwhzfVujZ_EEsQMk8BcVMbZyMdg5c8EvYIA,1726
|
|
5
5
|
dump_things_pyclient/commands/json2ttl.py,sha256=8BkvdjLWZ_H0L6fTmuR2M2MglKiMUiuNUcuWr_w6_dQ,2133
|
|
6
6
|
dump_things_pyclient/commands/redirect.py,sha256=kl8pGj8khjxk4lhk8AJLfgtCIm5PtjeMAl0J6K5FB7M,264
|
|
7
7
|
dump_things_pyclient/commands/dtc_plugins/__init__.py,sha256=0YLByLiofhHkhJcDCkokldcCw3Jj0rsKJinRX4tt3Hc,514
|
|
8
|
-
dump_things_pyclient/commands/dtc_plugins/auto_curate.py,sha256=
|
|
9
|
-
dump_things_pyclient/commands/dtc_plugins/clean_incoming.py,sha256=
|
|
10
|
-
dump_things_pyclient/commands/dtc_plugins/
|
|
11
|
-
dump_things_pyclient/commands/dtc_plugins/export.py,sha256=
|
|
12
|
-
dump_things_pyclient/commands/dtc_plugins/get_records.py,sha256=
|
|
8
|
+
dump_things_pyclient/commands/dtc_plugins/auto_curate.py,sha256=dIpTJ4IqaORqMH0j-h7dr-7aoxjsJADNALpUKbVskcs,9997
|
|
9
|
+
dump_things_pyclient/commands/dtc_plugins/clean_incoming.py,sha256=ikSPNTt254ax2tXhMK_gTgDCVkxMYJ_0NTAP8XsaRjk,2188
|
|
10
|
+
dump_things_pyclient/commands/dtc_plugins/delete_records.py,sha256=SRQTHz4cWofI-RVx_p_mUex3amTaGZ9xP_S4F12Pw64,3849
|
|
11
|
+
dump_things_pyclient/commands/dtc_plugins/export.py,sha256=s-uPWGLoZbyd4oyicNlk1_2f9MJWYDtSmLq7nTiKvE0,5232
|
|
12
|
+
dump_things_pyclient/commands/dtc_plugins/get_records.py,sha256=5v3RUrNjEO4T5Ku0ZwGBy39dNxG5K-UcOwUigL8N4Vo,7615
|
|
13
13
|
dump_things_pyclient/commands/dtc_plugins/list_incoming.py,sha256=tmM0Qs4MVwMMLyERsWCxWGTM90rSNOShLpHH32wObd8,1959
|
|
14
|
-
dump_things_pyclient/commands/dtc_plugins/maintenance.py,sha256=
|
|
15
|
-
dump_things_pyclient/commands/dtc_plugins/post_records.py,sha256=
|
|
16
|
-
dump_things_pyclient/commands/dtc_plugins/read_pages.py,sha256=
|
|
17
|
-
dump_things_pyclient-0.2.
|
|
18
|
-
dump_things_pyclient-0.2.
|
|
19
|
-
dump_things_pyclient-0.2.
|
|
20
|
-
dump_things_pyclient-0.2.
|
|
21
|
-
dump_things_pyclient-0.2.
|
|
14
|
+
dump_things_pyclient/commands/dtc_plugins/maintenance.py,sha256=yTw1T_cvVTmwuzrTPteu6O6qiNCMxL5ZQoVF8yb72-M,1707
|
|
15
|
+
dump_things_pyclient/commands/dtc_plugins/post_records.py,sha256=0676miD7VTMmokBAo7JdA9Dr9FZwhs0auNuRltulcBw,3469
|
|
16
|
+
dump_things_pyclient/commands/dtc_plugins/read_pages.py,sha256=Libxf36L-0wUqAqfavotZPRMy5LjWJ37n_zSae1TgTA,3546
|
|
17
|
+
dump_things_pyclient-0.2.6.dist-info/METADATA,sha256=9U_JvtQrlduHw2UZboU4uWfcbbAryqwBvnxCe3hoCg4,999
|
|
18
|
+
dump_things_pyclient-0.2.6.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
|
|
19
|
+
dump_things_pyclient-0.2.6.dist-info/entry_points.txt,sha256=U1QhQtk767G_OXdZwPdTXYbIPfcDU13Z2u1d6exX8uE,470
|
|
20
|
+
dump_things_pyclient-0.2.6.dist-info/top_level.txt,sha256=Asvruw-SyLoYhWis1CFOx89RGxpjXoTZVGoq4JSGt88,21
|
|
21
|
+
dump_things_pyclient-0.2.6.dist-info/RECORD,,
|
|
File without changes
|
{dump_things_pyclient-0.2.5.dist-info → dump_things_pyclient-0.2.6.dist-info}/entry_points.txt
RENAMED
|
File without changes
|
|
File without changes
|