Esempio n. 1
0
        public async Task <string> SymbolLastFieldExecuteAsync(string urlPattern, string symbol, string field, int last, string token)
        {
            var qsb = new QueryStringBuilder();

            if (!string.IsNullOrEmpty(token))
            {
                qsb.Add("token", token);
            }

            var pathNvc = new NameValueCollection {
                { "symbol", symbol }, { "last", last.ToString() }, { "field", field }
            };

            return(await ExecuteAsyncLegacy <string>(urlPattern, pathNvc, qsb));
        }
Esempio n. 2
0
        public async Task <ReturnType> SymbolLastExecuteAsync <ReturnType>(string urlPattern, string symbol, int last, string token)
            where ReturnType : class
        {
            var qsb = new QueryStringBuilder();

            if (!string.IsNullOrEmpty(token))
            {
                qsb.Add("token", token);
            }

            var pathNvc = new NameValueCollection {
                { "symbol", symbol }, { "last", last.ToString() }
            };

            return(await ExecuteAsyncLegacy <ReturnType>(urlPattern, pathNvc, qsb));
        }
Esempio n. 3
0
        public async Task <ReturnType> ExecuteAsyncLegacy <ReturnType>(string urlPattern, NameValueCollection pathNVC, QueryStringBuilder qsb)
            where ReturnType : class
        {
            ValidateAndProcessParams(ref urlPattern, ref pathNVC, ref qsb);

            if (!string.IsNullOrEmpty(publishableToken))
            {
                qsb.Add("token", publishableToken);
            }

            var content = string.Empty;

            if (sign)
            {
                client.DefaultRequestHeaders.Remove("x-iex-date");
                client.DefaultRequestHeaders.Remove("Authorization");

                (string iexdate, string authorization_header)headers = signer.Sign(publishableToken, "GET", urlPattern, qsb.Build());
                client.DefaultRequestHeaders.TryAddWithoutValidation("x-iex-date", headers.iexdate);
                client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", headers.authorization_header);
            }

            using (var responseContent = await client.GetAsync($"{urlPattern}{qsb.Build()}"))
            {
                try
                {
                    content = await responseContent.Content.ReadAsStringAsync();

                    return(JsonConvert.DeserializeObject <ReturnType>(content, jsonSerializerSettings));
                }
                catch (JsonException ex)
                {
                    throw new JsonException(content, ex);
                }
            }
        }
Esempio n. 4
0
        public async Task <IEXResponse <ReturnType> > ExecuteAsync <ReturnType>(
            string urlPattern, NameValueCollection pathNVC, QueryStringBuilder qsb, bool forceUseSecretToken = false)
            where ReturnType : class
        {
            ValidateAndProcessParams(ref urlPattern, ref pathNVC, ref qsb);

            if (!string.IsNullOrEmpty(publishableToken) && !forceUseSecretToken)
            {
                qsb.Add("token", publishableToken);
            }

            if (!string.IsNullOrEmpty(secretToken) && forceUseSecretToken)
            {
                qsb.Add("token", secretToken);
            }

            var content = string.Empty;

            if (sign)
            {
                client.DefaultRequestHeaders.Remove("x-iex-date");
                client.DefaultRequestHeaders.Remove("Authorization");

                (string iexdate, string authorization_header)headers = signer.Sign(publishableToken, "GET", urlPattern, qsb.Build());
                client.DefaultRequestHeaders.TryAddWithoutValidation("x-iex-date", headers.iexdate);
                client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", headers.authorization_header);
            }

            using (var responseContent = await client.GetAsync($"{urlPattern}{qsb.Build()}"))
            {
                try
                {
                    content = await responseContent.Content.ReadAsStringAsync();

                    if (content.Length > 7 && content.Substring(startIndex: 0, length: 8) == "{\"error\"")
                    {                     // {"error":"child \"token\" fails because [\"token\" must be a string]"}
                        var token = JToken.Parse(content);
                        return(new IEXResponse <ReturnType>()
                        {
                            ErrorMessage = token["error"].ToString()
                        });
                    }
                    else if (content == "Forbidden" || content == "Not found")
                    {                     // "Forbidden" or "Not found"
                        return(new IEXResponse <ReturnType>()
                        {
                            ErrorMessage = content
                        });
                    }
                    else
                    {
                        return new IEXResponse <ReturnType>()
                               {
                                   Data =
                                       JsonConvert.DeserializeObject <ReturnType>(content, jsonSerializerSettings)
                               }
                    };
                }
                catch (JsonException ex)
                {
                    throw new JsonException(content, ex);
                }
            }
        }