3

I am pretty new to protobuf and all of this, but I am trying to take a list of dictionaries and write them to a service using RPC/protobuf. Here is the proto:

syntax = "proto3";

package twittercontent.v1;

message TwitterContentRequest {
    string messageId           = 1;
    bool isPrivate             = 2;
    string handleId            = 3;
}

message TwitterContentResponse {
    string response = 1;
}

service TwitterContentService {
    rpc TwitterContent(TwitterContentRequest) returns (TwitterContentResponse) {}
}

I have the following list of dicts as well (just test data here):

test = [
        {"messageId": "23452345324", "isPrivate": False, "handleId": "q35jmefn"},
        {"messageId": "wegwer", "isPrivate": False, "handleId": "webwerbtetny"}
       ]

I'm not sure what to do from here, I've tried something like this:

from twittercontentservice import twittercontent_pb2

def sendMsg(test):
    result = []
    for i in test:
        unit = twittercontent_pb2.TwitterContentRequest()
        unit.messageId = i['messageId']
        unit.isPrivate = i['isPrivate']
        unit.handleId = i['handleId']
        result.append(unit)
    return result

sendMsg(test)

But I don't think this works, when I print the result of the function, it's just a list of the very last element in the test list. Any pointers from here would be great

DBA108642
  • 1,995
  • 1
  • 18
  • 55

2 Answers2

0

Your code as-is will map test into a list of twittercontent_pb2.TwitterContentRequest

You can prove this to yourself with print(len(sendMsg(test))) which should return 2.

However, this is all that you're doing.

You write that you want to "write them to a service" but your question includes insufficient detail of this service to provide an answer.

If the service is a gRPC service (these use protobuf messages), then you will need to create a gRPC client and use the client to submit your messages to the service.

See: gRPC Basic Tutorial for Python

DazWilkin
  • 32,823
  • 5
  • 47
  • 88
0

Your proto is Wrong as according to your proto you are requesting a single dictionary in your message and expecting multiple dictionaries. To resolve it you need to add a repeated keyword so your proto will become something like this:

syntax = "proto3";

package twittercontent.v1;

message TwitterContentRequest {
    repeated TwitterContent contentRequest = 1;
}


message TwitterContent
{
    string messageId           = 1;
    bool isPrivate             = 2;
    string handleId            = 3;
}

message TwitterContentResponse {
    string response = 1;
}

service TwitterContentService {
    rpc TwitterContent(TwitterContentRequest) returns (TwitterContentResponse);
}
Lakshika Parihar
  • 1,017
  • 9
  • 19