"""
Wrap all common calls to the divera api as defined by
https://api.divera247.com/?urls.primaryName=api%2Fv2%2Falarm
https://api.divera247.com/?urls.primaryName=api%2Fv2%2Fevent
https://api.divera247.com/?urls.primaryName=api%2Fv2%2Fnews
"""
import abc
from divera.api import endpointwrappers as epw
[docs]
class GetAll(epw.FunctionalEndPoint, abc.ABC):
method = 'GET'
[docs]
@staticmethod
def process_result(
result,
):
return list((result['data']['items'] or {}).values())
def __call__(
self,
base_url: str,
access_token: str,
*args, **kwargs
):
args_ = (
base_url.rstrip('/') + self.path + f'?accesskey={access_token}',
)
kwargs_ = {}
return args_, kwargs_
[docs]
class Get(GetAll, epw.ObjectRelated, abc.ABC):
def __init__(
self,
object_id: [int, str],
):
self.object_id = object_id
@property
def path(self):
return f'{super().path}/{self.object_id}'
[docs]
@staticmethod
def process_result(
result,
):
return result['data']
[docs]
class Confirm(epw.FunctionalEndPoint, epw.ObjectRelated, abc.ABC):
method = 'POST'
@property
def path(self) -> str:
return f'{super().path}/{self.object_id}'
def __init__(
self,
type_: str,
object_id: str,
response_id: str,
response_text: str = None,
):
self.object_id = object_id
self.response_id = response_id
self.response_text = response_text
self.type_ = type_
def __call__(
self,
base_url: str,
access_token: str,
*args, **kwargs
):
args_ = [
base_url.rstrip('/') + self.path + f'?accesskey={access_token}',
]
kwargs_ = {
'json': {
self.type_: {
"participation": self.response_id,
"custom_answer": self.response_text,
},
},
}
return args_, kwargs_
[docs]
class Read(Get, abc.ABC):
method = 'POST'
[docs]
@staticmethod
def process_result(
result,
):
return result