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
11a5f1ee
Commit
11a5f1ee
authored
Sep 05, 2011
by
Jacek Furmankiewicz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
doc updates
parent
446c73bf
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
94 additions
and
99 deletions
+94
-99
README.md
README.md
+79
-85
setup.py
setup.py
+15
-14
No files found.
README.md
View file @
11a5f1ee
...
...
@@ -12,25 +12,25 @@ Single REST module example
The simplest possible REST application:
from corepost.web import CorePost
from corepost.web import CorePost, route
from corepost.enums import Http
app = CorePost()
class RestApp(CorePost):
@app.
route("/",Http.GET)
def root(
request,**kwargs):
@
route("/",Http.GET)
def root(self,
request,**kwargs):
return request.path
@app.
route("/test",Http.GET)
def test(
request,**kwargs):
@
route("/test",Http.GET)
def test(self,
request,**kwargs):
return request.path
@app.route("/test/<int:numericid>/test2/<string
id>",Http.GET)
def test_get_resources(request,numericid,string
id,**kwargs):
return "%s - %s" % (numericid,stringid)
@route("/test/<int:numeric
id>",Http.GET)
def test_get_resources(self,request,numeric
id,**kwargs):
return "%s" % numericid
if __name__ == '__main__':
# shortcut method to run a CorePost Resource as a single site
app = RestApp()
app.run()
...
...
@@ -41,60 +41,54 @@ The key CorePost object is just an extension of the regular twisted.web Resource
Therefore, it can easily be used to assemble a multi-module REST applications with
different CorePost resources serving from different context paths:
from corepost.web import CorePost
from corepost.web import CorePost, route
from corepost.enums import Http
from twisted.web.resource import Resource
from twisted.internet import reactor
from twisted.web.server import Site
# Home module
home = CorePost()
class HomeApp(CorePost):
@home.
route("/")
def home_root(
request,**kwargs):
@
route("/")
def home_root(self,
request,**kwargs):
return "HOME %s" % kwargs
# First module
module1 = CorePost('module1')
class Module1(CorePost):
@module1.
route("/",Http.GET)
def module1_get(
request,**kwargs):
@
route("/",Http.GET)
def module1_get(self,
request,**kwargs):
return request.path
@module1.
route("/sub",Http.GET)
def module1e_sub(
request,**kwargs):
@
route("/sub",Http.GET)
def module1e_sub(self,
request,**kwargs):
return request.path
# Second module
module2 = CorePost('module2')
class Module2(CorePost):
@module2.
route("/",Http.GET)
def module2_get(
request,**kwargs):
@
route("/",Http.GET)
def module2_get(self,
request,**kwargs):
return request.path
@module2.
route("/sub",Http.GET)
def module2_sub(
request,**kwargs):
@
route("/sub",Http.GET)
def module2_sub(self,
request,**kwargs):
return request.path
if __name__ == '__main__'
:
def run_app_multi()
:
app = Resource()
app.putChild(home.path, home
)
app.putChild(module1.path,module1
)
app.putChild(module2.path,module2
)
app.putChild('', HomeApp()
)
app.putChild('module1',Module1()
)
app.putChild('module2',Module2()
)
factory = Site(app)
reactor.listenTCP(8080, factory)
reactor.run()
reactor.listenTCP(8081, factory) #@UndefinedVariable
reactor.run() #@UndefinedVariable
The example above creates 3 modules ("/","module1","/module2") and exposes the following URLs:
http://127.0.0.1:8080
http://127.0.0.1:8080/
http://127.0.0.1:8080/module1
http://127.0.0.1:8080/module1/
http://127.0.0.1:8080/module1/sub
http://127.0.0.1:8080/module2
http://127.0.0.1:8080/module2/
http://127.0.0.1:8080/module2/sub
Path argument extraction
...
...
@@ -110,8 +104,8 @@ The supported types are:
Example:
@
app.
route("/int/<int:intarg>/float/<float:floatarg>/string/<stringarg>",Http.GET)
def test(request,intarg,floatarg,stringarg,**kwargs):
@route("/int/<int:intarg>/float/<float:floatarg>/string/<stringarg>",Http.GET)
def test(
self,
request,intarg,floatarg,stringarg,**kwargs):
pass
Argument validation
...
...
@@ -120,25 +114,25 @@ Argument validation
CorePost integrates the popular 'formencode' package to implement form and query argument validation.
Validators can be specified using a
*formencode*
Schema object, or via custom field-specific validators, e.g.:
from corepost.web import CorePost, validate
from corepost.web import CorePost, validate
, route
from corepost.enums import Http
from formencode import Schema, validators
app = CorePost()
class TestSchema(Schema):
allow_extra_fields = True
childId = validators.Regex(regex="^value1|value2$")
@app.route("/validate/<int:rootId>/schema",Http.POST)
@validate(schema=TestSchema)
def postValidateSchema(request,rootId,childId,**kwargs):
class MyApp(CorePost):
@route("/validate/<int:rootId>/schema",Http.POST)
@validate(schema=TestSchema())
def postValidateSchema(self,request,rootId,childId,**kwargs):
'''Validate using a common schema'''
return "%s - %s - %s" % (rootId,childId,kwargs)
@app.
route("/validate/<int:rootId>/custom",Http.POST)
@
route("/validate/<int:rootId>/custom",Http.POST)
@validate(childId=validators.Regex(regex="^value1|value2$"))
def postValidateCustom(
request,rootId,childId,**kwargs):
def postValidateCustom(self,
request,rootId,childId,**kwargs):
'''Validate using argument-specific validators'
return "%s - %s - %s" % (rootId,childId,kwargs)
...
...
@@ -153,7 +147,7 @@ for list of available validators:
If you want a deferred async method, just complete the request yourself, instead of returning a string response
@
app.
route("/",Http.GET)
@route("/",Http.GET)
@defer.inlineCallbacks
def root(request,**kwargs):
val = yield db.query("SELECT ....")
...
...
setup.py
View file @
11a5f1ee
...
...
@@ -10,24 +10,25 @@ The simplest possible twisted.web CorePost REST application:
::
from corepost.web import CorePost
from corepost.web import CorePost
, route
from corepost.enums import Http
app = CorePost()
class RestApp(CorePost):
@app.
route("/",Http.GET)
def root(
request,**kwargs):
@
route("/",Http.GET)
def root(self,
request,**kwargs):
return request.path
@app.
route("/test",Http.GET)
def test(
request,**kwargs):
@
route("/test",Http.GET)
def test(self,
request,**kwargs):
return request.path
@app.
route("/test/<int:numericid>",Http.GET)
def test_get_resources(
request,numericid,**kwargs):
@
route("/test/<int:numericid>",Http.GET)
def test_get_resources(self,
request,numericid,**kwargs):
return "
%
s"
%
numericid
if __name__ == '__main__':
app = RestApp()
app.run()
Links
...
...
@@ -60,7 +61,7 @@ setup(
name
=
"CorePost"
,
version
=
"0.0.6"
,
author
=
"Jacek Furmankiewicz"
,
author_email
=
"jacek99@gmail.com"
,
author_email
=
"jacek
eadE
99@gmail.com"
,
description
=
(
"A Twisted Web REST micro-framework"
),
license
=
"BSD"
,
keywords
=
"twisted rest flask sinatra get post put delete web"
,
...
...
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