Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions hololinked/server/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
PropertyHandler,
EventHandler,
BaseHandler,
RWMultiplePropertiesHandler,
StopHandler,
ThingDescriptionHandler,
RPCHandler,
Expand Down Expand Up @@ -475,8 +476,6 @@ def add_action(
if not issubklass(handler, BaseHandler):
raise TypeError(f"handler should be subclass of BaseHandler, given type {type(handler)}")
http_methods = _comply_http_method(http_method)
if len(http_methods) != 1:
raise ValueError("http_method should be a single HTTP method")
if isinstance(action, Action):
action = action.to_affordance() # type: ActionAffordance
kwargs["resource"] = action
Expand Down Expand Up @@ -741,6 +740,7 @@ def add_interaction_affordances(
path = f"/{pep8_to_dashed_name(event.name)}"
self.server.add_event(URL_path=path, event=event, handler=self.server.event_handler)

# thing description handler
get_thing_model_action = next((action for action in actions if action.name == "get_thing_model"), None)
get_thing_description_action = deepcopy(get_thing_model_action)
get_thing_description_action.override_defaults(name="get_thing_description")
Expand All @@ -750,6 +750,21 @@ def add_interaction_affordances(
http_method=("GET",),
handler=ThingDescriptionHandler,
)

# RW multiple properties handler
read_properties = Thing._get_properties.to_affordance(Thing)
write_properties = Thing._set_properties.to_affordance(Thing)
read_properties.override_defaults(thing_id=get_thing_model_action.thing_id)
write_properties.override_defaults(thing_id=get_thing_model_action.thing_id)
self.server.add_action(
URL_path=f"/{thing_id}/properties" if thing_id else "/properties",
action=read_properties,
http_method=("GET", "PUT", "PATCH"),
handler=RWMultiplePropertiesHandler,
read_properties_resource=read_properties,
write_properties_resource=write_properties,
)

self.server.logger.debug(
f"added thing description action for thing id {thing_id if thing_id else 'unknown'} at path "
+ f"{f'/{thing_id}/resources/wot-td' if thing_id else '/resources/wot-td'}"
Expand Down
66 changes: 65 additions & 1 deletion hololinked/server/http/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,34 @@ async def delete(self) -> None:
self.finish()


class RWMultiplePropertiesHandler(ActionHandler):
def initialize(self, resource, owner_inst=None, metadata=None, **kwargs) -> None:
self.read_properties_resource = kwargs.pop("read_properties_resource", None)
self.write_properties_resource = kwargs.pop("write_properties_resource", None)
return super().initialize(resource, owner_inst, metadata)

async def get(self) -> None:
if self.is_method_allowed("GET"):
self.resource = self.read_properties_resource
if self.message_id is not None:
await self.handle_no_block_response()
else:
await self.handle_through_thing(Operations.invokeaction)
self.finish()

async def put(self) -> None:
if self.is_method_allowed("PUT"):
self.resource = self.write_properties_resource
await self.handle_through_thing(Operations.invokeaction)
self.finish()

async def patch(self) -> None:
if self.is_method_allowed("PATCH"):
self.resource = self.write_properties_resource
await self.handle_through_thing(Operations.invokeaction)
self.finish()


class EventHandler(BaseHandler):
"""handles events emitted by `Thing` and tunnels them as HTTP SSE"""

Expand Down Expand Up @@ -722,11 +750,11 @@ def generate_td(
) -> dict[str, JSONSerializable]:
TD = copy.deepcopy(TM)
# sanitize some things
TD["id"] = f"{self.server.router.get_basepath(authority=authority, use_localhost=use_localhost)}/{TD['id']}"

self.add_properties(TD, TM, authority=authority, use_localhost=use_localhost)
self.add_actions(TD, TM, authority=authority, use_localhost=use_localhost)
self.add_events(TD, TM, authority=authority, use_localhost=use_localhost)
self.add_top_level_forms(TD, authority=authority, use_localhost=use_localhost)

self.add_security_definitions(TD)
return TD
Expand Down Expand Up @@ -820,6 +848,42 @@ def add_events(
form.subprotocol = "sse"
TD["events"][name]["forms"].append(form.json())

def add_top_level_forms(self, TD: dict[str, JSONSerializable], authority: str, use_localhost: bool) -> None:
"""adds top level forms for reading and writing multiple properties"""

properties_end_point = f"{self.server.router.get_basepath(authority, use_localhost)}/{TD['id']}/properties"

if TD.get("forms", None) is None:
TD["forms"] = []

readallproperties = Form()
readallproperties.href = properties_end_point
readallproperties.op = "readallproperties"
readallproperties.htv_methodName = "GET"
readallproperties.contentType = "application/json"
TD["forms"].append(readallproperties.json())

writeallproperties = Form()
writeallproperties.href = properties_end_point
writeallproperties.op = "writeallproperties"
writeallproperties.htv_methodName = "PUT"
writeallproperties.contentType = "application/json"
TD["forms"].append(writeallproperties.json())

readmultipleproperties = Form()
readmultipleproperties.href = properties_end_point
readmultipleproperties.op = "readmultipleproperties"
readmultipleproperties.htv_methodName = "GET"
readmultipleproperties.contentType = "application/json"
TD["forms"].append(readmultipleproperties.json())

writemultipleproperties = Form()
writemultipleproperties.href = properties_end_point
writemultipleproperties.op = "writemultipleproperties"
writemultipleproperties.htv_methodName = "PATCH"
writemultipleproperties.contentType = "application/json"
TD["forms"].append(writemultipleproperties.json())

def add_security_definitions(self, TD: dict[str, JSONSerializable]) -> None:
from ...td.security_definitions import SecurityScheme

Expand Down
Loading