Passed
Push — master ( 23a88e...bcffd2 )
by Alexander
01:25
created

things3.things3_api.ThingsGUI.on_get()   B

Complexity

Conditions 8

Size

Total Lines 18
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 17
nop 4
dl 0
loc 18
rs 7.3333
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__ = "Apache License 2.0"
12
__version__ = "2.0.1"
13
__maintainer__ = "Alexander Willner"
14
__email__ = "[email protected]"
15
__status__ = "Development"
16
17
from os import sys, getcwd
18
import json
19
from flask import Flask
20
from flask import Response
21
from things3.things3 import Things3
22
23
PORT = 8088
24
APP = Flask(__name__)
25
PATH = getcwd() + '/resources/'
26
THINGS3 = Things3()
27
28
29
@APP.route('/<url>')
30
def on_get(url):
31
    """Handles other GET requests"""
32
    filename = PATH + url
33
    content_type = 'application/json'
34
    if filename.endswith('css'):
35
        content_type = 'text/css'
36
    if filename.endswith('html'):
37
        content_type = 'text/html'
38
    if filename.endswith('js'):
39
        content_type = 'text/javascript'
40
    if filename.endswith('png'):
41
        content_type = 'image/png'
42
    if filename.endswith('jpg'):
43
        content_type = 'image/jpeg'
44
    if filename.endswith('ico'):
45
        content_type = 'image/x-ico'
46
    with open(filename, 'rb') as source:
47
        data = source.read()
48
    return Response(response=data, content_type=content_type)
49
50
51
@APP.route('/api/<command>')
52
def api(command):
53
    """Return database as JSON strings."""
54
    if command in THINGS3.functions:
55
        func = THINGS3.functions[command]
56
        data = json.dumps(THINGS3.convert_tasks_to_model(func(THINGS3)))
57
        return Response(response=data, content_type='application/json')
58
59
    data = json.dumps(THINGS3.convert_tasks_to_model(
60
        THINGS3.get_not_implemented()))
61
    return Response(response=data, content_type='application/json',
62
                    status=404)
63
64
65
def main():
66
    """"Main function."""
67
    print("Starting up...")
68
    try:
69
        APP.run(port=PORT)
70
    except KeyboardInterrupt:
71
        print("Shutting down...")
72
        sys.exit(0)
73
74
75
if __name__ == "__main__":
76
    main()
77