Esempio n. 1
0
        /// <summary>
        /// Execute a authenticated call:
        /// </summary>
        public T Execute <T>(RestRequest request, TradierApiRequestType type, string rootName = "") where T : new()
        {
            T response;

            lock (_lockAccessCredentials)
            {
                var client = new RestClient(_requestEndpoint);
                client.AddDefaultHeader("Accept", "application/json");
                client.AddDefaultHeader("Authorization", "Bearer " + _accessToken);
                //client.AddDefaultHeader("Content-Type", "application/x-www-form-urlencoded");

                //Wait for the API rate limiting
                while (DateTime.Now < _rateLimitNextRequest[type])
                {
                    Thread.Sleep(10);
                }
                _rateLimitNextRequest[type] = DateTime.Now + _rateLimitPeriod[type];

                //Send the request:
                var raw = client.Execute(request);
                _previousRequestRaw = raw.Content;

                if (rootName != "")
                {
                    response = DeserializeRemoveRoot <T>(raw.Content, rootName);
                }
                else
                {
                    response = JsonConvert.DeserializeObject <T>(raw.Content);
                }

                if (response == null)
                {
                    var fault = JsonConvert.DeserializeObject <TradierFaultContainer>(raw.Content);
                    if (fault != null)
                    {
                        // JSON Errors:
                        ErrorHandler(fault.Fault);
                    }
                    else
                    {
                        // Text Errors:
                        var textFault = new TradierFault();
                        textFault.Description = raw.Content;
                        ErrorHandler(textFault);
                    }
                }

                if (raw.ErrorException != null)
                {
                    const string message = "Error retrieving response.  Check inner details for more info.";
                    throw new ApplicationException(message, raw.ErrorException);
                }
            }

            return(response);
        }
Esempio n. 2
0
        /// <summary>
        /// Tradier Fault Error Handlers:
        /// </summary>
        public void ErrorHandler(TradierFault fault)
        {
            Log.Error("Tradier.ErrorHandler(): " + fault.Description);

            //Add the fault to record:
            _faults.Add(fault);

            if (_errorHandlers.ContainsKey(fault.Description.ToLower()))
            {
                _errorHandlers[fault.Description.ToLower()]();
            }
        }