This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from uber/message
Add Message with parsing/serialziation support
- Loading branch information
Showing
32 changed files
with
1,217 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ping/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.PHONY: gen | ||
|
||
gen: | ||
thrift -out . --gen py ping.thrift |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
struct Pong { | ||
1: required double timestamp | ||
} | ||
|
||
service Ping { | ||
Pong ping(1: string name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from __future__ import absolute_import, unicode_literals, print_function | ||
|
||
import os.path | ||
import requests | ||
|
||
import thriftrw | ||
|
||
ping = thriftrw.load( | ||
os.path.join(os.path.dirname(__file__), 'ping.thrift'), | ||
) | ||
|
||
|
||
def main(): | ||
req = ping.Ping.ping.request('world') | ||
|
||
response = requests.post( | ||
'http://127.0.0.1:8888/thrift', | ||
data=ping.dumps.message(req, seqid=42), | ||
) | ||
reply = ping.loads.message(ping.Ping, response.content) | ||
assert reply.name == 'ping' | ||
assert reply.seqid == 42 | ||
|
||
resp = reply.body | ||
print(resp) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from __future__ import absolute_import | ||
|
||
from thrift.transport.THttpClient import THttpClient | ||
from thrift.protocol.TBinaryProtocol import TBinaryProtocolAccelerated | ||
|
||
from ping import Ping | ||
|
||
|
||
trans = THttpClient('http://localhost:8888/thrift') | ||
proto = TBinaryProtocolAccelerated(trans) | ||
client = Ping.Client(proto) | ||
|
||
print client.ping('world') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from __future__ import absolute_import, unicode_literals, print_function | ||
|
||
import time | ||
import os.path | ||
|
||
from tornado import web | ||
from tornado.ioloop import IOLoop | ||
from tornado.httpserver import HTTPServer | ||
|
||
import thriftrw | ||
|
||
ping = thriftrw.load( | ||
os.path.join(os.path.dirname(__file__), 'ping.thrift'), | ||
) | ||
|
||
|
||
class ThriftRequestHandler(web.RequestHandler): | ||
|
||
def post(self): | ||
assert self.request.body | ||
|
||
message = ping.loads.message(ping.Ping, self.request.body) | ||
method, handler = self._METHODS[message.name] | ||
|
||
args = message.body | ||
resp = method.response(success=handler(self, args)) | ||
|
||
reply = ping.dumps.message(resp, seqid=message.seqid) | ||
self.write(reply) | ||
|
||
def handle_ping(self, args): | ||
print('Hello, %s' % args.name) | ||
return ping.Pong(time.time()) | ||
|
||
_METHODS = {'ping': (ping.Ping.ping, handle_ping)} | ||
|
||
|
||
if __name__ == "__main__": | ||
app = web.Application([ | ||
(r'/thrift', ThriftRequestHandler), | ||
]) | ||
HTTPServer(app).listen(8888) | ||
print('Listening on http://127.0.0.1:8888/thrift') | ||
IOLoop.current().start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.