Skip to content

Commit 748ec0c

Browse files
committed
more cleanup
1 parent 5b4deb0 commit 748ec0c

File tree

4 files changed

+50
-32
lines changed

4 files changed

+50
-32
lines changed

example/resources/form.py

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
from reliqua.resources.base import Resource
8+
from reliqua.status_codes import HTTP
89

910

1011
class Contact(Resource):
@@ -34,3 +35,6 @@ def on_post(self, req, resp):
3435
p = req.params
3536
if p.get("subject"):
3637
resp.media = {"success": True}
38+
else:
39+
resp.status = HTTP("400")
40+
resp.media = {"error": "Subject is required"}

example/resources/media.py

+28-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88

99
from reliqua.resources.base import Resource
10+
from reliqua.status_codes import HTTP
1011

1112

1213
class Gzip(Resource):
@@ -16,19 +17,25 @@ class Gzip(Resource):
1617
"/gzip": {},
1718
}
1819

19-
def on_get(self, req, resp):
20+
def on_get(self, _req, resp):
2021
"""
2122
Send contact message.
2223
2324
:accepts json: Accepts JSON
24-
:response 200 binary: All good
25-
:return gzip: Return data
25+
:response 200 binary: All good
26+
:return gzip: Return data
2627
"""
27-
fh = open("/tmp/hello.txt.gz", "rb")
28-
resp.append_header("Content-Disposition", "attachment; filename=hello.txt.gz")
29-
resp.content_type = "application/gzip"
30-
resp.content_encoding = "gzip"
31-
resp.data = fh.read()
28+
try:
29+
with open("/tmp/hello.txt.gz", "rb") as fh:
30+
resp.append_header("Content-Disposition", "attachment; filename=hello.txt.gz")
31+
resp.content_type = "application/gzip"
32+
resp.content_encoding = "gzip"
33+
resp.data = fh.read()
34+
except FileNotFoundError:
35+
resp.status = HTTP("404")
36+
except Exception as e:
37+
resp.status = HTTP("500")
38+
resp.media = {"error": str(e)}
3239

3340

3441
class Binary(Resource):
@@ -38,16 +45,23 @@ class Binary(Resource):
3845
"/bin/{filename}": {},
3946
}
4047

41-
def on_get(self, req, resp, filename):
48+
def on_get(self, _req, resp, filename):
4249
"""
4350
Send contact message.
4451
4552
:param str filename: [in=path] Filename
46-
:response 200 binary: All good
53+
:response 200 binary: All good
4754
:return binary: Return data
4855
"""
4956
path = f"/tmp/{filename}"
50-
resp.stream = open(path, "rb")
51-
resp.content_type = "application/gzip"
52-
resp.content_encoding = "gzip"
53-
resp.content_length = os.path.getsize(path)
57+
try:
58+
with open(path, "rb") as fh:
59+
resp.stream = fh
60+
resp.content_type = "application/gzip"
61+
resp.content_encoding = "gzip"
62+
resp.content_length = os.path.getsize(path)
63+
except FileNotFoundError:
64+
resp.status = HTTP("404")
65+
except Exception as e:
66+
resp.status = HTTP("500")
67+
resp.media = {"error": str(e)}

example/resources/servers.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ class Server(Resource):
2626

2727
def on_get_by_id(self, _req, resp, id=None):
2828
"""
29-
Retrieve servers.
29+
Retrieve a server.
3030
31-
Retrieve a list of servers in the lab.
31+
Retrieve a server by its ID.
3232
3333
:param str id: [in=path, required] Server ID
3434
35-
:response 200: server was retrieved
36-
:response 400: invalid query parameter
35+
:response 200: Server was retrieved
36+
:response 404: Server not found
3737
3838
:return json:
3939
"""
4040
try:
41-
resp.media = servers[id]
42-
except IndexError:
41+
resp.media = servers[int(id)]
42+
except (IndexError, ValueError):
4343
resp.status = HTTP("404")
4444

4545

@@ -54,14 +54,14 @@ class Servers(Resource):
5454

5555
def on_get(self, req, resp):
5656
"""
57-
Retrieve a server.
57+
Retrieve servers.
5858
59-
Retrieve server information
59+
Retrieve a list of servers in the lab.
6060
6161
:param list labs: [in=query] The labs servers are located
6262
63-
:response 200: server was retrieved
64-
:response 400: invalid query parameter
63+
:response 200: Servers were retrieved
64+
:response 400: Invalid query parameter
6565
6666
:return json:
6767
"""
@@ -70,18 +70,18 @@ def on_get(self, req, resp):
7070

7171
def on_get_by_cpu(self, _req, resp, cpus=1):
7272
"""
73-
Retrieve a server by cpu.
73+
Retrieve a server by CPU.
7474
75-
Retrieve server information by cpu
75+
Retrieve server information by CPU.
7676
7777
:param int cpus: [in=path required] Number of CPUs for server
7878
79-
:response 200: server was retrieved
80-
:response 400: invalid query parameter
79+
:response 200: Server was retrieved
80+
:response 404: Server not found
8181
8282
:return json:
8383
"""
8484
try:
85-
resp.media = servers[cpus]
86-
except IndexError:
85+
resp.media = servers[int(cpus)]
86+
except (IndexError, ValueError):
8787
resp.status = HTTP("404")

example/resources/users.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def on_get_by_id(self, _req, resp, id=None):
6464
"""
6565
try:
6666
resp.media = users[int(id)]
67-
except IndexError:
67+
except (IndexError, ValueError):
6868
resp.status = HTTP("404")
6969

7070
def on_delete_by_id(self, _req, resp, id=None):
@@ -78,7 +78,7 @@ def on_delete_by_id(self, _req, resp, id=None):
7878
try:
7979
users.pop(int(id))
8080
resp.media = {"success": True}
81-
except IndexError:
81+
except (IndexError, ValueError):
8282
resp.status = HTTP("400")
8383

8484

0 commit comments

Comments
 (0)