Ejemplo n.º 1
0
        /// <summary>
        /// Send a message to the QuantConnect Chart Streaming API.
        /// </summary>
        /// <param name="userId">User Id</param>
        /// <param name="apiToken">API token for authentication</param>
        /// <param name="packet">Packet to transmit</param>
        public static void Transmit(int userId, string apiToken, Packet packet)
        {
            try
            {
                using (var client = new WebClient())
                {
                    var tx = JsonConvert.SerializeObject(packet);
                    if (tx.Length > 10000)
                    {
                        Log.Trace("StreamingApi.Transmit(): Packet too long: " + packet.GetType());
                    }

                    var response = client.UploadValues("http://streaming.quantconnect.com", new NameValueCollection
                    {
                        {"uid", userId.ToString()},
                        {"token", apiToken},
                        {"tx", tx}
                    });

                    Log.Trace("StreamingApi.Transmit():  Sending Packet ({0})", packet.GetType());

                    //Deserialize the response from the streaming API and throw in error case.
                    var result = JsonConvert.DeserializeObject<Response>(System.Text.Encoding.UTF8.GetString(response));
                    if (result.Type == "error")
                    {
                        throw new Exception(result.Message);
                    }
                }
            }
            catch (Exception err)
            {
                Log.Error(err);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Send a message to the QuantConnect Chart Streaming API.
        /// </summary>
        /// <param name="userId">User Id</param>
        /// <param name="apiToken">API token for authentication</param>
        /// <param name="packet">Packet to transmit</param>
        public static void Transmit(int userId, string apiToken, Packet packet)
        {
            try
            {
                var tx = JsonConvert.SerializeObject(packet);
                if (tx.Length > 10000)
                {
                    Log.Trace("StreamingApi.Transmit(): Packet too long: " + packet.GetType());
                    return;
                }
                if (userId == 0)
                {
                    Log.Error("StreamingApi.Transmit(): UserId is not set. Check your config.json file 'job-user-id' property.");
                    return;
                }
                if (apiToken == "")
                {
                    Log.Error("StreamingApi.Transmit(): API Access token not set. Check your config.json file 'api-access-token' property.");
                    return;
                }

                var request = new RestRequest();
                request.AddParameter("uid", userId);
                request.AddParameter("token", apiToken);
                request.AddParameter("tx", tx);
                Client.ExecuteAsyncPost(request, (response, handle) =>
                {
                    try
                    {
                        var result = JsonConvert.DeserializeObject<Response>(response.Content);
                        if (result.Type == "error")
                        {
                            Log.Error(new Exception(result.Message), "PacketType: " + packet.Type);
                        }
                    }
                    catch
                    {
                        Log.Error("StreamingApi.Client.ExecuteAsyncPost(): Error deserializing JSON content.");
                    }
                }, "POST");
            }
            catch (Exception err)
            {
                Log.Error(err, "PacketType: " + packet.Type);
            }
        }