python - How to write a common API client for Tornado and non-Tornado services -


i have web-service exposes api. want use api in 2 applications, 1 command-line tool, , web-server itself.

for web-server, using python tornado, able use aynchttpclient, gen.coroutine, etc. cli, can not use tornado since needs ioloop running async work.

i looking create kind of library talks api, , re-use library in cli web-service. means should able write (possibly tornado gen) code on top of functions make async.

why not use ioloop in cli tool?

@gen.coroutine def main():     client = asynchttpclient()     response = yield client.fetch('http://www.google.com')  if __name__ == '__main__':     ioloop.instance().run_sync(main) 

there's synchronous tornado.httpclient.httpclient uses same request , response objects asynchttpclient.

if want http-client-agnostic you'll need follow example of oauthlib. provide base client speaks in terms of http headers , response bodies, , separately provide bindings client various implementations requests-oauthlib. (or release base client , expect callers figure out mapping own http implementation)


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -