-
Notifications
You must be signed in to change notification settings - Fork 2
/
__main__.py
85 lines (73 loc) · 3.22 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from http.server import BaseHTTPRequestHandler
from urllib import parse
from socStyrelsenRequest.socRequest import SocRequest
from scbSource.scbRequest import scbRequest
from scbSource.scbFortuneReq import scbFortuneRequest
class GetHandler(BaseHTTPRequestHandler):
def do_GET(self):
# Socialstyrelsens diagnos data
if(self.path.startswith('/socDiagnoses')):
self.send_response(200)
self.send_header('Content-Type',
'application/json; charset=utf-8')
self.end_headers()
self.wfile.write(SocRequest().getDiagnoseIdAndName().encode('utf-8'))
# Socialstyrelsens data
elif(self.path.startswith('/soc')):
diagnose = self.path.strip('/soc')
jsonData = None
try:
jsonData = SocRequest().getDiagnoseJson(diagnose).encode('utf-8')
self.send_response(200)
self.send_header('Content-Type',
'application/json; charset=utf-8')
self.end_headers()
self.wfile.write(jsonData)
except RuntimeError as e:
self.send_response(404)
self.send_header('Content-Type',
'text/plain; charset=utf-8')
self.end_headers()
self.wfile.write("(404) Invalid Soc Diagnose Code".encode('utf-8'))
# Statistiska Central Byrans data
elif(self.path.startswith('/scb')):
dataset = self.path.strip('/scb')
jsonData = None
try:
if (dataset.startswith('Unemployment')):
jsonData = scbRequest().getJSON().encode('utf-8')
elif (dataset.startswith('Fortune')):
jsonData = scbFortuneRequest().getJSON().encode('utf-8')
else:
raise RuntimeError('Invalid scb request.')
self.send_response(200)
self.send_header('Content-Type',
'application/json; charset=utf-8')
self.end_headers()
self.wfile.write(jsonData)
except RuntimeError as e:
self.send_response(404)
self.send_header('Content-Type',
'text/plain; charset=utf-8')
self.end_headers()
self.wfile.write("(404) Invalid SCB Request.".encode('utf-8'))
# HTML
elif(self.path.startswith('/')):
if self.path == '/':
self.path += 'index.html'
self.send_response(200)
self.send_header('Content-Type',
'text/html; charset=utf-8')
self.end_headers()
self.wfile.write(open("./app" + self.path).read().encode('utf-8'))
else:
self.send_response(404)
self.send_header('Content-Type',
'text/plain; charset=utf-8')
self.end_headers()
self.wfile.write("(404) Invalid API Path".encode('utf-8'))
if __name__ == '__main__':
from http.server import HTTPServer
server = HTTPServer(('localhost', 8080), GetHandler)
print('Starting server, use <Ctrl-C> to stop')
server.serve_forever()