forked from vimeo/vimeo.py
/
server.py
63 lines (51 loc) · 1.84 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import tornado.ioloop
import tornado.web
import tornado.gen
from vimeo import VimeoClient
class TestSyncHandler(tornado.web.RequestHandler):
def get(self):
"""
Demonstrates synchronous use of VimeoClient
Caller does not need to be wrapped in a decorator, result is returned synchronously
"""
vimeo = VimeoClient("YOUR ACCESS TOKEN")
res = vimeo.users.emmett9001()
self.write(res)
self.finish()
class TestCoroutineHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
"""
Demonstrates asynchronous use of VimeoClient via tornado.gen
The caller must be decorated by tornado.gen.coroutine
The call to an API function must include the async kwarg set to True
The result must be yielded before assignment
"""
vimeo = VimeoClient("YOUR ACCESS TOKEN")
res = yield vimeo.users(query='cats', async=True)
self.write(res)
self.finish()
class TestCallbackHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
"""
Demonstrates asynchronous use of VimeoClient via callback function
Caller must be wrapped in @tornado.web.asynchronous
Call to API function must include the callback kwarg
"""
vimeo = VimeoClient("YOUR ACCESS TOKEN")
def callback(result):
self.write(result)
self.finish()
vimeo.users.emmett9001(callback=callback)
def get_app():
application = tornado.web.Application([
(r"/test_sync", TestSyncHandler),
(r"/test_coroutine", TestCoroutineHandler),
(r"/test_callback", TestCallbackHandler),
])
return application
if __name__ == "__main__":
application = get_app()
application.listen(5000)
tornado.ioloop.IOLoop.instance().start()