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# See the License for the specific language governing permissions and 18# limitations under the License. 19 20import http.server 21import json 22from typing import Mapping 23 24import avro.ipc 25import avro.protocol 26 27MAIL_PROTOCOL_JSON = 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) 44MAIL_PROTOCOL = avro.protocol.parse(MAIL_PROTOCOL_JSON) 45SERVER_ADDRESS = ("localhost", 9090) 46 47 48class MailResponder(avro.ipc.Responder): 49 def __init__(self) -> None: 50 super().__init__(MAIL_PROTOCOL) 51 52 def invoke(self, message: avro.protocol.Message, request: Mapping[str, Mapping[str, str]]) -> str: 53 if message.name == "send": 54 return f"Sent message to {request['message']['to']} from {request['message']['from']} with body {request['message']['body']}" 55 if message.name == "replay": 56 return "replay" 57 raise RuntimeError 58 59 60class MailHandler(http.server.BaseHTTPRequestHandler): 61 def do_POST(self) -> None: 62 self.responder = MailResponder() 63 call_request_reader = avro.ipc.FramedReader(self.rfile) 64 call_request = call_request_reader.read_framed_message() 65 resp_body = self.responder.respond(call_request) 66 self.send_response(200) 67 self.send_header("Content-Type", "avro/binary") 68 self.end_headers() 69 resp_writer = avro.ipc.FramedWriter(self.wfile) 70 resp_writer.write_framed_message(resp_body) 71 72 73def main(): 74 mail_server = http_server.HTTPServer(SERVER_ADDRESS, MailHandler) 75 mail_server.allow_reuse_address = True 76 mail_server.serve_forever() 77 78 79if __name__ == "__main__": 80 main() 81