Applying requested changes
parent
af83a190db
commit
1ad957142a
|
@ -26,40 +26,43 @@ class AccountDeviceEndpoint(SeleneEndpoint):
|
|||
|
||||
def post(self, account_id):
|
||||
payload = json.loads(self.request.data)
|
||||
device = AddDevice(payload)
|
||||
device.validate()
|
||||
add_device = AddDevice(payload)
|
||||
add_device.validate()
|
||||
code = self.request.args['code']
|
||||
# Checking if there's one pairing session for the pairing code
|
||||
pairing_json = self.cache.get('pairing.code:{}'.format(code))
|
||||
if pairing_json:
|
||||
device_id = self._finish_pairing(account_id, code, device, pairing_json)
|
||||
device_id = self._finish_pairing(account_id, code, add_device, pairing_json)
|
||||
response = device_id, HTTPStatus.OK
|
||||
else:
|
||||
response = '', HTTPStatus.NO_CONTENT
|
||||
return response
|
||||
|
||||
def _finish_pairing(self, account_id, code, device, pairing_json):
|
||||
def _finish_pairing(self, account_id, code, add_device, pairing_json):
|
||||
pairing = json.loads(pairing_json)
|
||||
# Removing the pairing code from the cache
|
||||
self.cache.delete('pairing.code:{}'.format(code))
|
||||
# Finishing the pairing process
|
||||
device_id = self._pair(
|
||||
account_id,
|
||||
str(device.name),
|
||||
str(device.wake_word_id),
|
||||
str(device.text_to_speech_id),
|
||||
add_device,
|
||||
pairing
|
||||
)
|
||||
return device_id
|
||||
|
||||
def _pair(self, account_id: str, name: str, wake_word_id: str, text_to_speech_id: str, pairing: dict):
|
||||
def _pair(self, account_id: str, add_device: AddDevice, pairing: dict):
|
||||
"""Creates a device and associate it to a pairing session"""
|
||||
with get_db_connection(self.config['DB_CONNECTION_POOL']) as db:
|
||||
result = DeviceRepository(db).add_device(account_id, name, wake_word_id, text_to_speech_id)
|
||||
pairing['uuid'] = result['id']
|
||||
self.cache.set_with_expiration(
|
||||
'pairing.token:{}'.format(pairing['token']),
|
||||
json.dumps(pairing),
|
||||
self.device_pairing_time
|
||||
result = DeviceRepository(db).add_device(
|
||||
account_id,
|
||||
str(add_device.name),
|
||||
str(add_device.wake_word_id),
|
||||
str(add_device.text_to_speech_id)
|
||||
)
|
||||
return pairing['uuid']
|
||||
pairing['uuid'] = result['id']
|
||||
self.cache.set_with_expiration(
|
||||
'pairing.token:{}'.format(pairing['token']),
|
||||
json.dumps(pairing),
|
||||
self.device_pairing_time
|
||||
)
|
||||
return pairing['uuid']
|
||||
|
|
|
@ -37,12 +37,7 @@ class DeviceActivateEndpoint(SeleneEndpoint):
|
|||
pairing = self._get_pairing_session(device_activate)
|
||||
if pairing:
|
||||
device_id = pairing['uuid']
|
||||
self._activate(
|
||||
device_id,
|
||||
str(device_activate.platform),
|
||||
str(device_activate.enclosure_version),
|
||||
str(device_activate.core_version)
|
||||
)
|
||||
self._activate(device_id, device_activate)
|
||||
response = self._generate_login(device_id), HTTPStatus.OK
|
||||
else:
|
||||
response = '', HTTPStatus.NO_CONTENT
|
||||
|
@ -61,22 +56,27 @@ class DeviceActivateEndpoint(SeleneEndpoint):
|
|||
self.cache.delete(self._token_key(token))
|
||||
return pairing
|
||||
|
||||
def _activate(self, device_id: str, platform: str, enclosure_version: str, core_version: str):
|
||||
def _activate(self, device_id: str, device_activate: DeviceActivate):
|
||||
"""Updates a device in the database with the core version, platform and enclosure_version fields"""
|
||||
with get_db_connection(self.config['DB_CONNECTION_POOL']) as db:
|
||||
DeviceRepository(db).update_device(device_id, platform, enclosure_version, core_version)
|
||||
DeviceRepository(db).update_device(
|
||||
device_id,
|
||||
str(device_activate.platform),
|
||||
str(device_activate.enclosure_version),
|
||||
str(device_activate.core_version)
|
||||
)
|
||||
|
||||
def _generate_login(self, device_id: str):
|
||||
self.sha512.update(bytes(str(uuid.uuid4()), 'utf-8'))
|
||||
access = self.sha512.hexdigest()
|
||||
self.sha512.update(bytes(str(uuid.uuid4()), 'utf-8'))
|
||||
refresh = self.sha512.hexdigest()
|
||||
login = {
|
||||
'uuid': device_id,
|
||||
'accessToken': access,
|
||||
'refreshToken': refresh,
|
||||
'expiration': self.ONE_DAY
|
||||
}
|
||||
login = dict(
|
||||
uuid=device_id,
|
||||
accessToken= access,
|
||||
refreshToken=refresh,
|
||||
expiration=self.ONE_DAY
|
||||
)
|
||||
login_json = json.dumps(login)
|
||||
# Storing device access token for one:
|
||||
self.cache.set_with_expiration('device.session:{uuid}'.format(uuid=device_id), login_json, self.ONE_DAY)
|
||||
|
|
|
@ -44,9 +44,10 @@ class DeviceCodeEndpoint(SeleneEndpoint):
|
|||
if self.cache.set_if_not_exists_with_expiration(self._code_key(code),
|
||||
value=pairing_json,
|
||||
expiration=self.device_pairing_time):
|
||||
return pairing
|
||||
response = pairing
|
||||
else:
|
||||
return self._create(state)
|
||||
response = self._create(state)
|
||||
return response
|
||||
|
||||
@staticmethod
|
||||
def _code_key(code):
|
||||
|
|
|
@ -4,8 +4,14 @@ from datetime import date, timedelta
|
|||
from behave import fixture, use_fixture
|
||||
|
||||
from public_api.api import public
|
||||
from selene.data.account import Account, AccountRepository, AccountAgreement, PRIVACY_POLICY, TERMS_OF_USE, Agreement, \
|
||||
AgreementRepository
|
||||
from selene.data.account import (
|
||||
Account,
|
||||
AccountRepository,
|
||||
AccountAgreement,
|
||||
PRIVACY_POLICY,
|
||||
TERMS_OF_USE,
|
||||
Agreement,
|
||||
AgreementRepository)
|
||||
from selene.data.device import DeviceRepository
|
||||
from selene.data.device.entity.text_to_speech import TextToSpeech
|
||||
from selene.data.device.entity.wake_word import WakeWord
|
||||
|
|
|
@ -20,10 +20,11 @@ def add_device(context):
|
|||
'wake_word_id': context.wake_word_id,
|
||||
'text_to_speech_id': context.text_to_speech_id
|
||||
}
|
||||
response = context.client.post('/api/account/{account_id}/device?code={code}'
|
||||
.format(account_id=context.account.id, code=context.pairing['code']),
|
||||
data=json.dumps(device),
|
||||
content_type='application_json')
|
||||
response = context.client.post(
|
||||
'/api/account/{account_id}/device?code={code}'
|
||||
.format(account_id=context.account.id, code=context.pairing['code']),
|
||||
data=json.dumps(device),
|
||||
content_type='application_json')
|
||||
context.device_id = response.data.decode('utf-8')
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue