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
a7e87051
Commit
a7e87051
authored
Aug 24, 2011
by
Jacek Furmankiewicz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added parsing of form and query args into direct function parameters
parent
54518aef
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
9 deletions
+29
-9
server.py
src/corepost/server.py
+21
-5
server_test.py
src/corepost/test/server_test.py
+8
-4
No files found.
src/corepost/server.py
View file @
a7e87051
...
...
@@ -3,12 +3,13 @@ Main server classes
@author: jacekf
'''
import
re
import
re
,
copy
from
twisted.internet
import
reactor
from
twisted.web.resource
import
Resource
from
twisted.web.server
import
Site
from
collections
import
defaultdict
from
enums
import
MediaType
from
corepost.enums
import
Http
class
RequestRouter
:
""" Common class for containing info related to routing a request to a function """
...
...
@@ -139,9 +140,10 @@ class CorePost(Resource):
def
__renderUrl
(
self
,
request
):
"""Finds the appropriate router and dispatches the request to the registered function"""
# see if already cached
urlrouter
,
pathargs
=
None
,
None
if
request
.
path
in
self
.
__cachedUrls
[
request
.
method
]:
cachedUrl
=
self
.
__cachedUrls
[
request
.
method
][
request
.
path
]
return
cachedUrl
.
router
.
call
(
request
,
**
cachedUrl
.
args
)
urlrouter
,
pathargs
=
cachedUrl
.
router
,
cachedUrl
.
args
else
:
# first time this URL is called
for
router
in
self
.
__urls
[
request
.
method
]
.
values
():
...
...
@@ -149,9 +151,23 @@ class CorePost(Resource):
if
args
!=
None
:
if
router
.
cache
:
self
.
__cachedUrls
[
request
.
method
][
request
.
path
]
=
CachedUrl
(
router
,
args
)
return
router
.
call
(
request
,
**
args
)
return
self
.
__renderError
(
request
,
404
,
"URL '
%
s' not found
\n
"
%
request
.
path
)
urlrouter
,
pathargs
=
router
,
args
#actual call
if
urlrouter
!=
None
and
pathargs
!=
None
:
allargs
=
copy
.
deepcopy
(
pathargs
)
#merge form args
for
arg
in
request
.
args
.
keys
():
# maintain first instance of an argument always
if
arg
not
in
allargs
:
allargs
[
arg
]
=
request
.
args
[
arg
][
0
]
# if POST/PUT, check if we need to automatically parse JSON
# TODO
return
urlrouter
.
call
(
request
,
**
allargs
)
else
:
return
self
.
__renderError
(
request
,
404
,
"URL '
%
s' not found
\n
"
%
request
.
path
)
def
__renderError
(
self
,
request
,
code
,
message
):
"""Common method for rendering errors"""
...
...
src/corepost/test/server_test.py
View file @
a7e87051
...
...
@@ -9,16 +9,20 @@ from corepost.enums import Http
app
=
CorePost
()
@
app
.
route
(
"/"
,
Http
.
GET
)
def
root
(
request
):
return
request
.
path
def
root
(
request
,
**
kwargs
):
return
"
%
s"
%
kwargs
@
app
.
route
(
"/test"
,
Http
.
GET
)
def
test
(
request
):
return
request
.
path
def
test
(
request
,
**
kwargs
):
return
"
%
s"
%
kwargs
@
app
.
route
(
"/test/<int:jacek>/yo/<someid>"
,
Http
.
GET
)
def
test_get_resources
(
request
,
jacek
,
someid
,
**
kwargs
):
return
"
%
s -
%
s"
%
(
jacek
,
someid
)
@
app
.
route
(
"/test"
,
Http
.
POST
)
def
test_post
(
request
,
**
kwargs
):
return
"
%
s"
%
kwargs
if
__name__
==
'__main__'
:
app
.
run
()
\ No newline at end of file
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