Debian Codegraph @

lang/py3/avro/tests/txsample_http_client.py

raw
1 #!/usr/bin/env python3
2 # -*- mode: python -*-
3 # -*- coding: utf-8 -*-
4
5 # Licensed to the Apache Software Foundation (ASF) under one
6 # or more contributor license agreements. See the NOTICE file
7 # distributed with this work for additional information
8 # regarding copyright ownership. The ASF licenses this file
9 # to you under the Apache License, Version 2.0 (the
10 # "License"); you may not use this file except in compliance
11 # with the License. You may obtain a copy of the License at
12 #
13 # http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 import sys
21
22 from twisted.internet import reactor, defer
23 from twisted.python.util import println
24
25 from avro import protocol
26 from avro import txipc
27
28 MAIL_PROTOCOL_JSON = """\
29 {"namespace": "example.proto",
30 "protocol": "Mail",
31
32 "types": [
33 {"name": "Message", "type": "record",
34 "fields": [
35 {"name": "to", "type": "string"},
36 {"name": "from", "type": "string"},
37 {"name": "body", "type": "string"}
38 ]
39 }
40 ],
41
42 "messages": {
43 "send": {
44 "request": [{"name": "message", "type": "Message"}],
45 "response": "string"
46 },
47 "replay": {
48 "request": [],
49 "response": "string"
50 }
51 }
52 }
53 """
54 MAIL_PROTOCOL = protocol.Parse(MAIL_PROTOCOL_JSON)
55 SERVER_HOST = 'localhost'
56 SERVER_PORT = 9090
57
58 class UsageError(Exception):
59 def __init__(self, value):
60 self.value = value
61 def __str__(self):
62 return repr(self.value)
63
64 def make_requestor(server_host, server_port, protocol):
65 client = txipc.TwistedHTTPTransceiver(SERVER_HOST, SERVER_PORT)
66 return txipc.TwistedRequestor(protocol, client)
67
68 if __name__ == '__main__':
69 if len(sys.argv) not in [4, 5]:
70 raise UsageError("Usage: <to> <from> <body> [<count>]")
71
72 # client code - attach to the server and send a message
73 # fill in the Message record
74 message = dict()
75 message['to'] = sys.argv[1]
76 message['from'] = sys.argv[2]
77 message['body'] = sys.argv[3]
78
79 try:
80 num_messages = int(sys.argv[4])
81 except:
82 num_messages = 1
83
84 # build the parameters for the request
85 params = {}
86 params['message'] = message
87
88 requests = []
89 # send the requests and print the result
90 for msg_count in range(num_messages):
91 requestor = make_requestor(SERVER_HOST, SERVER_PORT, MAIL_PROTOCOL)
92 d = requestor.request('send', params)
93 d.addCallback(lambda result: println("Result: " + result))
94 requests.append(d)
95 results = defer.gatherResults(requests)
96
97 def replay_cb(result):
98 print("Replay Result: " + result)
99 reactor.stop()
100
101 def replay(_):
102 # try out a replay message
103 requestor = make_requestor(SERVER_HOST, SERVER_PORT, MAIL_PROTOCOL)
104 d = requestor.request('replay', dict())
105 d.addCallback(replay_cb)
106
107 results.addCallback(replay)
108 reactor.run()