用于构建restful服务的werkzeug扩展。
RESTp的Python项目详细描述
werkzeug实用程序,用于构建restful服务。
什么是restpy?
restpy是在werkzeug上构建的一组小实用程序 更容易推出restful web服务。这个项目很少定义唯一的 Werkzeug本身之外的特性。把它想象成烧瓶,但要均匀 更少的功能。
简单用法示例
fromrestpy.apiimportApplicationfromrestpy.apiimportEndpointfromwerkzeug.routingimportMapfromwerkzeug.routingimportRulefromwerkzeug.wrappersimportResponse# Endpoints define HTTP verb methods to handle requests.classIndexEndpoint(Endpoint):defget(self,request):returnResponse("get")defpost(self,request):returnResponse("post")defput(self,request):returnResponse("put")defdelete(self,request):returnResponse("delete")# URL mappings are normal Werkzeug routing Maps.urls=Map([Rule("/",endpoint=IndexEndpoint)])# This object can be exposed to any WSGI server.application=Application(urls)
请求/响应挂钩
除了支持任何标准的wsgi中间件之外,restpy还支持 全局请求和响应挂钩的注册。请求钩子是任何python 可调用的,它接受当前请求作为第一个参数。响应挂钩, 同样,接受请求和响应作为参数。这些可以用来 注入一般功能,如身份验证,而不编写自己的 中间商。
importloggingfromwerkzueg.exceptionsimportUnauthorizedLOG=logging.getLogger(__name__)defauthenticate(request):"""A hook which checks for the secret key."""ifrequest.headers.get('Secrete-Token',None)!=42:raiseUnauthorized()deflog_complete(request,response):"""Log a message after every request."""LOG.debug('Request complete!')application=Application(urls,before=(authenticate,),after=(log_complete,))
“before”和“after”关键字参数将接受任何iterable的hook。 这些钩子将根据每个请求执行。
线程本地存储
在中间件、钩子和端点之间共享数据在 多线程或绿色线程环境。为了帮助你,这个包裹 很容易使用werkzueg本地对象在 线。
importuuidfromrestpy.apiimportApplicationfromrestpy.apiimportContextClearMiddlewarefromrestpy.apiimportEndpointfromrestpyimportcontextfromwerkzeug.routingimportMapfromwerkzeug.routingimportRulefromwerkzeug.wrappersimportResponseclassThreadedEndpoint(Endpoint):defget(self,request):returnResponse(context.storage.unique_value)defunique_value(request):"""Set a random to the context storage."""context.storage.unique_value=(uuid.uuid4())urls=Map([Rule("/",endpoint=ThreadedEndpoint)])# Set the thread context to clear after each request.application=ContextClearMiddleware(Application(urls,before=(unique_value,)))
上面的示例将为每个请求生成一个唯一的值并返回它 在GET请求中。如果wsgi应用程序在多线程中运行 环境,例如使用eventlet或gevent,该值将是 正在执行的线程。ContextClearMiddleware帮助确保 线程完成后,过时的数据不会保留。
唯一请求ID
与这个项目捆绑在一起的是一个中间件和请求挂钩,它们一起工作 为每个命中应用程序的请求提供uuid。这些助手 使用上下文存储还可以方便地记录请求id。
importloggingfromrestpy.apiimportApplicationfromrestpy.apiimportContextClearMiddlewarefromrestpy.apiimportEndpointfromrestpy.apiimportRequestLoggerfromrestpy.apiimportunique_requestfromrestpy.apiimportUniqueEnvironMiddlewarefromwerkzeug.routingimportMapfromwerkzeug.routingimportRulefromwerkzeug.wrappersimportResponseclassUniqueEndpoint(Endpoint):defget(self,request):returnResponse(request.uuid)urls=Map([Rule("/",endpoint=UniqueEndpoint)])# Set the thread context to clear after each request.application=ContextClearMiddleware(UniqueEnvironMiddleware(Application(urls,before=(unique_request))))logging.basicConfig(format='%(levelname)s:%(request_id)s:%(message)s',level=logging.DEBUG,)logging.setLoggerClass(RequestLogger)
上面的示例将应用程序配置为为为每个 请求,将此uuid存储在请求对象和上下文存储上,以及 将日志记录配置为在每个日志消息中包含唯一的请求ID。
许可证
此项目是在与Werkzeug相同的BSD许可下发布的:
Copyright (c) 2013 by Kevin Conway Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
出资人协议
本项目的所有贡献均受贡献者协议保护 在贡献文件中详细说明。所有参与者都应该在 有贡献,但作为总结:
You give us the rights to distribute your code and we promise to maintain an open source release of anything you contribute.