Passed
Push — master ( 3e6a93...b6cc82 )
by Alexander
01:56
created

src.things3_api.ThingsAPI.on_get()   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nop 4
dl 0
loc 11
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, getcwd
18
from wsgiref.simple_server import make_server
19
import falcon
0 ignored issues
show
introduced by
Unable to import 'falcon'
Loading history...
20
from things3 import Things3
21
22
PORT = 8088
23
APP = falcon.App()
24
PATH = getcwd() + '/resources/'
25
26
27
class ThingsGUI:  # pylint: disable=too-few-public-methods
28
    """Simple read-only Things KanbanView."""
29
30
    # pylint: disable=no-self-use,unused-argument
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:  # pylint: disable=too-few-public-methods
52
    """Simple read-only Things API."""
53
54
    things3 = Things3()
55
56
    # pylint: disable=no-self-use,unused-argument
57
    def on_get(self, req, resp, command):
58
        """Handles GET requests"""
59
60
        if command in self.things3.functions:
61
            func = self.things3.functions[command]
62
            resp.media = self.things3.convert_tasks_to_model(
63
                func(self.things3))
64
        else:
65
            resp.media = self.things3.convert_tasks_to_model(
66
                self.things3.get_not_implemented())
67
            resp.status = falcon.HTTP_404
68
69
70
def setup():
71
    """Create server."""
72
    APP.add_route('/api/{command}', ThingsAPI())
73
    APP.add_route('/{url}', ThingsGUI())
74
    httpd = make_server('', PORT, APP)
75
    print("Serving API at http://localhost:%d/api/{command}" % PORT)
76
    return httpd
77
78
79
def main():
80
    """"Main function."""
81
    print("Starting up...")
82
    httpd = setup()
83
84
    try:
85
        httpd.serve_forever()
86
    except KeyboardInterrupt:
87
        print("Shutting down...")
88
        httpd.server_close()
89
        sys.exit(0)
90
91
92
if __name__ == "__main__":
93
    main()
94