added device endpoint to account api and altered repository to serve it data

pull/66/head
Chris Veilleux 2019-03-05 15:20:31 -06:00
parent e3d42d3aca
commit ac7f1cb593
6 changed files with 118 additions and 5 deletions

View File

@ -4,9 +4,12 @@ from flask import Flask
from selene.api import get_base_config, selene_api, SeleneResponse
from selene.api.endpoints import AccountEndpoint, AgreementsEndpoint
from selene.util.log import configure_logger
from .endpoints.account_preferences import AccountPreferencesEndpoint
from .endpoints.device import DeviceEndpoint
from .endpoints.device_count import DeviceCountEndpoint
from .endpoints.skills import SkillsEndpoint
from .endpoints.skill_settings import SkillSettingsEndpoint
from .endpoints.wake_word_endpoint import WakeWordEndpoint
_log = configure_logger('account_api')
@ -48,3 +51,10 @@ acct.add_url_rule(
view_func=device_count_endpoint,
methods=['GET']
)
device_endpoint = DeviceEndpoint.as_view('device_endpoint')
acct.add_url_rule(
'/api/devices',
view_func=device_endpoint,
methods=['GET']
)

View File

@ -0,0 +1,50 @@
from dataclasses import asdict
from http import HTTPStatus
from selene.api import SeleneEndpoint
from selene.data.device import DeviceRepository
from selene.util.db import get_db_connection
class DeviceEndpoint(SeleneEndpoint):
def get(self):
self._authenticate()
with get_db_connection(self.config['DB_CONNECTION_POOL']) as db:
device_repository = DeviceRepository(db)
devices = device_repository.get_devices_by_account_id(
self.account.id
)
response_data = []
for device in devices:
wake_word = dict(
id=device.wake_word.id,
name=device.wake_word.wake_word
)
voice = dict(
id=device.text_to_speech.id,
name=device.text_to_speech.display_name
)
location = dict(
id=device.geography.id,
name=device.geography.country
)
placement = dict(
id=None,
name=device.placement
)
response_data.append(
dict(
core_version=device.core_version,
enclosure_version=device.enclosure_version,
id=device.id,
location=location,
name=device.name,
placement=placement,
platform=device.platform,
voice=voice,
wake_word=wake_word,
)
)
return response_data, HTTPStatus.OK

View File

@ -1,6 +1,7 @@
from dataclasses import dataclass
from datetime import datetime
from .geography import Geography
from .text_to_speech import TextToSpeech
from .wake_word import WakeWord
@ -15,5 +16,6 @@ class Device(object):
core_version: str
wake_word: WakeWord
text_to_speech: TextToSpeech
geography: Geography = None
placement: str = None
last_contact_ts: datetime = None

View File

@ -0,0 +1,9 @@
from dataclasses import dataclass
@dataclass
class Geography(object):
country: str
postal_code: str
time_zone: str
id: str = None

View File

@ -3,6 +3,7 @@ from typing import List
from selene.util.db import DatabaseRequest, get_sql_from_file, Cursor
from ..entity.device import Device
from ..entity.geography import Geography
from ..entity.text_to_speech import TextToSpeech
from ..entity.wake_word import WakeWord
@ -34,12 +35,22 @@ class DeviceRepository(object):
:param account_id: uuid
:return: List of User's devices
"""
query = DatabaseRequest(
sql=get_sql_from_file(path.join(SQL_DIR, 'get_devices_by_account_id.sql')),
db_request = DatabaseRequest(
sql=get_sql_from_file(
path.join(SQL_DIR, 'get_devices_by_account_id.sql')
),
args=dict(account_id=account_id)
)
sql_results = self.cursor.select_all(query)
return [Device(**result) for result in sql_results]
db_results = self.cursor.select_all(db_request)
devices = []
for row in db_results:
row['wake_word'] = WakeWord(**row['wake_word'])
row['text_to_speech'] = TextToSpeech(**row['text_to_speech'])
row['geography'] = Geography(**row['geography'])
devices.append(Device(**row))
return devices
def get_account_device_count(self, account_id):
query = DatabaseRequest(

View File

@ -1 +1,32 @@
SELECT * FROM device.device WHERE account_id = %(account_id)s
SELECT
d.id,
d.name,
d.platform,
d.enclosure_version,
d.core_version,
d.placement,
d.last_contact_ts,
json_build_object(
'wake_word', ww.wake_word,
'engine', ww.engine,
'id', ww.id
) AS wake_word,
json_build_object(
'setting_name', tts.setting_name,
'display_name', tts.display_name,
'engine', tts.engine,
'id', tts.id
) AS text_to_speech,
json_build_object(
'id', l.id,
'country', l.country,
'postal_code', l.postal_code,
'time_zone', l.time_zone
) AS geography
FROM
device.device d
INNER JOIN device.wake_word ww ON d.wake_word_id = ww.id
INNER JOIN device.text_to_speech tts ON d.text_to_speech_id = tts.id
LEFT JOIN device.location l ON d.location_id = l.id
WHERE
d.account_id = %(account_id)s