-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_api_server.py
130 lines (100 loc) · 3.47 KB
/
rest_api_server.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from bottle import Bottle, request, response, run, template, static_file
from pprint import pprint
import time
import algebra
import os
import sqlite3
import random
# file server
secret = 'CS218 is a Masters class - isntit'
app = Bottle()
@app.route('/')
def welcome():
pprint(dict(request.headers))
response.set_header('Vary', 'Accept')
if 'text/html' in request.headers.get('Accept', '*/*'):
response.content_type = 'text/html'
return '<h1>Welcome to CS 218!!!</h1>'
response.content_type = 'text/plain'
return 'hello'
@app.route('/now', method='GET')
def time_server():
response.content_type = 'text/plain'
response.set_header('Cache-Control', 'max-age=3')
return time.ctime()
@app.route('/joke', method='GET')
def joke_server():
response.content_type = 'text/plain'
# generate a random number between 1 and 163
# NOTE: DONT HARDCODE numbers in code!!!
r = random.randint(1, 163)
connection = sqlite3.connect("jokes.db")
cursor = connection.cursor()
cursor.execute("SELECT * from Jokes WHERE id = ?", (r,))
(i, joke) = cursor.fetchone()
connection.close()
return dict(i=i, joke=joke, service=request.path)
@app.route('/upper/<word>', method='GET')
def upper_case_service(word):
response.content_type = 'text/plain'
return word.upper()
@app.route('/prime/<number>', method='GET')
def is_prime(number):
number = int(number)
for i in range(2,number):
if number % i == 0:
response.content_type = 'text/plain'
return f'{number} is NOT prime'
response.content_type = 'text/plain'
return f'{number} is Prime!!!'
@app.route('/area/circle', method='GET')
def circle_area_service():
#last_visit = request.get_cookie('last-visit', 'unknown')
last_visit = request.get_cookie('last-visit', 'unknown', secret=secret)
print(f'Last visit {last_visit}')
response.set_header('Vary', 'Accept')
#response.set_cookie('last-visit', time.ctime())
response.set_cookie('last-visit', time.ctime(), secret=secret)
#pprint(dict(request.query))
try:
radius = float(request.query.get('radius', '0.0'))
except ValueError as e:
return e.args[0]
area = algebra.area_circle(radius)
if 'text/html' in request.headers.get('Accept', '*/*'):
response.content_type = 'text/html'
return f'<p>The area of the circle is <em>{area}</em> </p>'
return dict(radius=radius, area=area, service=request.path)
#return 'Test %r ' % area
file_template = '''\
<h1> List of files in <em> Farm Animals </em> directory </h1>
<hr>
<ol>
% for file in files:
<li> <a href= "files/{{file}}"> {{file}} </a></li>
% end
</ol>
'''
@app.route('/files', method='GET')
def show_files():
response.set_header('Vary', 'Accept')
try:
os.chdir('/home/app/')
except:
print('/home/app directory not present')
files = os.listdir('./farm_animals')
print(files)
if 'text/html' not in request.headers.get('Accept', '*/*'):
return dict(files=files)
return template(file_template, files=files)
@app.route('/files/<filename>', method='GET')
def serve_one_file(filename):
try:
os.chdir('/home/app/')
except:
print('/home/app directory not present')
return static_file(filename, './farm_animals')
if __name__ == '__main__':
run(app, host='0.0.0.0', port='8088')
#if __name__ == '__main__':
# run(host='localhost', port='8080')