Issues 351 - Making Identity static and unique

pull/420/head
Jonathan D'Orleans 2016-09-07 20:50:45 -04:00
parent 5fd8342868
commit b1a4a9c22b
7 changed files with 45 additions and 37 deletions

View File

@ -14,14 +14,14 @@ class Api(object):
config_server = config.get("server")
self.url = config_server.get("url")
self.version = config_server.get("version")
self.identity = IdentityManager().get()
self.identity = IdentityManager.get()
def request(self, params):
method = params.get("method", "GET")
headers = self.build_headers(params)
body = self.build_body(params)
url = self.build_url(params)
response = requests.request(method, url, headers=headers, data=body)
response = requests.request(method, url, headers=headers, json=body)
return self.get_response(response)
@staticmethod
@ -54,7 +54,7 @@ class Api(object):
def build_body(self, params):
body = params.get("body")
if body and params["headers"]["Content-Type"] == "application/json":
for k, v in body:
for k, v in body.iteritems():
if v == "":
body[k] = None
params["body"] = body
@ -77,6 +77,7 @@ class DeviceApi(Api):
def activate(self, state, token):
return self.request({
"method": "POST",
"path": "/activate",
"body": {"state": state, "token": token}
})

View File

@ -71,15 +71,19 @@ class IBMRecognizerWrapper(object):
audio, username, password, language=language, show_all=show_all)
class CerberusGoogleProxy(object):
class MycroftRecognizer(object):
def __init__(self, _):
self.version = get_version()
def transcribe(
self, audio, language="en-US", show_all=False, metrics=None):
# FIXME - Refactor
raise CerberusAccessDenied()
timer = Stopwatch()
timer.start()
identity = IdentityManager().get()
identity = IdentityManager.get()
headers = {'Authorization': 'Bearer ' + identity.token}
url = ConfigurationManager.get().get("server").get("url")
response = requests.post(url +
@ -128,7 +132,7 @@ class CerberusGoogleProxy(object):
RECOGNIZER_IMPLS = {
'mycroft': CerberusGoogleProxy,
'mycroft': MycroftRecognizer,
'google': GoogleRecognizerWrapper,
'wit': WitRecognizerWrapper,
'ibm': IBMRecognizerWrapper

View File

@ -28,21 +28,25 @@ class DeviceIdentity(object):
class IdentityManager(object):
FILE = 'identity.json'
__identity = None
def __init__(self):
self.file_system = FileSystemAccess('identity')
self.identity = DeviceIdentity()
self.load()
@staticmethod
def load():
try:
with FileSystemAccess('identity').open('identity.json', 'r') as f:
IdentityManager.__identity = DeviceIdentity(**json.load(f))
except:
IdentityManager.__identity = DeviceIdentity()
def load(self):
with self.file_system.open(self.FILE, 'r') as f:
self.identity = DeviceIdentity(**json.load(f))
@staticmethod
def save(uuid, token):
IdentityManager.__identity.uuid = uuid
IdentityManager.__identity.token = token
with FileSystemAccess('identity').open('identity.json', 'w') as f:
json.dump(IdentityManager.__identity.__dict__, f)
def save(self, identity):
self.identity = identity
with self.file_system.open(self.FILE, 'w') as f:
json.dump(self.identity, f)
def get(self):
return self.identity
@staticmethod
def get():
if not IdentityManager.__identity:
IdentityManager.load()
return IdentityManager.__identity

View File

@ -36,8 +36,8 @@ class CerberusConfigSkill(MycroftSkill):
self.register_intent(intent, self.handle_update_intent)
def handle_update_intent(self, message):
identity = IdentityManager().get()
if identity:
identity = IdentityManager.get()
if identity.token:
self.speak_dialog("not.paired")
else:
ConfigurationManager.load_remote()

View File

@ -21,6 +21,7 @@ from adapt.intent import IntentBuilder
from os.path import dirname
from mycroft.api import DeviceApi
from mycroft.identity import IdentityManager
from mycroft.skills.core import MycroftSkill
@ -54,8 +55,10 @@ class PairingSkill(MycroftSkill):
def activate(self):
try:
self.api.activate(self.state, self.data.get("token"))
token = self.data.get("token")
uuid = self.api.activate(self.state, token)
self.speak_dialog("pairing.paired")
IdentityManager.save(uuid.get("uuid"), token)
except:
self.data["expiration"] -= self.delay
@ -64,6 +67,7 @@ class PairingSkill(MycroftSkill):
self.handle_pairing()
else:
self.activator = Timer(self.delay, self.activate)
self.activator.start()
def is_paired(self):
try:

View File

@ -50,7 +50,7 @@ class WeatherSkill(MycroftSkill):
@property
def owm(self):
return OWM(API_key=self.config.get('api_key', ''),
identity=IdentityManager().get())
identity=IdentityManager.get())
def initialize(self):
self.load_data_files(dirname(__file__))

View File

@ -82,18 +82,13 @@ class CerberusWolframAlphaClient(object):
"""
Query Wolfram|Alpha with query using the v2.0 API
"""
response = {}
identity = IdentityManager().get()
if identity:
bearer_token = 'Bearer %s:%s' % \
(identity.device_id, identity.token)
query = urllib.parse.urlencode(dict(input=query))
url = 'https://cerberus.mycroft.ai/wolframalpha/v2/query?' + query
headers = {'Authorization': bearer_token}
response = requests.get(url, headers=headers)
if response.status_code == 401:
raise CerberusAccessDenied()
identity = IdentityManager.get()
query = urllib.parse.urlencode(dict(input=query))
url = 'https://cerberus.mycroft.ai/wolframalpha/v2/query?' + query
headers = {'Authorization': 'Bearer ' + identity.token}
response = requests.get(url, headers=headers)
if response.status_code == 401:
raise CerberusAccessDenied()
return wolframalpha.Result(StringIO(response.content))