1#!/usr/bin/env python3
2
3##
4# Licensed to the Apache Software Foundation (ASF) under one
5# or more contributor license agreements.  See the NOTICE file
6# distributed with this work for additional information
7# regarding copyright ownership.  The ASF licenses this file
8# to you under the Apache License, Version 2.0 (the
9# "License"); you may not use this file except in compliance
10# with the License.  You may obtain a copy of the License at
11#
12#     https://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS,
16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17#
18# See the License for the specific language governing permissions and
19# limitations under the License.
20import argparse
21import json
22
23import avro.ipc
24import avro.protocol
25
26MAIL_PROTOCOL = avro.protocol.parse(
27    json.dumps(
28        {
29            "namespace": "example.proto",
30            "protocol": "Mail",
31            "types": [
32                {
33                    "name": "Message",
34                    "type": "record",
35                    "fields": [{"name": "to", "type": "string"}, {"name": "from", "type": "string"}, {"name": "body", "type": "string"}],
36                }
37            ],
38            "messages": {
39                "send": {"request": [{"name": "message", "type": "Message"}], "response": "string"},
40                "replay": {"request": [], "response": "string"},
41            },
42        }
43    )
44)
45SERVER_HOST = "localhost"
46SERVER_PORT = 9090
47
48
49def make_requestor(server_host: str, server_port: int, protocol: avro.protocol.Protocol) -> avro.ipc.Requestor:
50    client = avro.ipc.HTTPTransceiver(server_host, server_port)
51    return avro.ipc.Requestor(protocol, client)
52
53
54def _parse_args() -> argparse.Namespace:
55    """Parse the command-line arguments"""
56    parser = argparse.ArgumentParser()
57    parser.add_argument("to", help="Who the message is to")
58    parser.add_argument("from_", help="Who the message is from")
59    parser.add_argument("body", help="The message body")
60    parser.add_argument("num_messages", type=int, default=1, help="The number of messages")
61    return parser.parse_args()
62
63
64def main() -> int:
65    # client code - attach to the server and send a message fill in the Message record
66    args = _parse_args()
67    params = {"message": {"to": args.to, "from": args.from_, "body": args.body}}
68    # send the requests and print the result
69    for msg_count in range(args.num_messages):
70        requestor = make_requestor(SERVER_HOST, SERVER_PORT, MAIL_PROTOCOL)
71        result = requestor.request("send", params)
72        print(f"Result: {result}")
73    # try out a replay message
74    requestor = make_requestor(SERVER_HOST, SERVER_PORT, MAIL_PROTOCOL)
75    result = requestor.request("replay", dict())
76    print(f"Replay Result: {result}")
77    return 0
78
79
80if __name__ == "__main__":
81    raise SystemExit(main())
82