コード例 #1
0
        public async Task Login(string request_token)
        {
            string tokenExchangeEndpoint = $"{this._configuration.GetValue<string>("kiteApiBaseUrl")}{this._configuration.GetValue<string>("kiteAccessTokenUrl")}";
            KiteAccessTokenResponseRoot kiteAccessTokenResponseRoot = null;
            bool cacheFetchResult = this._cache.TryGetValue <KiteAccessTokenResponseRoot>("kite_access_token", out kiteAccessTokenResponseRoot);

            if (!cacheFetchResult)
            {
                try
                {
                    string accessTokenRequest     = $"{this._configuration.GetValue<string>("kiteApiKey")}{request_token}{this._configuration.GetValue<string>("kiteApiSecret")}";
                    string accessTokenRequestHash = CommonFunctions.ComputeSha256Hash(accessTokenRequest);
                    var    param = new Dictionary <string, dynamic>
                    {
                        { "api_key", this._configuration.GetValue <string>("kiteApiKey") },
                        { "request_token", request_token },
                        { "checksum", accessTokenRequestHash }
                    };

                    string      paramString = String.Join("&", param.Select(x => CommonFunctions.BuildParam(x.Key, x.Value)));
                    HttpContent accessTokenRequestContent = new StringContent(paramString, Encoding.UTF8, "application/x-www-form-urlencoded");
                    accessTokenRequestContent.Headers.Add("X-Kite-Version", "3");
                    HttpResponseMessage access_token_response = await this._httpClient.PostAsync(tokenExchangeEndpoint, accessTokenRequestContent);

                    if (access_token_response.IsSuccessStatusCode)
                    {
                        Stream responseStream = await access_token_response.Content.ReadAsStreamAsync();

                        var respose = new StreamReader(responseStream).ReadToEnd();
                        kiteAccessTokenResponseRoot = JsonSerializer.Deserialize <KiteAccessTokenResponseRoot>(respose);
                        MemoryCacheEntryOptions cacheOptions = new MemoryCacheEntryOptions();
                        cacheOptions.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddHours(8));
                        cacheOptions.Priority           = CacheItemPriority.NeverRemove;
                        cacheOptions.RegisterPostEvictionCallback(new PostEvictionDelegate(this.CacheEvictionCallback));
                        cacheOptions.SetSize(CommonFunctions.GetObjectSize(kiteAccessTokenResponseRoot));

                        using (ICacheEntry cacheEntry = this._cache.CreateEntry((object)"kite_access_token"))
                        {
                            cacheEntry.SetSize(CommonFunctions.GetObjectSize(kiteAccessTokenResponseRoot));
                            cacheEntry.SetOptions(cacheOptions);
                            cacheEntry.SetValue(kiteAccessTokenResponseRoot);
                        }
                    }
                    else
                    {
                        this._logger.LogError(access_token_response.ReasonPhrase);
                    }
                }
                catch (Exception ex)
                {
                    this._logger.LogError(ex.Message);
                    throw new IntelliTradeException("An error occurred while trying to get access token from Kite.", ex);
                }
            }
        }
コード例 #2
0
        public async Task Logout()
        {
            KiteAccessTokenResponseRoot kiteAccessTokenResponseRoot = null;
            bool cacheFetchResult = this._cache.TryGetValue <KiteAccessTokenResponseRoot>("kite_access_token", out kiteAccessTokenResponseRoot);

            if ((kiteAccessTokenResponseRoot != null) && cacheFetchResult)
            {
                this._cache.Remove("kite_access_token");
                var param = new Dictionary <string, dynamic>();
                CommonFunctions.AddIfNotNull(param, "api_key", this._configuration.GetValue <string>("kiteApiKey"));
                CommonFunctions.AddIfNotNull(param, "access_token", (string)kiteAccessTokenResponseRoot.data.access_token);
                string encodedParmas = string.Empty;
                foreach (KeyValuePair <string, dynamic> keyValuePair in param)
                {
                    if (string.IsNullOrEmpty(encodedParmas))
                    {
                        encodedParmas = CommonFunctions.BuildParam(keyValuePair.Key, keyValuePair.Value);
                    }
                    else
                    {
                        encodedParmas = encodedParmas + "&" + CommonFunctions.BuildParam(keyValuePair.Key, keyValuePair.Value);
                    }
                }

                string kiteLogoutUrl = $"{this._configuration.GetValue<string>("kiteApiBaseUrl")}{this._configuration.GetValue<string>("kiteAccessTokenUrl")}?{encodedParmas}";
                this._httpClient.DefaultRequestHeaders.Add("X-Kite-Version", "3");
                HttpResponseMessage httpResponseMessage = await this._httpClient.DeleteAsync(kiteLogoutUrl);

                if (httpResponseMessage.IsSuccessStatusCode)
                {
                    this._logger.LogInformation("Logout from Kite API Successful.");
                }
                else
                {
                    this._logger.LogError("Logout from Kite API Unsuccessful.");
                }
            }
        }