Skip to content

Commit d1ad19f

Browse files
committed
Added network connect/disconnect API.
1 parent b83c488 commit d1ad19f

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

ircb/web/app.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
from ircb.config import settings
1111
from ircb.web.user import SigninView, SignoutView
12-
from ircb.web.network import NetworkListView, NetworkView
12+
from ircb.web.network import (NetworkListView, NetworkView,
13+
NetworkConnectionView)
1314

1415
logging.config.dictConfig(settings.LOGGING_CONF)
1516

@@ -39,6 +40,9 @@ def init(loop):
3940
name='networks')
4041
app.router.add_route('*', '/api/v1/network/{id}', NetworkView,
4142
name='network')
43+
app.router.add_route('PUT', '/api/v1/network/{id}/{action}',
44+
NetworkConnectionView,
45+
name='network_connection')
4246
srv = yield from loop.create_server(
4347
app.make_handler(), '0.0.0.0', 10001)
4448
return srv

ircb/web/network.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,30 @@ def put(self):
9696
raise web.HTTPNotFound()
9797
resp = yield from self._create_or_update(data, username, networks[0])
9898
return resp
99+
100+
101+
class NetworkConnectionView(View):
102+
store = NetworkStore
103+
104+
@auth_required
105+
@asyncio.coroutine
106+
def put(self):
107+
network_id = self.request.match_info['id']
108+
action = self.request.match_info['action']
109+
if action not in ('connect', 'disconnect'):
110+
raise web.HTTPNotFound()
111+
username = yield from get_auth(self.request)
112+
user = yield from UserStore.get(
113+
dict(query=('username', username)))
114+
networks = yield from NetworkStore.get(
115+
dict(query={'user_id': user.id, 'id': network_id})
116+
)
117+
if not networks:
118+
raise web.HTTPNotFound()
119+
network = networks[0]
120+
network = yield from NetworkStore.update(
121+
dict(filter=('id', network.id),
122+
update={'status': '0' if action == 'connect' else '2'})
123+
)
124+
return web.Response(body=self.serialize(network).encode(),
125+
content_type='application/json')

0 commit comments

Comments
 (0)