exante / python-protobuf-simple-socket-rpc

MIT License
4 stars 3 forks source link

Using Protobuf for Spotware open API #1

Closed jovana closed 2 years ago

jovana commented 2 years ago

Hi 👋 , I came around your repo while searching for a solution to use the Open API from Spotware: https://spotware.github.io/open-api-docs/messages/

They also have a Python lib based on Twisted.internet. I don't know and don't want to use the framework from Twisted. Your files helped me to start building.

I have created this simple test to find out how to implement this. I have some difficulties to figure out what is wrong, I hope you can take a look and give me a push in the right direction?


from protobuf_simple_socket_rpc.socket_rpc import SocketRPC
import socket
from google.protobuf.message import Message
from ctrader_open_api.messages.OpenApiMessages_pb2 import (
    ProtoOAApplicationAuthReq,
    ProtoOAApplicationAuthRes,
)

host = ("demo.ctraderapi.com", 5035)
socket = test_rpc.SocketRPC(socket.AF_INET, socket.SOCK_STREAM)
socket.connect(host)

request = ProtoOAApplicationAuthReq()
request.clientId = "CLIENT_ID"
request.clientSecret = "SECRET
print(request)

# request = Message()
socket.send_message(request)

response = ProtoOAApplicationAuthRes()
socket.read_message(response)

print(response)

Running this script gives me this error:

  File "/Users/***/protobuf_simple_socket_rpc/socket_rpc.py", line 73, in read_message
    message.ParseFromString(self.__recv(message_len))
  File "/Users/****/protobuf_simple_socket_rpc/socket_rpc.py", line 31, in __recv
    raise RuntimeError('connection closed before chunk was read')
RuntimeError: connection closed before chunk was read

To have the Protobuf message files you can run pip install protobuf_simple_socket_rpc or you can get them from here: https://github.com/spotware/OpenApiPy/tree/main/ctrader_open_api/messages

I hope you can find the time to have a look.

many thanks! 🙏

jovana commented 2 years ago

I have found a solution. I will close this.