Passed
Push — master ( d52a5c...eb2b83 )
by Alexander
01:44
created

src.things3_api.setup()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
"""Simple read-only Things 3 Web Serivce."""
5
6
from __future__ import print_function
7
8
__author__ = "Alexander Willner"
9
__copyright__ = "Copyright 2020 Alexander Willner"
10
__credits__ = ["Alexander Willner"]
11
__license__ = "MIT"
12
__version__ = "2.0.0"
13
__maintainer__ = "Alexander Willner"
14
__email__ = "[email protected]"
15
__status__ = "Development"
16
17
from os import sys
18
from os.path import dirname, realpath
19
from wsgiref.simple_server import make_server
20
import falcon
21
from things3 import Things3
22
23
PORT = 8088
24
APP = falcon.App()
25
PATH = dirname(realpath(__file__)) + '/../resources/'
26
27
28
class ThingsGUI:
29
    """Simple read-only Things KanbanView."""
30
31
    def on_get(self, req, resp, url):
32
        """Handles GET requests"""
33
        filename = PATH + url
34
        resp.status = falcon.HTTP_200
35
        if filename.endswith('css'):
36
            resp.content_type = 'text/css'
37
        if filename.endswith('html'):
38
            resp.content_type = falcon.MEDIA_HTML
39
        if filename.endswith('js'):
40
            resp.content_type = falcon.MEDIA_JS
41
        if filename.endswith('png'):
42
            resp.content_type = falcon.MEDIA_PNG
43
        if filename.endswith('jpg'):
44
            resp.content_type = falcon.MEDIA_JPEG
45
        if filename.endswith('ico'):
46
            resp.content_type = 'image/x-ico'
47
        with open(filename, 'rb') as source:
48
            resp.data = source.read()
49
50
51
class ThingsAPI:
52
    """Simple read-only Things API."""
53
54
    things3 = Things3()
55
56
    def on_get(self, req, resp, command):
57
        """Handles GET requests"""
58
        if command == "inbox":
59
            resp.media = self.things3.convert_tasks_to_model(
60
                self.things3.get_inbox())
61
        elif command == "today":
62
            resp.media = self.things3.convert_tasks_to_model(
63
                self.things3.get_today())
64
        elif command == "next":
65
            resp.media = self.things3.convert_tasks_to_model(
66
                self.things3.get_anytime())
67
        elif command == "backlog":
68
            resp.media = self.things3.convert_tasks_to_model(
69
                self.things3.get_someday())
70
        elif command == "upcoming":
71
            resp.media = self.things3.convert_tasks_to_model(
72
                self.things3.get_upcoming())
73
        elif command == "waiting":
74
            resp.media = self.things3.convert_tasks_to_model(
75
                self.things3.get_waiting())
76
        elif command == "mit":
77
            resp.media = self.things3.convert_tasks_to_model(
78
                self.things3.get_mit())
79
        else:
80
            resp.media = self.things3.convert_tasks_to_model(
81
                self.things3.get_not_implemented())
82
            resp.status = falcon.HTTP_404
83
84
85
def setup():
86
    APP.add_route('/api/{command}', ThingsAPI())
87
    APP.add_route('/{url}', ThingsGUI())
88
    HTTPD = make_server('', PORT, APP)
89
    print("Serving API at http://localhost:%d/api/{command}" % PORT)
90
    return HTTPD
91
92
93
def main():
94
    print("Starting up...")
95
    HTTPD = setup()
96
97
    try:
98
        HTTPD.serve_forever()
99
    except KeyboardInterrupt:
100
        print("Shutting down...")
101
        HTTPD.server_close()
102
        sys.exit(0)
103
104
105
if __name__ == "__main__":
106
    main()
107