scan
Scan REST API ResourcesLink
Provides Flask‑RESTful resources for managing scan datasets in PlantDB. The resources expose endpoints that allow clients to list scans, retrieve detailed information, create new scans, and manipulate scan metadata and filesets. These APIs simplify integration with PlantDB by handling request parsing, security (rate‑limiting and JWT extraction), and data sanitization.
Key FeaturesLink
- ScansList - Returns a list of scan identifiers with optional JSON‑based filtering and fuzzy matching.
- ScansTable - Supplies detailed information (metadata, tasks, files) for each scan, supporting the same filtering capabilities.
- Scan - Retrieves or creates a single scan, performing ID sanitization and returning comprehensive scan data.
- ScanCreate - Dedicated endpoint for creating new scans with optional metadata.
- ScanMetadata - GET/POST endpoints to read and update a scan’s metadata, with support for partial updates or full replacement.
- ScanFilesets - Lists filesets contained in a scan, also supporting query filtering and fuzzy matching.
- Security Integration - Leverages JWT-based authentication via the
@add_jwt_from_headerdecorator to ensure secure access to file operations.
Usage ExamplesLink
Below is a minimal example that demonstrates how to register the ScansList resource with a Flask‑RESTful application.
>>> import logging
>>> from flask import Flask
>>> from flask_restful import Api
>>> from plantdb.server.api.scan import ScansList
>>> from plantdb.commons.auth.session import JWTSessionManager
>>> from plantdb.commons.fsdb.core import FSDB
>>> from plantdb.commons.test_database import setup_test_database
>>> # Create a Flask application
>>> app = Flask(__name__)
>>> # Create a logger
>>> logger = logging.getLogger("plantdb.scan")
>>> logger.setLevel(logging.INFO)
>>> # Initialize a test database with a JWTSessionManager
>>> db_path = setup_test_database('real_plant')
>>> mgr = JWTSessionManager()
>>> db = FSDB(db_path, session_manager=mgr)
>>> db.connect()
>>> # RESTful API and resource registration
>>> api = Api(app)
>>> api.add_resource(ScansList, "/scans", resource_class_kwargs={"db": db})
>>> # Start the APP
>>> app.run(host='0.0.0.0', port=5000)
It may be used as follows (in another Python REPL):
>>> import requests
>>> # List all scans
>>> response = requests.get("http://127.0.0.1:5000/scans")
>>> scans = response.json()
>>> print(scans)
['real_plant_analyzed', 'real_plant', 'virtual_plant_analyzed', 'virtual_plant']
>>> # Run a filtered, fuzzy search on metadata
>>> query = {"object": {"environment":"Lyon.*"}}
>>> response = requests.get("http://127.0.0.1:5000/scans", params= {"filterQuery": query, "fuzzy": "true"})
>>> filtered_scans = response.json()
>>> print(filtered_scans) # list of scan IDs matching the filter
['real_plant_analyzed', 'real_plant']
Scan
Link
Bases: Resource
A RESTful resource class for serving scan dataset information.
This class handles HTTP GET requests for scan datasets, providing detailed information about the scan, including metadata, file locations, and task status.
Attributes:
| Name | Type | Description |
|---|---|---|
db |
FSDB
|
The database providing the resources to serve. |
logger |
Logger
|
The logger used to record operations and errors. |
Notes
The class sanitizes scan IDs before processing requests to ensure security. All responses are returned as JSON-serializable dictionaries.
See Also
plantdb.server.services.scan.get_scan_info : Function used to collect and format scan information plantdb.server.core.security.sanitize_name : Function used to validate and clean scan IDs
Initialize the resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
A database instance providing the resources to serve. |
required |
|
Logger
|
A logger instance to record operations and errors. |
None
|
Source code in plantdb/server/api/scan.py
342 343 344 345 346 347 348 349 350 351 352 353 | |
get
Link
get(scan_id, **kwargs)
Retrieve detailed information about a specific scan dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Identifier for the scan dataset. Must contain only alphanumeric characters, underscores, dashes, or periods. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary containing scan information with the following keys: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the scan_id contains invalid characters |
NotFoundError
|
If the specified scan does not exist in the database |
HTTPException
|
If the rate limit is exceeded, it returns an HTTP 429 ("Too Many Requests") response to the client. |
Examples:
>>> # Start a test REST API server first:
>>> # $ fsdb_rest_api --test
>>> import requests
>>> # Get detailed information about a specific dataset
>>> response = requests.get("http://127.0.0.1:5000/scan/real_plant_analyzed")
>>> scan_data = response.json()
>>> # Access metadata information
>>> print(scan_data['metadata']['nbPhotos'])
60
Source code in plantdb/server/api/scan.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
post
Link
post(scan_id, **kwargs)
Create a new scan dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Identifier for the new scan dataset. Must contain only alphanumeric characters, underscores, dashes, or periods. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary containing the response with the following possible structures: |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If the rate limit is exceeded, it returns an HTTP 429 ("Too Many Requests") response to the client. |
Notes
The request body accepts a JSON object containing:
- 'metadata' (dict): the new metadata
Examples:
>>> # Start a test REST API server first:
>>> # $ fsdb_rest_api --test
>>> import requests
>>> # Start by log in as 'admin'
>>> response = requests.post('http://127.0.0.1:5000/login', json={'username': 'admin', 'password': 'admin'})
>>> token = response.json()['access_token']
>>> # Create a new scan dataset
>>> response = requests.post("http://127.0.0.1:5000/scan/test_scan_001", headers={'Authorization': 'Bearer ' + token})
>>> print(response.ok)
True
>>> scan_data = response.json()
>>> print(scan_data['id'])
test_scan_001
>>> md = {'metadata': {'test_metadata': True}}
>>> response = requests.post("http://127.0.0.1:5000/scan/test_scan_md", json=md, headers={'Authorization': 'Bearer ' + token})
>>> print(response.ok)
Source code in plantdb/server/api/scan.py
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | |
ScanFilesets
Link
Bases: Resource
REST API resource for managing scan filesets operations.
This class provides endpoints to interact with filesets within a scan. It allows listing filesets with optional query filtering and fuzzy matching capabilities.
Attributes:
| Name | Type | Description |
|---|---|---|
db |
FSDB
|
The database providing the resources to serve. |
logger |
Logger
|
The logger used to record operations and errors. |
Initialize the resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
A database instance providing the resources to serve. |
required |
|
Logger
|
A logger instance to record operations and errors. |
None
|
Source code in plantdb/server/api/scan.py
679 680 681 682 683 684 685 686 687 688 689 690 | |
get
Link
get(scan_id, **kwargs)
List all filesets in a specified scan.
This method retrieves the list of filesets contained in a scan using the
list_filesets() method from plantdb.commons.fsdb.core.Scan.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The ID of the scan. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Response containing either: - On success (200): {'filesets': list of fileset IDs} - On error (404, 500): {'message': error description} |
int
|
HTTP status code (200, 404, or 500) |
Examples:
>>> # Start a test REST API server first:
>>> # $ fsdb_rest_api --test
>>> import requests
>>> # List filesets in a scan:
>>> url = "http://127.0.0.1:5000/scan/real_plant/filesets"
>>> response = requests.get(url)
>>> print(response.status_code)
200
>>> print(response.json())
{'filesets': ['images']}
>>> url = "http://127.0.0.1:5000/scan/real_plant_analyzed/filesets"
>>> response = requests.get(url)
>>> print(len(response.json()['filesets'])) # Get the number of filesets
10
Source code in plantdb/server/api/scan.py
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 | |
ScanMetadata
Link
Bases: Resource
Resource class for managing scan metadata operations through REST API endpoints.
This class provides HTTP endpoints for retrieving and updating scan metadata through a RESTful interface. It handles both complete metadata operations and individual key access.
Attributes:
| Name | Type | Description |
|---|---|---|
db |
FSDB
|
The database providing the resources to serve. |
logger |
Logger
|
The logger used to record operations and errors. |
Initialize the resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
A database instance providing the resources to serve. |
required |
|
Logger
|
A logger instance to record operations and errors. |
None
|
Source code in plantdb/server/api/scan.py
507 508 509 510 511 512 513 514 515 516 517 518 | |
get
Link
get(scan_id, **kwargs)
Retrieve metadata for a specified scan.
This method retrieves the metadata dictionary for a scan. Optionally, it can return the value for a specific metadata key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The ID of the scan. |
required |
Returns:
| Type | Description |
|---|---|
Union[dict, Any]
|
Without a 'key' URL parameter, it returns the complete metadata dictionary. If a 'key' URL parameter is provided, it returns the value for that key. |
Raises:
| Type | Description |
|---|---|
ScanNotFoundError
|
If the specified scan doesn't exist. |
KeyError
|
If the specified key doesn't exist in the metadata. |
Notes
The method can take direct parameters in the request body with the following fields:
- key (str): If provided, returns only the value for this specific metadata key.
Examples:
>>> # Start a test REST API server first:
>>> # $ fsdb_rest_api --test
>>> import requests
>>> url = "http://127.0.0.1:5000/scan/real_plant/metadata"
>>> response = requests.get(url) # Get all metadata
>>> metadata = response.json()['metadata']
>>> print(metadata['owner'])
guest
>>> response = requests.get(url+"?key=owner") # Get a specific metadata key
>>> print(response.json()['metadata'])
guest
Source code in plantdb/server/api/scan.py
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | |
post
Link
post(scan_id, **kwargs)
Update metadata for a specified scan.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The ID of the scan to update metadata for |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Response dictionary with either: - 'metadata': Updated metadata dictionary on success - 'message': Error message on failure |
int
|
HTTP status code (200 for success, 4xx/5xx for errors) |
Notes
The request body accepts a JSON object containing:
- 'metadata' (dict): the new metadata
Examples:
>>> # Start a test REST API server first:
>>> # $ fsdb_rest_api --test
>>> import requests
>>> # Start by log in as 'admin'
>>> response = requests.post('http://127.0.0.1:5000/login', json={'username': 'admin', 'password': 'admin'})
>>> token = response.json()['access_token']
>>> # Get the whole metadata dictionary for an existing dataset:
>>> url = "http://127.0.0.1:5000/scan/real_plant/metadata"
>>> response = requests.get(url) # Get all metadata
>>> metadata = response.json()['metadata']
>>> # Update the original metadata dictionary and upload it to the database:
>>> metadata['object']['description'] = 'Test plant scan'
>>> response = requests.post(url, json={'metadata': metadata}, headers={'Authorization': 'Bearer ' + token})
>>> print(response.ok)
True
>>> print(response.json()['metadata']['object']['description'])
Test plant scan
Source code in plantdb/server/api/scan.py
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 | |
ScansList
Link
Bases: Resource
A RESTful resource for managing and retrieving scan datasets.
This class implements a REST API endpoint that provides access to scan datasets. It supports filtered queries and fuzzy matching capabilities through HTTP GET requests.
Attributes:
| Name | Type | Description |
|---|---|---|
db |
FSDB
|
The database providing the resources to serve. |
logger |
Logger
|
The logger used to record operations and errors. |
Initialize the resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
A database instance providing the resources to serve. |
required |
|
Logger
|
A logger instance to record operations and errors. |
None
|
Source code in plantdb/server/api/scan.py
133 134 135 136 137 138 139 140 141 142 143 144 | |
get
Link
get()
Retrieve a list of scan datasets with optional filtering.
This endpoint provides access to scan datasets stored in the database. It allows filtering of results using a JSON-formatted query string and supports fuzzy matching for string-based searches.
Returns:
| Type | Description |
|---|---|
list
|
The List of scan datasets matching the filter criteria. Each item is a dictionary containing dataset metadata. |
int
|
HTTP status code ( |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If the rate limit is exceeded, it returns an HTTP 429 ("Too Many Requests") response to the client. |
Notes
The method can take direct parameters in the request body with the following fields:
- filter_query (str): JSON string representing the filter query, example: ``{"object":{"species":"Arabidopsis.*"}}``.
- fuzzy (str): Boolean indicating whether to perform fuzzy filtering, ``false`` by default.
Examples:
>>> # Start a test REST API server first:
>>> # $ fsdb_rest_api --test
>>> import requests
>>> # Get a list of all datasets from the DB:
>>> response = requests.get("http://127.0.0.1:5000/scans")
>>> scans_list = response.json()
>>> print(scans_list) # List the known dataset ids
['arabidopsis000', 'virtual_plant_analyzed', 'real_plant_analyzed', 'real_plant', 'virtual_plant']
>>> # Get a list of datasets with fuzzy filtering on metadata
>>> query = {"object": {"environment": "Lyon.*"}}
>>> response = requests.get("http://127.0.0.1:5000/scans", params={"filterQuery": query, "fuzzy": "true"})
>>> filtered_scans = response.json()
>>> print(filtered_scans) # list of scan IDs matching the filter
['real_plant_analyzed', 'real_plant']
Source code in plantdb/server/api/scan.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
ScansTable
Link
Bases: Resource
A RESTful resource for managing and retrieving scan dataset information.
This class provides a REST API endpoint to serve information about scan datasets. It supports filtering datasets based on query parameters and returns detailed information about each matching scan.
Attributes:
| Name | Type | Description |
|---|---|---|
db |
FSDB
|
The database providing the resources to serve. |
logger |
Logger
|
The logger instance for this resource. |
See Also
plantdb.server.services.scan.get_scan_info : Function used to extract information for each scan
Initialize the resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
A database instance providing the resources to serve. |
required |
|
Logger
|
A logger instance to record operations and errors. |
None
|
Source code in plantdb/server/api/scan.py
229 230 231 232 233 234 235 236 237 238 239 240 | |
get
Link
get(**kwargs)
Retrieve a list of scan dataset information.
This method handles GET requests to retrieve scan information. It supports filtering through query parameters and returns detailed information about matching scans.
Returns:
| Type | Description |
|---|---|
List[Dict]
|
List of dictionaries containing scan information with: |
Raises:
| Type | Description |
|---|---|
JSONDecodeError
|
If the provided filterQuery parameter is not valid JSON. |
HTTPException
|
If the rate limit is exceeded, it returns an HTTP 429 ("Too Many Requests") response to the client. |
Notes
The method can take direct parameters in the request body with the following fields:
- filter_query: JSON string representing the filter query, example: ``{"object":{"species":"Arabidopsis.*"}}``.
- fuzzy: Boolean indicating whether to perform fuzzy filtering, ``False`` by default.
Examples:
>>> # Start a test REST API server first:
>>> # $ fsdb_rest_api --test
>>> import requests
>>> import json
>>> # Get a list of information dictionaries about all datasets:
>>> response = requests.get("http://127.0.0.1:5000/scans_info")
>>> scans_info = response.json()
>>> # List the known dataset id:
>>> print(sorted(scan['id'] for scan in scans_info))
['arabidopsis000', 'real_plant', 'real_plant_analyzed', 'virtual_plant', 'virtual_plant_analyzed']
>>> # Add a metadata filter to the query:
>>> query = {"object": {"species": "Arabidopsis.*"}}
>>> response = requests.get("http://127.0.0.1:5000/scans_info", params={"filterQuery": json.dumps(query), "fuzzy": "true"})
>>> scans_info = response.json()
>>> print(sorted(scan['id'] for scan in scans_info))
['arabidopsis000']
Source code in plantdb/server/api/scan.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |