Skip to content

Commit

Permalink
feat(api): Add endpoints to handle API update ignores (#1693)
Browse files Browse the repository at this point in the history
Provides two endpoints, a getter and setter, to handle returning the currently ignored update
  • Loading branch information
Laura-Danielle authored Jun 14, 2018
1 parent aeb1785 commit 8c5eae9
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
44 changes: 44 additions & 0 deletions api-server-lib/ot2serverlib/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,54 @@
from aiohttp import web
from threading import Thread
import ot2serverlib
from ot2serverlib import ignore_update

log = logging.getLogger(__name__)


async def get_ignore_version(request):
"""
This handler returns a GET request of form application/json.
The return body will be formatted as:
{"version": version_ignored}
If no version has been previously ignored, the value will be null
"""
ignored_version = ignore_update._get_ignored_version()
res = {'version': ignored_version}
return web.json_response(res)


async def set_ignore_version(request):
"""
This handler expects a POST request of form application/json.
The request body should be formatted as:
{"version": version_ignored}
The POST will 400 in the following scenarios:
1. Sending an empty dict
2. Sending a dict with an empty string
"""
data = await request.json()
if 'version' in data.keys():
ignored_version = data.get('version')
log.debug('Set Ignore Version to {}'.format(ignored_version))
if ignored_version == '':
status = 400
res = {'version': None}
else:
ignore_update._set_ignored_version(ignored_version)
status = 200
res = {'version': ignored_version}
else:
status = 400
res = {'version': None}

return web.json_response(res, status=status)


async def update_api(request: web.Request) -> web.Response:
"""
This handler accepts a POST request with Content-Type: multipart/form-data
Expand Down
29 changes: 29 additions & 0 deletions api-server-lib/ot2serverlib/ignore_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import json
import os

PATH = os.path.abspath(os.path.dirname(__file__))
filepath = os.path.join(PATH, 'ignore.json')


def _set_ignored_version(version):
"""
Private helper function that writes the most updated
API version that was ignored by a user in the app
:param version: Most recent ignored API update
"""
data = {'version': version}
with open(filepath, 'w') as data_file:
json.dump(data, data_file)


def _get_ignored_version():
"""
:return: Most recently ignored API version
"""
if os.path.exists(filepath):
with open(filepath) as data_file:
data = json.load(data_file)
version = data.get('version')
else:
version = None
return version
4 changes: 4 additions & 0 deletions api/opentrons/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ def init(loop=None):
'/server/update', endpoints.update_api)
server.app.router.add_post(
'/server/update/firmware', endpoints.update_firmware)
server.app.router.add_get(
'/server/update/ignore', endpoints.get_ignore_version)
server.app.router.add_post(
'/server/update/ignore', endpoints.set_ignore_version)
server.app.router.add_post(
'/server/restart', endpoints.restart)
server.app.router.add_post(
Expand Down
31 changes: 31 additions & 0 deletions api/tests/opentrons/server/test_update_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from opentrons.server.main import init
from opentrons.server.endpoints import update
import ot2serverlib
from ot2serverlib import ignore_update


async def test_restart(virtual_smoothie_env, monkeypatch, loop, test_client):
Expand Down Expand Up @@ -82,3 +83,33 @@ async def test_feature_flags(
r2body = await r2.text()
expected = {flag_name: flag_value}
assert json.loads(r2body) == expected


async def test_ignore_updates(
virtual_smoothie_env, loop, test_client):
app = init(loop)
cli = await loop.create_task(test_client(app))

# Test no ignore file found
r0 = await cli.get('/server/update/ignore')
r0body = await r0.text()
assert json.loads(r0body) == {'version': None}

# Test that values are set correctly
ignore_name = "testy_ignore.json"
tmpdir = tempfile.mkdtemp("files")
ignore_update.filepath = os.path.join(tmpdir, ignore_name)

ignore = {'version': '3.1.3'}
r1 = await cli.post('server/update/ignore', json=ignore)
assert r1.status == 200

# Test that you cannot pass an empty version
ignore2 = {'version': ''}
r2 = await cli.post('server/update/ignore', json=ignore2)
assert r2.status == 400

# Test that version in the temporary directory is still '3.1.3'
r3 = await cli.get('/server/update/ignore')
r3body = await r3.text()
assert json.loads(r3body) == {'version': '3.1.3'}

0 comments on commit 8c5eae9

Please sign in to comment.