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
...
@@ -12,25 +12,25 @@ Single REST module example
The simplest possible REST application:
The simplest possible REST application:
from corepost.web import CorePost
from corepost.web import CorePost, route
from corepost.enums import Http
from corepost.enums import Http
app = CorePost()
class RestApp(CorePost):
@app.
route("/",Http.GET)
@
route("/",Http.GET)
def root(
request,**kwargs):
def root(self,
request,**kwargs):
return request.path
return request.path
@app.
route("/test",Http.GET)
@
route("/test",Http.GET)
def test(
request,**kwargs):
def test(self,
request,**kwargs):
return request.path
return request.path
@app.route("/test/<int:numericid>/test2/<string
id>",Http.GET)
@route("/test/<int:numeric
id>",Http.GET)
def test_get_resources(request,numericid,string
id,**kwargs):
def test_get_resources(self,request,numeric
id,**kwargs):
return "%s - %s" % (numericid,stringid)
return "%s" % numericid
if __name__ == '__main__':
if __name__ == '__main__':
# shortcut method to run a CorePost Resource as a single site
app = RestApp()
app.run()
app.run()
...
@@ -41,60 +41,54 @@ The key CorePost object is just an extension of the regular twisted.web Resource
...
@@ -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
Therefore, it can easily be used to assemble a multi-module REST applications with
different CorePost resources serving from different context paths:
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 corepost.enums import Http
from twisted.web.resource import Resource
from twisted.web.resource import Resource
from twisted.internet import reactor
from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.server import Site
# Home module
class HomeApp(CorePost):
home = CorePost()
@home.
route("/")
@
route("/")
def home_root(
request,**kwargs):
def home_root(self,
request,**kwargs):
return "HOME %s" % kwargs
return "HOME %s" % kwargs
# First module
class Module1(CorePost):
module1 = CorePost('module1')
@module1.
route("/",Http.GET)
@
route("/",Http.GET)
def module1_get(
request,**kwargs):
def module1_get(self,
request,**kwargs):
return request.path
return request.path
@module1.
route("/sub",Http.GET)
@
route("/sub",Http.GET)
def module1e_sub(
request,**kwargs):
def module1e_sub(self,
request,**kwargs):
return request.path
return request.path
# Second module
class Module2(CorePost):
module2 = CorePost('module2')
@module2.
route("/",Http.GET)
@
route("/",Http.GET)
def module2_get(
request,**kwargs):
def module2_get(self,
request,**kwargs):
return request.path
return request.path
@module2.
route("/sub",Http.GET)
@
route("/sub",Http.GET)
def module2_sub(
request,**kwargs):
def module2_sub(self,
request,**kwargs):
return request.path
return request.path
if __name__ == '__main__'
:
def run_app_multi()
:
app = Resource()
app = Resource()
app.putChild(home.path, home
)
app.putChild('', HomeApp()
)
app.putChild(module1.path,module1
)
app.putChild('module1',Module1()
)
app.putChild(module2.path,module2
)
app.putChild('module2',Module2()
)
factory = Site(app)
factory = Site(app)
reactor.listenTCP(8080, factory)
reactor.listenTCP(8081, factory) #@UndefinedVariable
reactor.run()
reactor.run() #@UndefinedVariable
The example above creates 3 modules ("/","module1","/module2") and exposes the following URLs:
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/
http://127.0.0.1:8080/module1
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/module1/sub
http://127.0.0.1:8080/module2
http://127.0.0.1:8080/module2
http://127.0.0.1:8080/module2/
http://127.0.0.1:8080/module2/sub
http://127.0.0.1:8080/module2/sub
Path argument extraction
Path argument extraction
...
@@ -110,8 +104,8 @@ The supported types are:
...
@@ -110,8 +104,8 @@ The supported types are:
Example:
Example:
@
app.
route("/int/<int:intarg>/float/<float:floatarg>/string/<stringarg>",Http.GET)
@route("/int/<int:intarg>/float/<float:floatarg>/string/<stringarg>",Http.GET)
def test(request,intarg,floatarg,stringarg,**kwargs):
def test(
self,
request,intarg,floatarg,stringarg,**kwargs):
pass
pass
Argument validation
Argument validation
...
@@ -120,25 +114,25 @@ Argument validation
...
@@ -120,25 +114,25 @@ Argument validation
CorePost integrates the popular 'formencode' package to implement form and query 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.:
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 corepost.enums import Http
from formencode import Schema, validators
from formencode import Schema, validators
app = CorePost()
class TestSchema(Schema):
class TestSchema(Schema):
allow_extra_fields = True
allow_extra_fields = True
childId = validators.Regex(regex="^value1|value2$")
childId = validators.Regex(regex="^value1|value2$")
@app.route("/validate/<int:rootId>/schema",Http.POST)
class MyApp(CorePost):
@validate(schema=TestSchema)
def postValidateSchema(request,rootId,childId,**kwargs):
@route("/validate/<int:rootId>/schema",Http.POST)
@validate(schema=TestSchema())
def postValidateSchema(self,request,rootId,childId,**kwargs):
'''Validate using a common schema'''
'''Validate using a common schema'''
return "%s - %s - %s" % (rootId,childId,kwargs)
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$"))
@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'
'''Validate using argument-specific validators'
return "%s - %s - %s" % (rootId,childId,kwargs)
return "%s - %s - %s" % (rootId,childId,kwargs)
...
@@ -153,7 +147,7 @@ for list of available validators:
...
@@ -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
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
@defer.inlineCallbacks
def root(request,**kwargs):
def root(request,**kwargs):
val = yield db.query("SELECT ....")
val = yield db.query("SELECT ....")
...
...
setup.py
View file @
11a5f1ee
...
@@ -10,24 +10,25 @@ The simplest possible twisted.web CorePost REST application:
...
@@ -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
from corepost.enums import Http
app = CorePost()
class RestApp(CorePost):
@app.
route("/",Http.GET)
@
route("/",Http.GET)
def root(
request,**kwargs):
def root(self,
request,**kwargs):
return request.path
return request.path
@app.
route("/test",Http.GET)
@
route("/test",Http.GET)
def test(
request,**kwargs):
def test(self,
request,**kwargs):
return request.path
return request.path
@app.
route("/test/<int:numericid>",Http.GET)
@
route("/test/<int:numericid>",Http.GET)
def test_get_resources(
request,numericid,**kwargs):
def test_get_resources(self,
request,numericid,**kwargs):
return "
%
s"
%
numericid
return "
%
s"
%
numericid
if __name__ == '__main__':
if __name__ == '__main__':
app = RestApp()
app.run()
app.run()
Links
Links
...
@@ -60,7 +61,7 @@ setup(
...
@@ -60,7 +61,7 @@ setup(
name
=
"CorePost"
,
name
=
"CorePost"
,
version
=
"0.0.6"
,
version
=
"0.0.6"
,
author
=
"Jacek Furmankiewicz"
,
author
=
"Jacek Furmankiewicz"
,
author_email
=
"jacek99@gmail.com"
,
author_email
=
"jacek
eadE
99@gmail.com"
,
description
=
(
"A Twisted Web REST micro-framework"
),
description
=
(
"A Twisted Web REST micro-framework"
),
license
=
"BSD"
,
license
=
"BSD"
,
keywords
=
"twisted rest flask sinatra get post put delete web"
,
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