Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
corepost
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
nexlab
corepost
Commits
6051d011
Commit
6051d011
authored
Oct 11, 2011
by
Jacek Furmankiewicz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed code to run again, with routing totally separated from core Resource
parent
65689030
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
17 deletions
+24
-17
routing.py
corepost/routing.py
+11
-12
web.py
corepost/web.py
+13
-5
No files found.
corepost/routing.py
View file @
6051d011
...
...
@@ -151,7 +151,6 @@ class RequestRouter:
'''Obsolete'''
raise
RuntimeError
(
"Do not @app.route() any more, as of 0.0.6 API has been re-designed around class methods, see docs and examples"
)
@
defer
.
inlineCallbacks
def
getResponse
(
self
,
request
):
"""Finds the appropriate router and dispatches the request to the registered function. Returns the appropriate Response object"""
# see if already cached
...
...
@@ -211,7 +210,7 @@ class RequestRouter:
if
isinstance
(
val
,
defer
.
Deferred
):
# add callback to finish the request
val
.
addCallback
(
self
.
__finishDeferred
,
request
)
yield
val
.
callback
(
request
)
return
val
else
:
#special logic for POST to return 201 (created)
if
request
.
method
==
Http
.
POST
:
...
...
@@ -221,26 +220,26 @@ class RequestRouter:
else
:
request
.
setResponseCode
(
201
)
defer
.
returnValue
(
self
.
__renderResponse
(
request
,
val
)
)
return
self
.
__generateResponse
(
request
,
val
,
request
.
code
)
except
exceptions
.
TypeError
as
ex
:
defer
.
returnValue
(
self
.
__createErrorResponse
(
request
,
400
,
"
%
s"
%
ex
)
)
return
self
.
__createErrorResponse
(
request
,
400
,
"
%
s"
%
ex
)
except
Exception
as
ex
:
defer
.
returnValue
(
self
.
__createErrorResponse
(
request
,
500
,
"Unexpected server error:
%
s
\n
%
s"
%
(
type
(
ex
),
ex
)
))
return
self
.
__createErrorResponse
(
request
,
500
,
"Unexpected server error:
%
s
\n
%
s"
%
(
type
(
ex
),
ex
))
else
:
defer
.
returnValue
(
self
.
__createErrorResponse
(
request
,
404
,
"URL '
%
s' not found
\n
"
%
request
.
path
)
)
return
self
.
__createErrorResponse
(
request
,
404
,
"URL '
%
s' not found
\n
"
%
request
.
path
)
except
Exception
as
ex
:
defer
.
returnValue
(
self
.
__createErrorResponse
(
request
,
500
,
"Internal server error:
%
s"
%
ex
)
)
return
self
.
__createErrorResponse
(
request
,
500
,
"Internal server error:
%
s"
%
ex
)
def
__
render
Response
(
self
,
request
,
response
,
code
=
200
):
def
__
generate
Response
(
self
,
request
,
response
,
code
=
200
):
"""
Takes care of automatically rendering the response and converting it to appropriate format (text,XML,JSON,YAML)
depending on what the caller can accept. Returns Response
"""
if
isinstance
(
response
,
str
):
return
Response
(
code
,
response
)
return
Response
(
code
,
response
,{
HttpHeader
.
CONTENT_TYPE
:
MediaType
.
TEXT_PLAIN
}
)
elif
isinstance
(
response
,
Response
):
return
response
else
:
...
...
@@ -274,12 +273,12 @@ class RequestRouter:
"""Finishes any Defered/inlineCallback methods. Returns Response"""
if
val
!=
None
:
try
:
defer
.
returnValue
(
self
.
__renderResponse
(
request
,
val
)
)
return
self
.
__generateResponse
(
request
,
val
)
except
Exception
as
ex
:
msg
=
"Unexpected server error:
%
s
\n
%
s"
%
(
type
(
ex
),
ex
)
defer
.
returnValue
(
self
.
__createErrorResponse
(
request
,
500
,
msg
)
)
return
self
.
__createErrorResponse
(
request
,
500
,
msg
)
else
:
defer
.
returnValue
(
Response
(
209
,
None
)
)
return
Response
(
209
,
None
)
def
__createErrorResponse
(
self
,
request
,
code
,
message
):
"""Common method for rendering errors"""
...
...
corepost/web.py
View file @
6051d011
...
...
@@ -51,13 +51,21 @@ class CorePost(Resource):
def
__renderUrl
(
self
,
request
):
try
:
val
=
self
.
__router
.
getResponse
(
request
)
# return can be Deferred or Response
if
isinstance
(
val
,
Deferred
):
val
.
addCallback
(
self
.
__finishRequest
,
request
)
return
NOT_DONE_YET
elif
isinstance
(
val
,
Response
):
self
.
__applyResponse
(
request
,
val
.
code
,
val
.
headers
)
return
val
.
entity
else
:
raise
RuntimeError
(
"Unexpected return type from request router
%
s"
%
val
)
except
Exception
as
ex
:
self
.
__applyResponse
(
request
,
500
,
None
)
return
str
(
ex
)
def
__finishRequest
(
self
,
response
,
request
):
if
not
request
.
finished
:
self
.
__applyResponse
(
request
,
response
.
code
,
response
.
headers
)
request
.
write
(
response
.
entity
)
request
.
finish
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment