Skip to content

Commit

Permalink
feat: update discovery and put its overwriting as an option
Browse files Browse the repository at this point in the history
  • Loading branch information
Elodie Thiéblin committed Dec 7, 2023
1 parent ecbe034 commit b183da5
Show file tree
Hide file tree
Showing 21 changed files with 169 additions and 173 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,20 @@ The `config.toml` file can also be used to define FLask server configuration (c.

### Configuration variables

| Variable name | default value | description |
| ------------------------- | ----------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [TDD__]TD_REPO_URL | http://localhost:5000 | The URL to access the TDD API server |
| [TDD__]SPARQLENDPOINT_URL | http://localhost:3030/things | The SPARQL endpoint URL |
| [TDD__]CHECK_SCHEMA | False | Define if TDD API will check the TDs regarding to the JSON-Schema and SHACL shapes |
| [TDD__]MAX_TTL | None | Integer, maximum time-to-live (in seconds) that a TD will be kept on the server (unlimited if None) |
| [TDD__]MANDATE_TTL | False | Boolean value, if set to True, it will only upload TDs having a time-to-live (ttl) value. The server will send a 400 HTTP code if the TD does not contain one. |
| [TDD__]LIMIT_BATCH_TDS | 25 | Default limit of returned TDs by batch (used for pagination) |
| [TDD__]ENDPOINT_TYPE | None | Special configuration to workaround SPARQL endpoints which do not follow the SPARQL standard. Possible values: `GRAPHDB` or `VIRTUOSO` |
| [TDD__]TD_JSONSCHEMA | ./tdd/data/td-json-schema-validation.json | The path to the file containing JSON-Schema to validate the TDs |
| [TDD__]TD_ONTOLOGY | ./tdd/data/td.ttl | The path to the file containing the TD OWL Ontology (only used for SHACL validation) |
| [TDD__]TD_SHACL_VALIDATOR | ./tdd/data/td-validation.ttl | The path to the file containing the SHACL shapes (only used for SHACL validation) |
| [TDD__]PERIOD_CLEAR_EXPIRE_TD | 3600 | The number of seconds between each clearing of expired TDs (0 to disable clearing expired TD) |
| Variable name | default value | description |
| ----------------------------- | ----------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [TDD__]TD_REPO_URL | http://localhost:5000 | The URL to access the TDD API server |
| [TDD__]SPARQLENDPOINT_URL | http://localhost:3030/things | The SPARQL endpoint URL |
| [TDD__]CHECK_SCHEMA | False | Define if TDD API will check the TDs regarding to the JSON-Schema and SHACL shapes |
| [TDD__]MAX_TTL | None | Integer, maximum time-to-live (in seconds) that a TD will be kept on the server (unlimited if None) |
| [TDD__]MANDATE_TTL | False | Boolean value, if set to True, it will only upload TDs having a time-to-live (ttl) value. The server will send a 400 HTTP code if the TD does not contain one. |
| [TDD__]LIMIT_BATCH_TDS | 25 | Default limit of returned TDs by batch (used for pagination) |
| [TDD__]ENDPOINT_TYPE | None | Special configuration to workaround SPARQL endpoints which do not follow the SPARQL standard. Possible values: `GRAPHDB` or `VIRTUOSO` |
| [TDD__]TD_JSONSCHEMA | ./tdd/data/td-json-schema-validation.json | The path to the file containing JSON-Schema to validate the TDs |
| [TDD__]TD_ONTOLOGY | ./tdd/data/td.ttl | The path to the file containing the TD OWL Ontology (only used for SHACL validation) |
| [TDD__]TD_SHACL_VALIDATOR | ./tdd/data/td-validation.ttl | The path to the file containing the SHACL shapes (only used for SHACL validation) |
| [TDD__]PERIOD_CLEAR_EXPIRE_TD | 3600 | The number of seconds between each clearing of expired TDs (0 to disable clearing expired TD) |
| [TDD__]OVERWRITE_DISCOVERY | False | Use custom discovery context (for offline purposes) |

## Deploy to develop on the API

Expand Down
2 changes: 1 addition & 1 deletion tdd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def generate():
)

response = {
"@context": "https://w3c.github.io/wot-discovery/context/discovery-context.jsonld",
"@context": "https://www.w3.org/2022/wot/discovery",
"@type": "ThingCollection",
"total": number_total,
"members": all_tds,
Expand Down
2 changes: 2 additions & 0 deletions tdd/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"MAX_TTL": None,
"MANDATE_TTL": False,
"PERIOD_CLEAR_EXPIRE_TD": 3600,
"OVERWRITE_DISCOVERY": False,
}

CONFIG = ConfigurationSet(
Expand Down Expand Up @@ -71,3 +72,4 @@ def _cast_to_int(fieldname):
CONFIG["MANDATE_TTL"] = _cast_to_boolean("MANDATE_TTL")
CONFIG["ENDPOINT_TYPE"] = check_possible_endpoints()
CONFIG["PERIOD_CLEAR_EXPIRE_TD"] = _cast_to_int("PERIOD_CLEAR_EXPIRE_TD")
CONFIG["OVERWRITE_DISCOVERY"] = _cast_to_boolean("OVERWRITE_DISCOVERY")
3 changes: 3 additions & 0 deletions tdd/context.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
import json
from tdd.config import CONFIG

from tdd.utils import DEFAULT_THING_CONTEXT_URI, DEFAULT_DISCOVERY_CONTEXT_URI
from tdd.sparql import (
Expand Down Expand Up @@ -37,6 +38,8 @@ def overwrite_thing_context(ld_content):


def overwrite_discovery_context(ld_content):
if not CONFIG["OVERWRITE_DISCOVERY"]:
return
if "@context" not in ld_content:
return
if type(ld_content["@context"]) not in (tuple, list):
Expand Down
120 changes: 60 additions & 60 deletions tdd/data/fixed-discovery-ctx.json
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
{
"@context": {
"discovery": "https://www.w3.org/2021/wot/discovery#",
"ThingDirectory": {
"@id": "discovery:ThingDirectory",
"@type": "@id"
},
"ThingLink": {
"@id": "discovery:ThingLink",
"@type": "@id"
},
"ThingCollection": {
"@id": "discovery:ThingCollection",
"@type": "@id"
},
"next": {
"@id": "discovery:nextThingCollection",
"@type": "@id"
},
"total": {
"@id": "discovery:contains",
"@type": "xsd:integer"
},
"members": {
"@id": "discovery:hasCollectionMember",
"@type": "@id",
"@container": "@set"
},
"registration": {
"@id": "discovery:hasRegistrationInformation",
"@type": "@id",
"@context": {
"RegistrationInformation": {
"@id": "discovery:RegistrationInformation",
"@type": "@id"
},
"created": {
"@id": "discovery:dateCreated",
"@type": "xsd:dateTime"
},
"modified": {
"@id": "discovery:dateModified",
"@type": "xsd:dateTime"
},
"expires": {
"@id": "discovery:expires",
"@type": "xsd:dateTime"
},
"retrieved": {
"@id": "discovery:retrieved",
"@type": "xsd:dateTime"
},
"ttl": {
"@id": "discovery:ttl",
"@type": "xsd:unsignedInt"
}
}
}
}
}
{
"@context": {
"discovery": "https://www.w3.org/2022/wot/discovery-ontology#",
"ThingDirectory": {
"@id": "discovery:ThingDirectory",
"@type": "@id"
},
"ThingLink": {
"@id": "discovery:ThingLink",
"@type": "@id"
},
"ThingCollection": {
"@id": "discovery:ThingCollection",
"@type": "@id"
},
"next": {
"@id": "discovery:nextThingCollection",
"@type": "@id"
},
"total": {
"@id": "discovery:contains",
"@type": "xsd:integer"
},
"members": {
"@id": "discovery:hasCollectionMember",
"@type": "@id",
"@container": "@set"
},
"registration": {
"@id": "discovery:hasRegistrationInformation",
"@type": "@id",
"@context": {
"RegistrationInformation": {
"@id": "discovery:RegistrationInformation",
"@type": "@id"
},
"created": {
"@id": "discovery:dateCreated",
"@type": "xsd:dateTime"
},
"modified": {
"@id": "discovery:dateModified",
"@type": "xsd:dateTime"
},
"expires": {
"@id": "discovery:expires",
"@type": "xsd:dateTime"
},
"retrieved": {
"@id": "discovery:retrieved",
"@type": "xsd:dateTime"
},
"ttl": {
"@id": "discovery:ttl",
"@type": "xsd:unsignedInt"
}
}
}
}
}
2 changes: 1 addition & 1 deletion tdd/data/tdd-description.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"@context": [
"https://www.w3.org/2022/wot/td/v1.1",
"https://w3c.github.io/wot-discovery/context/discovery-context.jsonld"
"https://www.w3.org/2022/wot/discovery"
],
"@type": "ThingDirectory",
"title": "Thing Description Directory (TDD)",
Expand Down
2 changes: 1 addition & 1 deletion tdd/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def validate_ttl(ld_content, mandate_ttl):

def get_registration_dict(uri, rdf_graph):
registration_query = (
"PREFIX discovery: <https://www.w3.org/2021/wot/discovery#>"
"PREFIX discovery: <https://www.w3.org/2022/wot/discovery-ontology#>"
"SELECT DISTINCT ?created ?modified ?expires ?ttl "
"WHERE {"
f" <{uri}> discovery:hasRegistrationInformation ?reg."
Expand Down
6 changes: 3 additions & 3 deletions tdd/sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

GET_URI_BY_ONTOLOGY = """
PREFIX td: <https://www.w3.org/2019/wot/td#>
PREFIX disco: <https://www.w3.org/2021/wot/discovery#>
PREFIX disco: <https://www.w3.org/2022/wot/discovery-ontology#>
SELECT DISTINCT ?graph ?id WHERE {{
GRAPH <urn:tdd:metadata> {{
?graph <urn:tdd:expressedIn> <{ontology}>.
Expand All @@ -24,7 +24,7 @@
"""

GET_TD_CREATION_DATE = """
PREFIX disc: <https://www.w3.org/2021/wot/discovery#>
PREFIX disc: <https://www.w3.org/2022/wot/discovery-ontology#>
PREFIX td: <https://www.w3.org/2019/wot/td#>
SELECT ?created WHERE {{
GRAPH <td:{uri}> {{
Expand Down Expand Up @@ -89,7 +89,7 @@
GET_EXPIRED_TD_GRAPHS = """
PREFIX td: <https://www.w3.org/2019/wot/td#>
PREFIX schema: <http://schema.org/>
PREFIX discovery: <https://www.w3.org/2021/wot/discovery#>
PREFIX discovery: <https://www.w3.org/2022/wot/discovery-ontology#>
SELECT ?graph WHERE {
GRAPH ?graph {
?td a td:Thing.
Expand Down
6 changes: 3 additions & 3 deletions tdd/td.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
get_all_contexts,
overwrite_thing_context, # not possible to remove it yet,
# because of hasSecurityConfiguration framing
# overwrite_discovery_context,
overwrite_discovery_context,
)

from tdd.errors import (
Expand Down Expand Up @@ -98,7 +98,7 @@ def use_custom_context(ld_content):
overwrite_thing_context(ld_content)

# replace discovery context uri witht the fixed discovery context
# overwrite_discovery_context(ld_content)
overwrite_discovery_context(ld_content)

return ld_content

Expand Down Expand Up @@ -274,7 +274,7 @@ def frame_td_nt_content(td_id, nt_content, original_context):

# no need since the published context is up to date
overwrite_thing_context(frame)
# overwrite_discovery_context(frame)
overwrite_discovery_context(frame)
json_ld_compacted = frame_nt_content(td_id, nt_content, frame)

jsonld_response = json.loads(json_ld_compacted)
Expand Down
8 changes: 3 additions & 5 deletions tdd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

from tdd.config import CONFIG

TDD = Namespace("https://www.w3.org/2021/wot/discovery#")
TDD = Namespace("https://www.w3.org/2022/wot/discovery-ontology#")
TD = Namespace("https://www.w3.org/2019/wot/td#")

DEFAULT_THING_CONTEXT_URI = "https://www.w3.org/2022/wot/td/v1.1"
DEFAULT_DISCOVERY_CONTEXT_URI = (
"https://w3c.github.io/wot-discovery/context/discovery-context.jsonld"
)
DEFAULT_DISCOVERY_CONTEXT_URI = "https://www.w3.org/2022/wot/discovery"


def get_collection_etag():
Expand Down Expand Up @@ -39,7 +37,7 @@ def update_collection_etag():
TD_PREFIX = {
"td": "https://www.w3.org/2019/wot/td#",
"td-jsonschema": "https://www.w3.org/2019/wot/json-schema#",
"td-discovery": "https://www.w3.org/2021/wot/discovery#",
"td-discovery": "https://www.w3.org/2022/wot/discovery-ontology#",
"td-hypermedia": "https://www.w3.org/2019/wot/hypermedia#",
}

Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
tdd.CONFIG["LIMIT_BATCH_TDS"] = 15
tdd.CONFIG["CHECK_SCHEMA"] = True
tdd.CONFIG["PERIOD_CLEAR_EXPIRE_TD"] = 0
tdd.CONFIG["OVERWRITE_DISCOVERY"] = True


@pytest.fixture(autouse=True)
Expand Down
Loading

0 comments on commit b183da5

Please sign in to comment.