Debian Codegraph @

lang/py3/avro/tests/sample_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 avro import ipc
23 from avro import protocol
24
25 MAIL_PROTOCOL_JSON = """\
26 {"namespace": "example.proto",
27 "protocol": "Mail",
28
29 "types": [
30 {"name": "Message", "type": "record",
31 "fields": [
32 {"name": "to", "type": "string"},
33 {"name": "from", "type": "string"},
34 {"name": "body", "type": "string"}
35 ]
36 }
37 ],
38
39 "messages": {
40 "send": {
41 "request": [{"name": "message", "type": "Message"}],
42 "response": "string"
43 },
44 "replay": {
45 "request": [],
46 "response": "string"
47 }
48 }
49 }
50 """
51 MAIL_PROTOCOL = protocol.Parse(MAIL_PROTOCOL_JSON)
52 SERVER_HOST = 'localhost'
53 SERVER_PORT = 9090
54
55 class UsageError(Exception):
56 def __init__(self, value):
57 self.value = value
58 def __str__(self):
59 return repr(self.value)
60
61 def make_requestor(server_host, server_port, protocol):
62 client = ipc.HTTPTransceiver(SERVER_HOST, SERVER_PORT)
63 return ipc.Requestor(protocol, client)
64
65 if __name__ == '__main__':
66 if len(sys.argv) not in [4, 5]:
67 raise UsageError("Usage: <to> <from> <body> [<count>]")
68
69 # client code - attach to the server and send a message
70 # fill in the Message record
71 message = dict()
72 message['to'] = sys.argv[1]
73 message['from'] = sys.argv[2]
74 message['body'] = sys.argv[3]
75
76 try:
77 num_messages = int(sys.argv[4])
78 except:
79 num_messages = 1
80
81 # build the parameters for the request
82 params = {}
83 params['message'] = message
84
85 # send the requests and print the result
86 for msg_count in range(num_messages):
87 requestor = make_requestor(SERVER_HOST, SERVER_PORT, MAIL_PROTOCOL)
88 result = requestor.request('send', params)
89 print("Result: " + result)
90
91 # try out a replay message
92 requestor = make_requestor(SERVER_HOST, SERVER_PORT, MAIL_PROTOCOL)
93 result = requestor.request('replay', dict())
94 print("Replay Result: " + result)