|
1 | | -import bottle |
2 | | -from bottle import run, get, post, delete |
| 1 | +from hashlib import sha1 |
| 2 | +from bottle import run, get, post, delete, install, HTTPError, request |
| 3 | +from bottle_sqlite import SQLitePlugin |
| 4 | + |
| 5 | +from dbsetup import init_db |
| 6 | + |
| 7 | + |
| 8 | +DBNAME='test.db' |
| 9 | + |
| 10 | +init_db(DBNAME) |
| 11 | +install(SQLitePlugin(dbfile=DBNAME)) |
| 12 | + |
| 13 | +def to_dict(row): |
| 14 | + return dict(sha=row['sha'], |
| 15 | + url=row['url'], |
| 16 | + timestamp=row['timestamp'], |
| 17 | + # Convert integer to boolean |
| 18 | + deleted=(1 == row['deleted'])) |
| 19 | + |
3 | 20 |
|
4 | 21 | @get('/') |
5 | 22 | @get('/links') |
6 | | -def list_links(): |
| 23 | +def list_links(db): |
7 | 24 | '''Return a complete list of all links''' |
8 | | - return dict(links=[]) |
| 25 | + links = [] |
| 26 | + for row in db.execute('SELECT * from links'): |
| 27 | + links.append(to_dict(row)) |
| 28 | + return dict(links=links) |
9 | 29 |
|
10 | | -@get('/links/<id>') |
11 | | -def get_link(id): |
| 30 | +@get('/links/<sha>') |
| 31 | +def get_link(db, sha): |
12 | 32 | '''Returns a specific link''' |
13 | | - return dict(link={"sha":"1111111", |
14 | | - "url":"http://www.google.com", |
15 | | - "timestamp":"2013-09-19 08:22:19.000"}) |
| 33 | + row = db.execute('SELECT * from links WHERE sha IS ?', [sha]).fetchone() |
| 34 | + if row: |
| 35 | + return dict(link=to_dict(row)) |
| 36 | + |
| 37 | + return HTTPError(404, "No such item") |
16 | 38 |
|
17 | | -@delete('/links/<id>') |
18 | | -def delete_link(id): |
| 39 | +@delete('/links/<sha>') |
| 40 | +def delete_link(db, sha): |
19 | 41 | '''Deletes a specific link from the list. |
20 | 42 | On success, returns an empty response''' |
21 | | - return {} |
| 43 | + db.execute('UPDATE links SET deleted = 1, timestamp = CURRENT_TIMESTAMP \ |
| 44 | + WHERE sha IS ? AND userid is ?', [sha, userid]) |
| 45 | + |
| 46 | + if db.total_changes > 0: |
| 47 | + return {} |
| 48 | + |
| 49 | + return HTTPError(404, "No such item") |
22 | 50 |
|
23 | 51 | @post('/links') |
24 | | -def add_link(): |
| 52 | +def add_link(db): |
25 | 53 | '''Adds a link to the list. |
26 | 54 | On success, returns the entry created.''' |
27 | | - return dict(link={"sha":"1111111", |
28 | | - "url":"http://www.google.com", |
29 | | - "timestamp":"2013-09-19 08:22:19.000"}) |
| 55 | + # Only accept json data |
| 56 | + if request.content_type != 'application/json': |
| 57 | + return HTTPError(415, "Only json is accepted") |
| 58 | + # Check required fields |
| 59 | + if 'url' not in request.json: |
| 60 | + return HTTPError(400, "Must specify a url") |
| 61 | + |
| 62 | + # Sha is optional, generate if not present |
| 63 | + if 'sha' not in request.json: |
| 64 | + request.json['sha'] = sha1(request.json['url']).hexdigest() |
| 65 | + |
| 66 | + args = [request.json['url'], |
| 67 | + request.json['sha']] |
| 68 | + if 'timestamp' in request.json: |
| 69 | + args.append(request.json['timestamp']) |
| 70 | + stmt = 'INSERT INTO links (url, sha, timestamp) VALUES(?, ?, ?)' |
| 71 | + else: |
| 72 | + stmt = 'INSERT INTO links (url, sha) VALUES(?, ?)' |
| 73 | + |
| 74 | + db.execute(stmt, args) |
| 75 | + |
| 76 | + return get_link(db, request.json['sha']) |
| 77 | + |
30 | 78 |
|
31 | 79 | if __name__ == '__main__': |
32 | | - bottle.debug(True) |
33 | | - run(port=5500) |
| 80 | + # Restart server automatically when this file changes |
| 81 | + run(host='0.0.0.0', port=5500, reloader=True, debug=True) |
0 commit comments