Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.

Commit 48f763f

Browse files
authored
feat/sql_alchemy (#51)
1 parent 8aa8288 commit 48f763f

25 files changed

+1498
-893
lines changed

.github/workflows/build_tests.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,4 @@ jobs:
4747
python setup.py bdist_wheel
4848
- name: Install package
4949
run: |
50-
pip install .
51-
- uses: pypa/gh-action-pip-audit@v1.0.0
50+
pip install .

.github/workflows/unit_tests.yml

Lines changed: 0 additions & 66 deletions
This file was deleted.

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
FROM ubuntu:latest
22

33
RUN apt-get update && \
4-
apt-get install -y git python3 python3-dev python3-pip curl build-essential libffi-dev python3-numpy rustc
4+
apt-get install -y git python3 python3-dev python3-pip curl build-essential libffi-dev python3-numpy rustc flac libmysqlclient-dev
55

66
RUN pip3 install SpeechRecognition==3.8.1
77

88
COPY . /tmp/ovos-backend
9-
RUN pip3 install /tmp/ovos-backend
9+
RUN pip3 install /tmp/ovos-backend[mysql]
1010

1111
RUN pip3 install git+https://github.com/OpenVoiceOS/ovos-backend-manager
1212

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ This is NOT meant to provision third party devices, but rather to run on the myc
1010

1111
Documentation can be found at https://openvoiceos.github.io/community-docs/personal_backend
1212

13+
NOTES:
14+
- this backend moved to SQL databases on release 0.1.6a10, json databases from older version are not compatible
15+
- at the time of writing, backend manager does not yet work with this backend version
16+
1317

1418
## Install
1519

ovos_local_backend/backend/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313

1414
from flask import Flask
1515
from ovos_local_backend.configuration import CONFIGURATION
16+
from ovos_local_backend.database import connect_db
1617

1718
API_VERSION = CONFIGURATION["api_version"]
1819

1920

2021
def create_app():
2122
app = Flask(__name__)
2223

24+
app, db = connect_db(app)
25+
2326
from ovos_local_backend.utils import nice_json
2427
from ovos_local_backend.backend.decorators import noindex
2528
from ovos_local_backend.backend.auth import get_auth_routes
@@ -28,12 +31,15 @@ def create_app():
2831
from ovos_local_backend.backend.precise import get_precise_routes
2932
from ovos_local_backend.backend.external_apis import get_services_routes
3033
from ovos_local_backend.backend.admin import get_admin_routes
34+
from ovos_local_backend.backend.crud import get_database_crud
35+
3136
app = get_auth_routes(app)
3237
app = get_device_routes(app)
3338
app = get_stt_routes(app)
3439
app = get_precise_routes(app)
3540
app = get_services_routes(app)
3641
app = get_admin_routes(app)
42+
app = get_database_crud(app)
3743

3844
@app.route("/", methods=['GET'])
3945
@noindex

ovos_local_backend/backend/admin.py

Lines changed: 13 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@
1111
# limitations under the License.
1212
#
1313
import time
14+
15+
import base64
16+
from flask import request
1417
from ovos_local_backend.backend import API_VERSION
15-
from ovos_local_backend.utils import nice_json
16-
from ovos_local_backend.database.settings import DeviceDatabase
17-
from ovos_local_backend.configuration import CONFIGURATION
1818
from ovos_local_backend.backend.decorators import noindex, requires_admin
19-
from flask import request
19+
import ovos_local_backend.database as db
2020
from ovos_local_backend.utils import generate_code
21+
from ovos_local_backend.utils import nice_json
2122
from ovos_local_backend.utils.geolocate import get_request_location
2223

2324

25+
2426
def get_admin_routes(app):
2527
@app.route("/" + API_VERSION + "/admin/<uuid>/pair", methods=['GET'])
2628
@requires_admin
@@ -30,8 +32,7 @@ def pair_device(uuid):
3032
token = f"{code}:{uuid}"
3133
# add device to db
3234
location = get_request_location()
33-
with DeviceDatabase() as db:
34-
db.add_device(uuid, token, location=location)
35+
db.add_device(uuid, token, location=location)
3536

3637
device = {"uuid": uuid,
3738
"expires_at": time.time() + 99999999999999,
@@ -43,72 +44,21 @@ def pair_device(uuid):
4344
@requires_admin
4445
@noindex
4546
def set_device(uuid):
46-
data = request.json
47-
with DeviceDatabase() as db:
48-
device = db.get_device(uuid)
49-
if not device:
50-
return {"error": "unknown device"}
51-
if "name" in data:
52-
device.name = data["name"]
53-
if "opt_in" in data:
54-
device.opt_in = data["opt_in"]
55-
if "device_location" in data:
56-
device.device_location = data["device_location"]
57-
if "email" in data:
58-
device.email = data["email"]
59-
if "isolated_skills" in data:
60-
device.isolated_skills = data["isolated_skills"]
61-
if "lang" in data:
62-
device.lang = data["lang"]
63-
db.update_device(device)
64-
return nice_json(device.serialize())
47+
device_data = db.update_device(uuid, **request.json)
48+
return nice_json(device_data)
6549

6650
@app.route("/" + API_VERSION + "/admin/<uuid>/location", methods=['PUT'])
6751
@requires_admin
6852
@noindex
6953
def set_location(uuid):
70-
with DeviceDatabase() as db:
71-
device = db.get_device(uuid)
72-
if not device:
73-
return {"error": "unknown device"}
74-
device.location = request.json
75-
db.update_device(device)
76-
return nice_json(device.serialize())
54+
device_data = db.update_device(uuid, location=request.json)
55+
return nice_json(device_data)
7756

7857
@app.route("/" + API_VERSION + "/admin/<uuid>/prefs", methods=['PUT'])
7958
@requires_admin
8059
@noindex
8160
def set_prefs(uuid):
82-
data = request.json
83-
with DeviceDatabase() as db:
84-
device = db.get_device(uuid)
85-
if not device:
86-
return {"error": "unknown device"}
87-
if "time_format" in data:
88-
device.time_format = data["time_format"]
89-
if "date_format" in data:
90-
device.date_format = data["date_format"]
91-
if "system_unit" in data:
92-
device.system_unit = data["system_unit"]
93-
if "lang" in data:
94-
device.lang = data["lang"]
95-
if "tts_module" in data:
96-
device.default_tts = data["tts_module"]
97-
if "tts_config" in data:
98-
device.default_tts_cfg = data["tts_config"]
99-
elif data["tts_module"] in CONFIGURATION["tts_configs"]:
100-
device.default_tts_cfg = CONFIGURATION["tts_configs"][data["tts_module"]]
101-
else:
102-
device.default_tts_cfg = {}
103-
if "wake_word" in data:
104-
device.default_ww = data["wake_word"]
105-
if "ww_config" in data:
106-
device.default_ww_cfg = data["ww_config"]
107-
elif data["ww_module"] in CONFIGURATION["ww_configs"]:
108-
device.default_ww_cfg = CONFIGURATION["ww_configs"][data["ww_module"]]
109-
else:
110-
device.default_ww_cfg = {}
111-
db.update_device(device)
112-
return nice_json(device.serialize())
61+
device_data = db.update_device(uuid, **request.json)
62+
return nice_json(device_data)
11363

11464
return app

ovos_local_backend/backend/auth.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
from ovos_local_backend.backend import API_VERSION
2222
from ovos_local_backend.backend.decorators import noindex, requires_auth
23-
from ovos_backend_client.database import OAuthTokenDatabase, OAuthApplicationDatabase
23+
from ovos_local_backend.database import add_oauth_application, add_oauth_token, get_oauth_application, get_oauth_token
24+
2425
from ovos_local_backend.utils import nice_json
2526

2627
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
@@ -50,36 +51,40 @@ def oauth_url(oauth_id):
5051
""" send auth url to user to confirm authorization,
5152
once user opens it callback is triggered
5253
"""
54+
auth = request.headers.get('Authorization', '').replace("Bearer ", "")
55+
uid = auth.split(":")[-1]
56+
token_id = f"{uid}|{oauth_id}"
57+
5358
params = dict(request.args)
54-
params["callback_endpoint"] = request.base_url + f"/{API_VERSION}/auth/callback/{oauth_id}"
59+
params["callback_endpoint"] = request.base_url + f"/{API_VERSION}/auth/callback/{token_id}"
5560

5661
client = WebApplicationClient(params["client_id"])
5762
request_uri = client.prepare_request_uri(
5863
params["auth_endpoint"],
5964
redirect_uri=params["callback_endpoint"],
6065
scope=params["scope"],
6166
)
62-
with OAuthApplicationDatabase() as db:
63-
db.add_application(oauth_id,
64-
params["client_id"],
65-
params["client_secret"],
66-
params["auth_endpoint"],
67-
params["token_endpoint"],
68-
params["refresh_endpoint"],
69-
params["callback_endpoint"],
70-
params["scope"])
67+
68+
add_oauth_application(token_id=token_id,
69+
client_id=params["client_id"],
70+
client_secret=params["client_secret"],
71+
auth_endpoint=params["auth_endpoint"],
72+
token_endpoint=params["token_endpoint"],
73+
refresh_endpoint=params["refresh_endpoint"],
74+
callback_endpoint=params["callback_endpoint"],
75+
scope=params["scope"])
7176

7277
return request_uri, 200
7378

74-
@app.route(f"/{API_VERSION}/auth/callback/<oauth_id>", methods=['GET'])
79+
@app.route(f"/{API_VERSION}/auth/callback/<token_id>", methods=['GET'])
7580
@noindex
76-
def oauth_callback(oauth_id):
81+
def oauth_callback(token_id):
7782
""" user completed oauth, save token to db
7883
"""
7984
params = dict(request.args)
8085
code = params["code"]
8186

82-
data = OAuthApplicationDatabase()[oauth_id]
87+
data = get_oauth_application(token_id)
8388
client_id = data["client_id"]
8489
client_secret = data["client_secret"]
8590
token_endpoint = data["token_endpoint"]
@@ -99,17 +104,16 @@ def oauth_callback(oauth_id):
99104
auth=(client_id, client_secret),
100105
).json()
101106

102-
with OAuthTokenDatabase() as db:
103-
db.add_token(oauth_id, token_response)
104-
107+
add_oauth_token(token_id, token_response)
105108
return nice_json(params)
106109

107110
@app.route(f"/{API_VERSION}/device/<uuid>/token/<oauth_id>", methods=['GET'])
108111
@requires_auth
109112
@noindex
110113
def oauth_token(uuid, oauth_id):
111114
"""a device is requesting a token for a previously approved OAuth app"""
112-
data = OAuthTokenDatabase().get(oauth_id) or {}
115+
token_id = f"@{uuid}|{oauth_id}"
116+
data = get_oauth_token(token_id)
113117
return nice_json(data)
114118

115119
return app

0 commit comments

Comments
 (0)