Here i am trying to use grpc python betterproto library. i succesfully created proto file which is this,
triangle_service.ex
syntax = “proto3”
package triangle service;
service TriangleService {
rpc CreateUser(UserRequest) returns (UserResponse) {}
}
message UserRequest {
int64 user_id = 1;
string password = 2;
string username = 3;
}
message UserResponse {
User user = 1;
string status = 2;
}
message User {
int64 user_id = 1;
string username = 2;
}
when i compile this proto file then generate proto file contains this,
@dataclass
class User(betterproto.Message):
user_id: int = betterproto.int64_field(1)
username: string = betterproto.string_field(2)
@dataclass
class UserResponse(betterproto.Message):
user: “User” = betterproto.message_field(1)
status: str = betterproto.message_field(2)
@dataclass
class UserRequest(betterproto.Message):
user_id: int = betterproto.int64_field(1)
password: string = betterproto.string_field(2)
username: string = betterproto.string_field(3)
...
i successfully created grpc client.py but when client send data to grpc_server.py then grpc server received data and store in database and upto this point it is successful. we can see data in database but when i tried to send Response it is starting to give error like this
TypeError: Object of type User is not JSON serializable
grpc_server.py
class TriangleService(TriangleServiceBase):
async def create_user(self, user_request: "UserRequest") -> "UserResponse":
# here code for store data in database
response = UserResponse(user = User(user_id = 1, username = "a"), status = "ok")
return response