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
Hide 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:
...
@@ -151,7 +151,6 @@ class RequestRouter:
'''Obsolete'''
'''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"
)
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
):
def
getResponse
(
self
,
request
):
"""Finds the appropriate router and dispatches the request to the registered function. Returns the appropriate Response object"""
"""Finds the appropriate router and dispatches the request to the registered function. Returns the appropriate Response object"""
# see if already cached
# see if already cached
...
@@ -211,7 +210,7 @@ class RequestRouter:
...
@@ -211,7 +210,7 @@ class RequestRouter:
if
isinstance
(
val
,
defer
.
Deferred
):
if
isinstance
(
val
,
defer
.
Deferred
):
# add callback to finish the request
# add callback to finish the request
val
.
addCallback
(
self
.
__finishDeferred
,
request
)
val
.
addCallback
(
self
.
__finishDeferred
,
request
)
yield
val
.
callback
(
request
)
return
val
else
:
else
:
#special logic for POST to return 201 (created)
#special logic for POST to return 201 (created)
if
request
.
method
==
Http
.
POST
:
if
request
.
method
==
Http
.
POST
:
...
@@ -221,26 +220,26 @@ class RequestRouter:
...
@@ -221,26 +220,26 @@ class RequestRouter:
else
:
else
:
request
.
setResponseCode
(
201
)
request
.
setResponseCode
(
201
)
defer
.
returnValue
(
self
.
__renderResponse
(
request
,
val
)
)
return
self
.
__generateResponse
(
request
,
val
,
request
.
code
)
except
exceptions
.
TypeError
as
ex
:
except
exceptions
.
TypeError
as
ex
:
defer
.
returnValue
(
self
.
__createErrorResponse
(
request
,
400
,
"
%
s"
%
ex
)
)
return
self
.
__createErrorResponse
(
request
,
400
,
"
%
s"
%
ex
)
except
Exception
as
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
:
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
:
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)
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
depending on what the caller can accept. Returns Response
"""
"""
if
isinstance
(
response
,
str
):
if
isinstance
(
response
,
str
):
return
Response
(
code
,
response
)
return
Response
(
code
,
response
,{
HttpHeader
.
CONTENT_TYPE
:
MediaType
.
TEXT_PLAIN
}
)
elif
isinstance
(
response
,
Response
):
elif
isinstance
(
response
,
Response
):
return
response
return
response
else
:
else
:
...
@@ -274,12 +273,12 @@ class RequestRouter:
...
@@ -274,12 +273,12 @@ class RequestRouter:
"""Finishes any Defered/inlineCallback methods. Returns Response"""
"""Finishes any Defered/inlineCallback methods. Returns Response"""
if
val
!=
None
:
if
val
!=
None
:
try
:
try
:
defer
.
returnValue
(
self
.
__renderResponse
(
request
,
val
)
)
return
self
.
__generateResponse
(
request
,
val
)
except
Exception
as
ex
:
except
Exception
as
ex
:
msg
=
"Unexpected server error:
%
s
\n
%
s"
%
(
type
(
ex
),
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
:
else
:
defer
.
returnValue
(
Response
(
209
,
None
)
)
return
Response
(
209
,
None
)
def
__createErrorResponse
(
self
,
request
,
code
,
message
):
def
__createErrorResponse
(
self
,
request
,
code
,
message
):
"""Common method for rendering errors"""
"""Common method for rendering errors"""
...
...
corepost/web.py
View file @
6051d011
...
@@ -51,16 +51,24 @@ class CorePost(Resource):
...
@@ -51,16 +51,24 @@ class CorePost(Resource):
def
__renderUrl
(
self
,
request
):
def
__renderUrl
(
self
,
request
):
try
:
try
:
val
=
self
.
__router
.
getResponse
(
request
)
val
=
self
.
__router
.
getResponse
(
request
)
val
.
addCallback
(
self
.
__finishRequest
,
request
)
# return can be Deferred or Response
return
NOT_DONE_YET
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
:
except
Exception
as
ex
:
self
.
__applyResponse
(
request
,
500
,
None
)
self
.
__applyResponse
(
request
,
500
,
None
)
return
str
(
ex
)
return
str
(
ex
)
def
__finishRequest
(
self
,
response
,
request
):
def
__finishRequest
(
self
,
response
,
request
):
self
.
__applyResponse
(
request
,
response
.
code
,
response
.
headers
)
if
not
request
.
finished
:
request
.
write
(
response
.
entity
)
self
.
__applyResponse
(
request
,
response
.
code
,
response
.
headers
)
request
.
finish
()
request
.
write
(
response
.
entity
)
request
.
finish
()
def
__applyResponse
(
self
,
request
,
code
,
headers
=
{
"content-type"
:
MediaType
.
TEXT_PLAIN
}):
def
__applyResponse
(
self
,
request
,
code
,
headers
=
{
"content-type"
:
MediaType
.
TEXT_PLAIN
}):
request
.
setResponseCode
(
code
)
request
.
setResponseCode
(
code
)
...
...
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