|
17 | 17 | import requests
|
18 | 18 | import yaml
|
19 | 19 | from more_itertools import take
|
| 20 | +from pystac import Collection |
20 | 21 | from pystac import STACValidationError
|
21 | 22 | from pystac_client import Client
|
22 | 23 | from requests import Request
|
@@ -580,28 +581,92 @@ def validate_collections(
|
580 | 581 | errors: Errors,
|
581 | 582 | r_session: Session,
|
582 | 583 | ) -> None:
|
583 |
| - print("WARNING: Collections validation is not yet fully implemented.") |
584 |
| - |
585 | 584 | if not (data_link := link_by_rel(root_body["links"], "data")):
|
586 | 585 | errors += "[Collections] /: Link[rel=data] must href /collections"
|
587 | 586 | else:
|
588 |
| - _, body, _ = retrieve( |
589 |
| - f"{data_link['href']}/{collection}", |
| 587 | + retrieve( |
| 588 | + f"{data_link['href']}/non-existent-collection", |
590 | 589 | errors,
|
591 | 590 | "Collections",
|
| 591 | + status_code=404, |
592 | 592 | r_session=r_session,
|
| 593 | + additional="non-existent collection", |
593 | 594 | )
|
594 |
| - # todo: validate body |
595 | 595 |
|
596 |
| - retrieve( |
597 |
| - f"{data_link['href']}/non-existent-collection", |
| 596 | + collections_url = f"{data_link['href']}" |
| 597 | + _, body, resp_headers = retrieve( |
| 598 | + collections_url, |
598 | 599 | errors,
|
599 | 600 | "Collections",
|
600 |
| - status_code=404, |
601 | 601 | r_session=r_session,
|
602 |
| - additional="non-existent collection", |
603 | 602 | )
|
604 | 603 |
|
| 604 | + if not body: |
| 605 | + errors += "[Collections] /collections body was empty" |
| 606 | + else: |
| 607 | + if ( |
| 608 | + not resp_headers |
| 609 | + or resp_headers.get("content-type") != "application/json" |
| 610 | + ): |
| 611 | + errors += "[Collections] /collections content-type header was not application/json" |
| 612 | + |
| 613 | + if not (self_link := link_by_rel(body.get("links", []), "self")): |
| 614 | + errors += "[Collections] /collections does not have self link" |
| 615 | + elif collections_url != self_link.get("href"): |
| 616 | + errors += ( |
| 617 | + "[Collections] /collections self link does not match requested url" |
| 618 | + ) |
| 619 | + |
| 620 | + if not link_by_rel(body.get("links", []), "root"): |
| 621 | + errors += "[Collections] /collections does not have root link" |
| 622 | + |
| 623 | + if body.get("collections") is None: |
| 624 | + errors += "[Collections] /collections does not have 'collections' field" |
| 625 | + |
| 626 | + if not (collections_list := body.get("collections")): |
| 627 | + errors += "[Collections] /collections 'collections' field is empty" |
| 628 | + else: |
| 629 | + try: |
| 630 | + for c in collections_list: |
| 631 | + Collection.from_dict(c) |
| 632 | + except Exception as e: |
| 633 | + errors += f"[Collections] /collections Collection '{c['id']}' failed pystac hydration: {e}" |
| 634 | + |
| 635 | + collection_url = f"{data_link['href']}/{collection}" |
| 636 | + _, body, resp_headers = retrieve( |
| 637 | + collection_url, |
| 638 | + errors, |
| 639 | + "Collections", |
| 640 | + r_session=r_session, |
| 641 | + ) |
| 642 | + |
| 643 | + if not body: |
| 644 | + errors += f"[Collections] /collections/{collection} body was empty" |
| 645 | + else: |
| 646 | + if ( |
| 647 | + not resp_headers |
| 648 | + or resp_headers.get("content-type") != "application/json" |
| 649 | + ): |
| 650 | + errors += f"[Collections] /collections/{collection} content-type header was not application/json" |
| 651 | + |
| 652 | + if not (self_link := link_by_rel(body.get("links", []), "self")): |
| 653 | + errors += f"[Collections] /collections/{collection} does not have self link" |
| 654 | + elif collection_url != self_link.get("href"): |
| 655 | + errors += f"[Collections] /collections/{collection} self link does not match requested url" |
| 656 | + |
| 657 | + if not link_by_rel(body.get("links", []), "root"): |
| 658 | + errors += f"[Collections] /collections/{collection} does not have root link" |
| 659 | + |
| 660 | + if not link_by_rel(body.get("links", []), "parent"): |
| 661 | + errors += f"[Collections] /collections/{collection} does not have parent link" |
| 662 | + |
| 663 | + try: |
| 664 | + Collection.from_dict(body) |
| 665 | + except Exception as e: |
| 666 | + errors += f"[Collections] /collections/{collection} failed pystac hydration: {e}" |
| 667 | + |
| 668 | + # todo: collection pagination |
| 669 | + |
605 | 670 |
|
606 | 671 | def validate_features(
|
607 | 672 | root_body: Dict[str, Any],
|
|
0 commit comments