This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
ws.py
119 lines (91 loc) · 2.87 KB
/
ws.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
# encoding=utf8
from pecan.rest import RestController
from wsme.types import Base, text, wsattr
import wsme
import wsmeext.pecan
class Author(Base):
id = int
firstname = text
books = wsattr(['Book'])
class Book(Base):
id = int
name = text
author = wsattr('Author')
class BookNotFound(Exception):
message = "Book with ID={id} Not Found"
code = 404
def __init__(self, id):
message = self.message.format(id=id)
super(BookNotFound, self).__init__(message)
class NonHttpException(Exception):
message = "Internal Exception for Book ID={id}"
code = 684
def __init__(self, id):
message = self.message.format(id=id)
super(NonHttpException, self).__init__(message)
class BooksController(RestController):
@wsmeext.pecan.wsexpose(Book, int, int)
def get(self, author_id, id):
print repr(author_id), repr(id)
book = Book(
name=u"Les Confessions d’un révolutionnaire pour servir à "
u"l’histoire de la révolution de février",
author=Author(lastname=u"Proudhon")
)
return book
@wsmeext.pecan.wsexpose(Book, int, int, body=Book)
def put(self, author_id, id, book=None):
print author_id, id
print book
book.id = id
book.author = Author(id=author_id)
return book
class Criterion(Base):
op = text
attrname = text
value = text
class AuthorsController(RestController):
books = BooksController()
@wsmeext.pecan.wsexpose([Author], [unicode], [Criterion])
def get_all(self, q=None, r=None):
if q:
return [
Author(id=i, firstname=value)
for i, value in enumerate(q)
]
if r:
return [
Author(id=i, firstname=c.value)
for i, c in enumerate(r)
]
return [
Author(id=1, firstname=u'FirstName')
]
@wsmeext.pecan.wsexpose(Author, int)
def get(self, id):
if id == 999:
raise wsme.exc.ClientSideError('Wrong ID')
if id == 998:
raise BookNotFound(id)
if id == 997:
raise NonHttpException(id)
if id == 911:
return wsme.api.Response(Author(),
status_code=401)
author = Author()
author.id = id
author.firstname = u"aname"
author.books = [
Book(
name=u"Les Confessions d’un révolutionnaire pour servir à "
u"l’histoire de la révolution de février",
)
]
return author
@wsmeext.pecan.wsexpose(Author, body=Author, status_code=201)
def post(self, author):
author.id = 10
return author
@wsmeext.pecan.wsexpose(None, int)
def delete(self, author_id):
print "Deleting", author_id