|
1 | 1 | """metocean-edr-profile core requirements. See <https://github.com/EUMETNET/metocean-edr-profile/>.""" |
2 | 2 |
|
3 | 3 | import json |
| 4 | +import re |
4 | 5 |
|
5 | 6 | import pint |
6 | 7 | import requests |
@@ -42,33 +43,55 @@ def requirement7_2(jsondata: dict, timeout: int = 10) -> tuple[bool, str]: |
42 | 43 | returns status: bool, message |
43 | 44 | """ |
44 | 45 | spec_url = f"{spec_base_url}#_openapi" |
45 | | - openapi_type = "application/vnd.oai.openapi+json;version=3.1" |
| 46 | + openapi_types = [ |
| 47 | + "application/openapi+json", |
| 48 | + "application/openapi+yaml", |
| 49 | + "application/vnd.oai.openapi+json", |
| 50 | + "application/vnd.oai.openapi+yaml", |
| 51 | + ] |
46 | 52 | servicedoc_type = "text/html" |
47 | 53 |
|
48 | 54 | service_desc_link = "" |
49 | 55 | service_desc_type = "" |
50 | 56 | for link in jsondata["links"]: |
51 | 57 | if link["rel"] == "service-desc": |
52 | 58 | service_desc_link = link["href"] |
53 | | - service_desc_type = link["type"] |
| 59 | + service_desc_type, service_desc_version = (link["type"].split(";", 1) + [None, None])[:2] |
54 | 60 | break |
55 | 61 |
|
| 62 | + # print(service_desc_type, service_desc_version, file=sys.stderr) |
| 63 | + # logging.DEBUG(service_desc_type, service_desc_version) |
56 | 64 | if not service_desc_link: |
57 | 65 | return ( |
58 | 66 | False, |
59 | 67 | f"No service-desc link found. See <{spec_url}> for more info.", |
60 | 68 | ) |
61 | 69 |
|
62 | 70 | # C - relation type |
63 | | - if openapi_type not in service_desc_type: |
| 71 | + if service_desc_type not in openapi_types: |
64 | 72 | return ( |
65 | 73 | False, |
66 | | - "OpenAPI link service-desc should identify the content as openAPI " |
67 | | - f"and include version. Example <{openapi_type}>. Found: " |
| 74 | + "OpenAPI link service-desc must identify the content as openAPI " |
| 75 | + f"and should include version. Example <{openapi_types[0]}>. Found: " |
68 | 76 | f"<{service_desc_type}>. See <{spec_url}> and <{spec_base_url}" |
69 | 77 | "#_openapi_2> for more info.", |
70 | 78 | ) |
71 | 79 |
|
| 80 | + if service_desc_version: |
| 81 | + m = re.match(r"^version=(\d+\.\d+)$", service_desc_version) |
| 82 | + if m: |
| 83 | + version = m.groups()[0] |
| 84 | + if version != "3.1": # profile should allow later versions FIXME |
| 85 | + return ( |
| 86 | + False, |
| 87 | + f"OpenAPI version not 3.1: {version}.", |
| 88 | + ) |
| 89 | + else: |
| 90 | + return ( |
| 91 | + False, |
| 92 | + f"OpenAPI version suffix malformed: {service_desc_version}.", |
| 93 | + ) |
| 94 | + |
72 | 95 | # A - described using an OpenAPI document |
73 | 96 | response = requests.get(service_desc_link, timeout=timeout) |
74 | 97 | if not response.status_code < 400: |
|
0 commit comments