| ''' |
| Example Action feed for Google Order Redirect. Create "generated" directory for generated proto classes. |
| Download lastest version of protoc from https://github.com/protocolbuffers/protobuf/releases |
| Generate proto classes with: |
| protoc --python_out=./generated --proto_path=../proto/ ../proto/action.proto |
| |
| See https://protobuf.dev/reference/python/python-generated/ for more details. |
| ''' |
| |
| import json |
| from generated import action_pb2 |
| from google.protobuf.json_format import MessageToDict |
| |
| def create_feed() -> str: |
| # Create ActionFeed |
| feed = action_pb2.ActionFeed() |
| |
| # Create ActionDetail by passing fields to the constructor |
| # Example using the same link for delivery and takeout |
| action_detail_combined = action_pb2.ActionDetail( |
| entity_id='merchant-1', |
| link_id='merchant-1-takeout-delivery-action', |
| url='http://provider.com/merchant-1', |
| actions=[ |
| action_pb2.Action( |
| food_ordering_info=action_pb2.FoodOrderingInfo( |
| service_type=action_pb2.FoodOrderingInfo.ServiceType.DELIVERY |
| ) |
| ), |
| action_pb2.Action( |
| food_ordering_info=action_pb2.FoodOrderingInfo( |
| service_type=action_pb2.FoodOrderingInfo.ServiceType.TAKEOUT |
| ) |
| ) |
| ] |
| ) |
| |
| # Add ActionDetail to data |
| feed.data.append(action_detail_combined) |
| |
| # Example using separate link for delivery |
| action_detail_delivery = action_pb2.ActionDetail( |
| entity_id='merchant-2', |
| link_id='merchant-2-delivery-action', |
| url='http://provider.com/merchant-2/delivery', |
| actions=[ |
| action_pb2.Action( |
| food_ordering_info=action_pb2.FoodOrderingInfo( |
| service_type=action_pb2.FoodOrderingInfo.ServiceType.DELIVERY |
| ) |
| ) |
| ] |
| ) |
| |
| # Add ActionDetail to feed data |
| feed.data.append(action_detail_delivery) |
| |
| # Serialize feed to JSON string |
| return json.dumps(MessageToDict(feed, preserving_proto_field_name=True)) |
| |
| feedJSON = create_feed() |
| print(feedJSON) |
| |