API integration with Golang

Created at 16 Nov 2020, 15:21
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
LS

lsgndln

Joined 13.11.2020

API integration with Golang
16 Nov 2020, 15:21


Hello, I wanted to know if one of you guys had an example of integration of the ctrader Open API with Golang ? I am trying to port the C# example to Go but I have some issues with that.

What I have done so far :

  • generate the Protobuf go files
  • connect to the server demo API server
  • creating the ProtoMessage with a ProtoOAApplicationAuthReq payload 

What I receive from the server :

�3FRAME_TOO_LONG"!Frame size exceeds allowed limit.

I assume it is some kind of byte conversion or websocket error but I am not sure.

Below is the code I use. I somebody could help me, I would appreciate it a lot :D Thank you in advance


func main() {
	clientID := "xxxxx"
	clientSecret := "xxxxx"

	conn, err := tls.Dial("tcp", "demo.ctraderapi.com:5035", nil)
	if err != nil {
		log.Fatal(err)
	}

	request := pb.ProtoOAApplicationAuthReq{
		ClientId:     &clientID,
		ClientSecret: &clientSecret,
	}
	bytes, _, err := toBytes(request)
	if err != nil {
		log.Fatal(err)
	}

	payloadType := uint32(pb.ProtoOAPayloadType_PROTO_OA_APPLICATION_AUTH_REQ)
	msg := pb.ProtoMessage{
		PayloadType: &payloadType,
		Payload:     bytes,
	}

	for {
		transmit(conn, msg)

		go func() {
			bytes, _, err := bufio.NewReader(conn).ReadLine()
			if err != nil && err != io.EOF {
				spew.Dump(err)
			}

			fmt.Print("Message from server: " + string(bytes) + "\n")
			spew.Dump(bytes)
		}()
		time.Sleep(time.Second)
	}
}

func transmit(conn net.Conn, msg pb.ProtoMessage) {
	bytes, length, err := toBytes(msg)
	if err != nil {
		spew.Dump(err)
	}

	conn.Write([]byte(strconv.Itoa(length)))
	conn.Write(bytes)
}

func toBytes(object interface{}) ([]byte, int, error) {
	bytes, err := json.Marshal(object)
	if err != nil {
		return nil, 0, err
	}

	fmt.Println(bytes)
	fmt.Println(len(bytes))
	return bytes, len(bytes), nil
}

 


@lsgndln