Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

215 mark quantumleap subscription as deprecated #263

Merged
merged 14 commits into from
Apr 17, 2024
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ __pycache__
*.log
.project
.pydevproject

*.yml
# include issue branch workflows
!.github/workflows/issue-tracker.yml
Expand Down
22 changes: 17 additions & 5 deletions examples/ngsi_v2/e10_ngsi_v2_quantumleap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import time
import random

from filip.models.ngsi_v2.subscriptions import Message
from filip.models.ngsi_v2.subscriptions import Message, Subscription
from filip.models.ngsi_v2.context import ContextEntity
from filip.models.base import FiwareHeader
from filip.clients.ngsi_v2 import ContextBrokerClient, QuantumLeapClient
Expand Down Expand Up @@ -77,10 +77,22 @@
# create a subscription
# Note: that the IP must be the ones that orion and quantumleap can access,
# e.g. service name or static IP, localhost will not work here.
ql_client.post_subscription(entity_id=hall_entity.id,
cb_url="http://orion:1026",
ql_url="http://quantumleap:8668",
throttling=0)

subscription:Subscription = Subscription.model_validate({
"subject": {
"entities": [
{
"id": hall_entity.id
}
]
},
"notification": { #Notify QL automatically
"http": {
"url": "http://quantumleap:8668/v2/notify"
}
}
})
subscription_id = cb_client.post_subscription(subscription=subscription)

# Get all subscriptions
subscription_list = cb_client.get_subscription_list()
Expand Down
1 change: 0 additions & 1 deletion filip/clients/mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import warnings
from datetime import datetime
from typing import Any, Callable, Dict, List, Tuple, Union

import paho.mqtt.client as mqtt

from filip.clients.mqtt.encoder import BaseEncoder, Json, Ultralight
Expand Down
68 changes: 26 additions & 42 deletions filip/clients/ngsi_v2/quantumleap.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,48 +184,9 @@ def post_subscription(self,
used as a
time index.
"""
headers = self.headers.copy()
params = {}
url = urljoin(self.base_url, 'v2/subscribe')
validate_http_url(cb_url)
cb_url = urljoin(str(cb_url), 'v2')
params.update({'orionUrl': cb_url.encode('utf-8')})

validate_http_url(ql_url)
ql_url = urljoin(str(ql_url), 'v2')
params.update({'quantumleapUrl': ql_url.encode('utf-8')})

if entity_type:
params.update({'entityType': entity_type})
if entity_id:
params.update({'entityId': entity_id})
if id_pattern:
params.update({'idPattern': id_pattern})
if attributes:
params.update({'attributes': attributes})
if observed_attributes:
params.update({'observedAttributes': observed_attributes})
if notified_attributes:
params.update({'notifiedAttributes': notified_attributes})
if throttling or throttling == 0:
if throttling >= 0 and type(throttling) == int:
params.update({'throttling': throttling})
else:
raise TypeError("Throttling must be a positive integer or zero")
if time_index_attribute:
params.update({'timeIndexAttribute': time_index_attribute})

try:
res = self.post(url=url, headers=headers, params=params)
if res.ok:
msg = "Subscription created successfully!"
self.logger.info(msg)

res.raise_for_status()
except requests.exceptions.RequestException as err:
msg = "Could not create subscription."
self.log_error(err=err, msg=msg)
raise
raise DeprecationWarning("Subscription endpoint of Quantumleap API is "
"deprecated, use the ORION subscription endpoint "
"instead")

def delete_entity(self, entity_id: str,
entity_type: Optional[str] = None) -> str:
Expand Down Expand Up @@ -302,6 +263,7 @@ def __query_builder(self,
url,
*,
entity_id: str = None,
id_pattern: str = None,
options: str = None,
entity_type: str = None,
aggr_method: Union[str, AggrMethod] = None,
Expand Down Expand Up @@ -344,10 +306,15 @@ def __query_builder(self,
coords:
attrs:
aggr_scope:
id_pattern (str): The pattern covering the entity ids for which
to subscribe. The pattern follow regular expressions (POSIX
Extendede) e.g. ".*", "Room.*". Detail information:
https://en.wikibooks.org/wiki/Regular_Expressions/POSIX-Extended_Regular_Expressions

Returns:
Dict
"""
assert (id_pattern is None or entity_id is None), "Cannot have both id and idPattern as parameter."
params = {}
headers = self.headers.copy()
max_records_per_request = 10000
Expand Down Expand Up @@ -387,6 +354,8 @@ def __query_builder(self,
params.update({'aggr_scope': aggr_scope.value})
if entity_id:
params.update({'id': entity_id})
if id_pattern:
params.update({'idPattern': id_pattern})

# This loop will chop large requests into smaller junks.
# The individual functions will then merge the final response models
Expand Down Expand Up @@ -431,6 +400,7 @@ def __query_builder(self,
# v2/entities
def get_entities(self, *,
entity_type: str = None,
id_pattern: str = None,
from_date: str = None,
to_date: str = None,
limit: int = 10000,
Expand All @@ -446,6 +416,10 @@ def get_entities(self, *,
when required. If used to resolve ambiguity for the given
entityId, make sure the given entityId exists for this
entityType.
id_pattern (str): The pattern covering the entity ids for which
to subscribe. The pattern follow regular expressions (POSIX
Extendede) e.g. ".*", "Room.*". Detail information:
https://en.wikibooks.org/wiki/Regular_Expressions/POSIX-Extended_Regular_Expressions
from_date (str): The starting date and time (inclusive) from which
the context information is queried. Must be in ISO8601 format
(e.g., 2018-01-05T15:44:34)
Expand All @@ -461,11 +435,13 @@ def get_entities(self, *,
"""
url = urljoin(self.base_url, 'v2/entities')
res = self.__query_builder(url=url,
id_pattern=id_pattern,
entity_type=entity_type,
from_date=from_date,
to_date=to_date,
limit=limit,
offset=offset)

ta = TypeAdapter(List[TimeSeriesHeader])
return ta.validate_python(res[0])

Expand Down Expand Up @@ -821,6 +797,7 @@ def get_entity_by_type(self,
*,
attrs: str = None,
entity_id: str = None,
id_pattern: str = None,
aggr_method: Union[str, AggrMethod] = None,
aggr_period: Union[str, AggrPeriod] = None,
from_date: str = None,
Expand All @@ -842,6 +819,7 @@ def get_entity_by_type(self,
url = urljoin(self.base_url, f'v2/types/{entity_type}')
res_q = self.__query_builder(url=url,
entity_id=entity_id,
id_pattern=id_pattern,
attrs=attrs,
options=options,
aggr_method=aggr_method,
Expand Down Expand Up @@ -874,6 +852,7 @@ def get_entity_values_by_type(self,
*,
attrs: str = None,
entity_id: str = None,
id_pattern: str = None,
aggr_method: Union[str, AggrMethod] = None,
aggr_period: Union[str, AggrPeriod] = None,
from_date: str = None,
Expand All @@ -896,6 +875,7 @@ def get_entity_values_by_type(self,
url = urljoin(self.base_url, f'v2/types/{entity_type}/value')
res_q = self.__query_builder(url=url,
entity_id=entity_id,
id_pattern=id_pattern,
attrs=attrs,
options=options,
entity_type=entity_type,
Expand Down Expand Up @@ -928,6 +908,7 @@ def get_entity_attr_by_type(self,
attr_name: str,
*,
entity_id: str = None,
id_pattern: str = None,
aggr_method: Union[str, AggrMethod] = None,
aggr_period: Union[str, AggrPeriod] = None,
from_date: str = None,
Expand Down Expand Up @@ -984,6 +965,7 @@ def get_entity_attr_by_type(self,
f'/{attr_name}')
res_q = self.__query_builder(url=url,
entity_id=entity_id,
id_pattern=id_pattern,
options=options,
entity_type=entity_type,
aggr_method=aggr_method,
Expand Down Expand Up @@ -1029,6 +1011,7 @@ def get_entity_attr_values_by_type(self,
attr_name: str,
*,
entity_id: str = None,
id_pattern: str = None,
aggr_method: Union[
str, AggrMethod] = None,
aggr_period: Union[
Expand Down Expand Up @@ -1079,6 +1062,7 @@ def get_entity_attr_values_by_type(self,
f'{attr_name}/value')
res_q = self.__query_builder(url=url,
entity_id=entity_id,
id_pattern=id_pattern,
options=options,
entity_type=entity_type,
aggr_method=aggr_method,
Expand Down
2 changes: 1 addition & 1 deletion filip/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class FiwareHeader(BaseModel):
)
service_path: str = Field(
alias="fiware-servicepath",
default="",
default="/",
description="Fiware service path",
max_length=51,
)
Expand Down
Loading