Add device registry code · sdasgit/tutorials@c25a1eb · GitHub
Skip to content

Commit c25a1eb

Browse files
committed
Add device registry code
1 parent 59d1f6a commit c25a1eb

6 files changed

Lines changed: 219 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Device Registry Service
2+
3+
## Usage
4+
5+
All responses will have the form
6+
7+
```json
8+
{
9+
"data": "Mixed type holding the content of the response",
10+
"message": "Description of what happened"
11+
}
12+
```
13+
14+
Subsequent response definitions will only detail the expected value of the `data field`
15+
16+
### List all devices
17+
18+
**Definition**
19+
20+
`GET /devices`
21+
22+
**Response**
23+
24+
- `200 OK` on success
25+
26+
```json
27+
[
28+
{
29+
"identifier": "floor-lamp",
30+
"name": "Floor Lamp",
31+
"device_type": "switch",
32+
"controller_gateway": "192.1.68.0.2"
33+
},
34+
{
35+
"identifier": "samsung-tv",
36+
"name": "Living Room TV",
37+
"device_type": "tv",
38+
"controller_gateway": "192.168.0.9"
39+
}
40+
]
41+
```
42+
43+
### Registering a new device
44+
45+
**Definition**
46+
47+
`POST /devices`
48+
49+
**Arguments**
50+
51+
- `"identifier":string` a globally unique identifier for this device
52+
- `"name":string` a friendly name for this device
53+
- `"device_type":string` the type of the device as understood by the client
54+
- `"controller_gateway":string` the IP address of the device's controller
55+
56+
If a device with the given identifier already exists, the existing device will be overwritten.
57+
58+
**Response**
59+
60+
- `201 Created` on success
61+
62+
```json
63+
{
64+
"identifier": "floor-lamp",
65+
"name": "Floor Lamp",
66+
"device_type": "switch",
67+
"controller_gateway": "192.1.68.0.2"
68+
}
69+
```
70+
71+
## Lookup device details
72+
73+
`GET /device/<identifier>`
74+
75+
**Response**
76+
77+
- `404 Not Found` if the device does not exist
78+
- `200 OK` on success
79+
80+
```json
81+
{
82+
"identifier": "floor-lamp",
83+
"name": "Floor Lamp",
84+
"device_type": "switch",
85+
"controller_gateway": "192.1.68.0.2"
86+
}
87+
```
88+
89+
## Delete a device
90+
91+
**Definition**
92+
93+
`DELETE /devices/<identifier>`
94+
95+
**Response**
96+
97+
- `404 Not Found` if the device does not exist
98+
- `204 No Content` on success
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import markdown
2+
import os
3+
import shelve
4+
5+
# Import the framework
6+
from flask import Flask, g
7+
from flask_restful import Resource, Api, reqparse
8+
9+
# Create an instance of Flask
10+
app = Flask(__name__)
11+
12+
# Create the API
13+
api = Api(app)
14+
15+
def get_db():
16+
db = getattr(g, '_database', None)
17+
if db is None:
18+
db = g._database = shelve.open("devices.db")
19+
return db
20+
21+
@app.teardown_appcontext
22+
def teardown_db(exception):
23+
db = getattr(g, '_database', None)
24+
if db is not None:
25+
db.close()
26+
27+
@app.route("/")
28+
def index():
29+
"""Present some documentation"""
30+
31+
# Open the README file
32+
with open(os.path.dirname(app.root_path) + '/README.md', 'r') as markdown_file:
33+
34+
# Read the content of the file
35+
content = markdown_file.read()
36+
37+
# Convert to HTML
38+
return markdown.markdown(content)
39+
40+
41+
class DeviceList(Resource):
42+
def get(self):
43+
shelf = get_db()
44+
keys = list(shelf.keys())
45+
46+
devices = []
47+
48+
for key in keys:
49+
devices.append(shelf[key])
50+
51+
return {'message': 'Success', 'data': devices}, 200
52+
53+
def post(self):
54+
parser = reqparse.RequestParser()
55+
56+
parser.add_argument('identifier', required=True)
57+
parser.add_argument('name', required=True)
58+
parser.add_argument('device_type', required=True)
59+
parser.add_argument('controller_gateway', required=True)
60+
61+
# Parse the arguments into an object
62+
args = parser.parse_args()
63+
64+
shelf = get_db()
65+
shelf[args['identifier']] = args
66+
67+
return {'message': 'Device registered', 'data': args}, 201
68+
69+
70+
class Device(Resource):
71+
def get(self, identifier):
72+
shelf = get_db()
73+
74+
# If the key does not exist in the data store, return a 404 error.
75+
if not (identifier in shelf):
76+
return {'message': 'Device not found', 'data': {}}, 404
77+
78+
return {'message': 'Device found', 'data': shelf[identifier]}, 200
79+
80+
def delete(self, identifier):
81+
shelf = get_db()
82+
83+
# If the key does not exist in the data store, return a 404 error.
84+
if not (identifier in shelf):
85+
return {'message': 'Device not found', 'data': {}}, 404
86+
87+
del shelf[identifier]
88+
return '', 204
89+
90+
91+
api.add_resource(DeviceList, '/devices')
92+
api.add_resource(Device, '/device/<string:identifier>')
93+
94+
95+
96+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: '3.4'
2+
3+
services:
4+
device-registry:
5+
build: .
6+
volumes:
7+
- .:/usr/src/app
8+
ports:
9+
- 5000:80
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Flask==0.12.2
2+
flask-restful==0.3.6
3+
markdown==2.6.11
Lines changed: 3 additions & 0 deletions

0 commit comments

Comments
 (0)