From b1a4a9c22bb8918e1a06bc2449b922a987178d4e Mon Sep 17 00:00:00 2001 From: Jonathan D'Orleans Date: Wed, 7 Sep 2016 20:50:45 -0400 Subject: [PATCH] Issues 351 - Making Identity static and unique --- mycroft/api/__init__.py | 7 +++-- mycroft/client/speech/recognizer_wrapper.py | 10 ++++-- mycroft/identity/__init__.py | 34 ++++++++++++--------- mycroft/skills/cerberus_config/__init__.py | 4 +-- mycroft/skills/pairing/__init__.py | 6 +++- mycroft/skills/weather/__init__.py | 2 +- mycroft/skills/wolfram_alpha/__init__.py | 19 +++++------- 7 files changed, 45 insertions(+), 37 deletions(-) diff --git a/mycroft/api/__init__.py b/mycroft/api/__init__.py index c36ce0d382..ca44ae2979 100644 --- a/mycroft/api/__init__.py +++ b/mycroft/api/__init__.py @@ -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} }) diff --git a/mycroft/client/speech/recognizer_wrapper.py b/mycroft/client/speech/recognizer_wrapper.py index 16b09f160e..f6c3be0931 100644 --- a/mycroft/client/speech/recognizer_wrapper.py +++ b/mycroft/client/speech/recognizer_wrapper.py @@ -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 diff --git a/mycroft/identity/__init__.py b/mycroft/identity/__init__.py index 1dfd1e723d..1b44a3f34b 100644 --- a/mycroft/identity/__init__.py +++ b/mycroft/identity/__init__.py @@ -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 diff --git a/mycroft/skills/cerberus_config/__init__.py b/mycroft/skills/cerberus_config/__init__.py index d1de71c121..7939840805 100644 --- a/mycroft/skills/cerberus_config/__init__.py +++ b/mycroft/skills/cerberus_config/__init__.py @@ -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() diff --git a/mycroft/skills/pairing/__init__.py b/mycroft/skills/pairing/__init__.py index 18c04d7cae..d2ae602a63 100644 --- a/mycroft/skills/pairing/__init__.py +++ b/mycroft/skills/pairing/__init__.py @@ -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: diff --git a/mycroft/skills/weather/__init__.py b/mycroft/skills/weather/__init__.py index 90e4113d04..adca13b25a 100644 --- a/mycroft/skills/weather/__init__.py +++ b/mycroft/skills/weather/__init__.py @@ -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__)) diff --git a/mycroft/skills/wolfram_alpha/__init__.py b/mycroft/skills/wolfram_alpha/__init__.py index 1555679f9f..fa10572885 100644 --- a/mycroft/skills/wolfram_alpha/__init__.py +++ b/mycroft/skills/wolfram_alpha/__init__.py @@ -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))