twisted的launchkey异步sdk

launchkey-twisted的Python项目详细描述


https://travis-ci.org/LaunchKey/launchkey-python-twisted.svg?branch=master

概述

launchkey twisted库允许与launchkey引擎api进行异步通信 通过python库和twisted。公共接口与 synchronous SDK除了两个例外:

  1. launchkey_twisted.twistedapi的init脚本的第一个参数是 Twisted Web Client Agent
  2. 所有公共方法返回 Twisted Deferred 物体。

安装

$ easy_install launchkey-twisted

$ pip install launchkey-twisted

下面的示例是一个简单的基于会话的启动请求和随后的 脱离轨道(如适用)。不建议对基于生产服务器的实现使用轮询。

importsysfromtwisted.pythonimportlogfromtwisted.internetimporttask,reactor,deferfromtwisted.web.clientimportAgentfromlaunchkey_twistedimportTwistedAPI,PendingResponse# agent is needed to link the SDK to the reactor as an HTTP clientagent=Agent(reactor)# app_key will be provided in the dashboardapp_key=1234567890# app_secret will be provided in the dashboard once, or a new one may be generatedapp_secret="abcdefghijklmnopqrstuvwxyz123456"# private key location will will be the path to the private key file you received from the dashboard or the# private key file you used to generate the public key you uploaded to the dashboardprivate_key_location="/path/to/private.key"# Your LaunchKey usernameusername="myusername"# Poll interval determines the delay in seconds between poll requestspoll_interval=1.0# Log out delay determines the delay in seconds between an accepted authentication and logoutlog_out_delay=3.0# If you are having issues, set this to true for verbose logging onthe reactordebug=Falsedefhandle_auth_response(auth_request):"""
    Receives the auth_request identifier and begin the polling process
    :param auth_request: Identifier for this authentication request returned by the LaunchKey Engine API
    :return: Deferred
    """print("Authentication request successfully initiated with identifier: {0}".format(auth_request))returnpoll_for_user_repsonse(auth_request)defpoll_for_user_repsonse(auth_request):"""
    Schedule the next poll operation.   Adds callback for success and error situations
    :param auth_request: Identifier for this authentication request returned by the LaunchKey Engine API
    :return: Deferred
    """print("Poll for user response in {0} seconds".format(poll_interval))d=task.deferLater(reactor,poll_interval,api.poll_request,auth_request)d.addCallback(handle_poll_response,auth_request)d.addErrback(handle_poll_error)returnddefhandle_poll_error(failure):"""
    Trap PendingResponse errors and schedule another poll
    :param failure:
    :return: Deferred
    """failure.trap(PendingResponse)returnpoll_for_user_repsonse(failure.value.auth_request)defhandle_poll_response(response,auth_request):"""
    Receive the poll response and the original auth_request to validate the poll response package and detyermine the
    outcome.
    :param response: Poll response object
    :param auth_request: Identifier for this authentication request returned by the LaunchKey Engine API
    :return: Deferred
    """print("User response received, checking the response package")deferred=api.is_authorized(auth_request,response['auth'])deferred.addCallback(handle_is_authorized_response,auth_request)returndeferreddefhandle_is_authorized_response(authorized,auth_request):"""
    Receive the is_authorized response and schedule the logout if authorized
    :param authorized: Boolean value depicting the users response to the auth_request.
    (True: accepted | False: declined)
    :param auth_request: Identifier for this authentication request returned by the LaunchKey Engine API
    :return: Deferred
    """ifauthorized:print("User accepted, scheduling logout for {0} seconds from now".format(log_out_delay))deferred=task.deferLater(reactor,log_out_delay,log_user_out,auth_request)else:print("User declined")deferred=defer.succeed(True)returndeferreddeflog_user_out(auth_request):"""
    End the user session for the provided authentication request
    :param auth_request: Identifier for this authentication request returned by the LaunchKey Engine API
    :return: Deferred
    """print("Logging user out")deferred=api.logout(auth_request)deferred.addCallback(done)returndeferreddefhandle_error(failure):"""
    Print an error
    :param failure:
    :return:
    """print("Completed with error: ",failure)returnfailuredefdone(*args):"""
    Stop the reactor
    :param args:
    :return:
    """reactor.stop()print("Reactor stopped")agent=Agent(reactor)api=TwistedAPI(agent,app_key,app_secret,open(private_key_location,"r").read())print("Starting authentication request for user: {0}".format(username))auth=api.authorize(username,True)auth.addCallback(handle_auth_response)auth.addErrback(handle_error)auth.addBoth(done)ifdebug:log.startLogging(sys.stderr)print("Starting reactor")reactor.run()

测试

$ python setup.py nosetests

贡献

  1. 叉开它
  2. 创建您的功能分支(git checkout-b my new feature
  3. 提交更改(git commit-am'add some feature'
  4. 推到分支(git push origin my new feature
  5. 创建新的拉取请求

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Vert。x垂直通道和服务之间的差异   java第二个活动从第一个活动调用后不调用   java如何在运行时从文件夹加载jar文件&在JBoss EAP 6.0.1中实例化类   Windows上带Java的GTK   java SocketException与改进   java计算通过if语句的学生人数我应该在if语句中输入什么?   java显示收到的推送通知   java纹理三角形,仅渲染一种颜色   读取布尔值时发生java输入不匹配异常   在Maven中安装插件时发生java错误   java Mockito InvalidUseofMatcherException   java类型的安卓 Apache。无法将lang.string转换为JSON对象   车牌图像的javaopencv分割   java如何在没有ModelAndView对象的情况下将数据绑定到视图   java确定两个顶点是否连接jGraphX