Simple-Bookmarking-Service/api/api.py

154 lines
5.1 KiB
Python
Raw Normal View History

2020-01-19 03:38:52 +00:00
#!/usr/bin/env python3
# Copyright 2020 - David Todd (c0de@c0defox.es)
# Licensed under the MIT License (https://opensource.org/licenses/MIT)
"""
This file contains the API for a simple bookmarking application
"""
import json
import datetime
from pprint import pprint
from uuid import UUID
from bottle import response, abort
2020-01-19 03:38:52 +00:00
from .db import Database
class API:
"""
This class contains various methods for a simple bookmarking application API
As of right now, these are mostly just to pass data through to the database.
Stuff should be sanitized here though
"""
def __init__(self):
"""
Setup some internal variables, such as various constants
and the database connection
"""
self.database = Database('bookmarks.db')
self.dt_fmt = "%H:%M:%S on %B %d %Y"
self.response_type = 'application/json'
self.format_date = datetime.datetime.strftime
2020-01-19 04:07:26 +00:00
def _get_bookmark_object(self, bookmark):
"""
Shortcut to return the `bookmark_object` that several methods return
"""
# Maybe I need to use `sqlite3.Cursor.description` or `sqlite3.Row.keys()`
return {
'uuid': bookmark[0].hex,
'uri': bookmark[1],
'title': bookmark[2],
'date_created': self.format_date(bookmark[3], self.dt_fmt),
'date_updated': self.format_date(bookmark[4], self.dt_fmt) if bookmark[4] != None else '',
}
2020-01-19 03:38:52 +00:00
def save_bookmark(self, title, uri):
"""
Save the bookmark with `title` and `uri`
Returns a JSON object containing
the UUID of the saved bookmark
"""
response.content_type = self.response_type
bookmark = self.database.save_bookmark(uri, title)
return json.dumps({
'uuid': bookmark.hex
})
def get_bookmark(self, bookmark_id):
"""
Get the bookmark with the provided `bookmark_id`
`bookmark_id` is of type string
Returns a JSON object containing
everything about the bookmark
"""
response.content_type = self.response_type
bookmark = self.database.get_bookmark(UUID(bookmark_id))
if bookmark == None:
return abort(404, "Provided bookmark doesn't exist or has been deleted")
2020-01-19 04:07:26 +00:00
return json.dumps(self._get_bookmark_object(bookmark))
2020-01-19 03:38:52 +00:00
def get_all_bookmarks(self):
"""
Gets all of the bookmarks that are currently saved
Returns a JSON object containing
everything about each bookmark
"""
response.content_type = self.response_type
bookmarks = self.database.get_all_bookmarks()
if len(bookmarks) == 0:
return abort(404, "There are no bookmarks saved")
2020-01-19 03:38:52 +00:00
bookmark_object = [
{
'uuid': bookmark[0].hex,
'uri': bookmark[1],
'title': bookmark[2],
'date_created': self.format_date(bookmark[3], self.dt_fmt),
2020-01-19 03:47:57 +00:00
'date_updated': self.format_date(bookmark[4], self.dt_fmt) if bookmark[4] != None else '',
2020-01-19 03:38:52 +00:00
} for bookmark in bookmarks
]
return json.dumps(bookmark_object)
def delete_bookmark(self, bookmark_id):
"""
2020-01-19 03:47:57 +00:00
Deletes the bookmark that is associated with `bookmark_id`
Returns a JSON object containing the bookmark's UUID,
and the status as to whether or not it was successfully deleted
2020-01-19 03:38:52 +00:00
"""
response.content_type = self.response_type
2020-01-19 03:47:57 +00:00
return json.dumps({
'uuid': bookmark_id,
'bookmark_deleted': self.database.delete_bookmark(UUID(bookmark_id)),
})
2020-01-19 04:07:26 +00:00
def update_bookmark_title(self, bookmark_id, title):
"""
Updates the bookmark that is associated
with `bookmark_id` with the provided `title`
Returns a JSON object containing
everything about the bookmark
"""
response.content_type = self.response_type
2020-01-19 04:12:34 +00:00
bookmark = self.database.update_bookmark_title(UUID(bookmark_id), title)
2020-01-19 04:07:26 +00:00
if bookmark == None:
return abort(404, "Provided bookmark doesn't exist or has been deleted")
return json.dumps(self._get_bookmark_object(bookmark))
def update_bookmark_uri(self, bookmark_id, uri):
"""
Updates the bookmark that is associated
with `bookmark_id` with the provided `uri`
This action will change the `bookmark_id`
Returns a JSON object containing
everything about the bookmark
"""
2020-01-19 04:33:02 +00:00
conflict_msg = "The new bookmark_id matches one already existing in the database." \
"Perhaps you want to delete this bookmark instead?"
2020-01-19 04:07:26 +00:00
response.content_type = self.response_type
2020-01-19 04:12:34 +00:00
bookmark = self.database.update_bookmark_uri(UUID(bookmark_id), uri)
2020-01-19 04:07:26 +00:00
if bookmark == None:
return abort(404, "Provided bookmark doesn't exist or has been deleted")
2020-01-19 04:33:02 +00:00
if not bookmark:
return abort(409, conflict_msg)
2020-01-19 04:07:26 +00:00
return json.dumps(self._get_bookmark_object(bookmark))