<commit_before>﻿/**
* Copyright 2017 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

using IBM.WatsonDeveloperCloud.Http;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;

namespace IBM.WatsonDeveloperCloud.Util
{
    public class TokenManager
    {
        public IClient Client { get; set; }

        private string _iamUrl;
        private IamTokenData _tokenInfo;
        private string _iamApikey;
        private string _userAccessToken;

        public TokenManager(TokenOptions options)
        {
            _iamUrl = "https://iam.ng.bluemix.net/identity/token";
            _tokenInfo = new IamTokenData();
            if (!string.IsNullOrEmpty(options.IamApiKey))
                _iamApikey = options.IamApiKey;
            if (!string.IsNullOrEmpty(options.IamAccessToken))
                _userAccessToken = options.IamAccessToken;

            this.Client = new WatsonHttpClient(this._iamUrl);
        }

        /// <summary>
        /// This function sends an access token back through a callback. The source of the token
        /// is determined by the following logic:
        /// 1. If user provides their own managed access token, assume it is valid and send it
        /// 2. If this class is managing tokens and does not yet have one, make a request for one
        /// 3. If this class is managing tokens and the token has expired, refresh it
        /// 4. If this class is managing tokens and has a valid token stored, send it
        /// </summary>
        /// <returns>An IamTokenData object containing the IAM token.</returns>
        public string GetToken()
        {
            if (!string.IsNullOrEmpty(_userAccessToken))
            {
                // 1. use user-managed token
                return _userAccessToken;
            }
            else if (!string.IsNullOrEmpty(_tokenInfo.AccessToken) || IsRefreshTokenExpired())
            {
                // 2. request an initial token
                var tokenInfo = RequestToken();<commit_msg>Change default iam url to iam.bluemix.net. only set if options.IamUrl is not supplied.<commit_after>﻿/**
* Copyright 2017 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

using IBM.WatsonDeveloperCloud.Http;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;

namespace IBM.WatsonDeveloperCloud.Util
{
    public class TokenManager
    {
        public IClient Client { get; set; }

        private string _iamUrl;
        private IamTokenData _tokenInfo;
        private string _iamApikey;
        private string _userAccessToken;

        public TokenManager(TokenOptions options)
        {
            _iamUrl = !string.IsNullOrEmpty(options.IamUrl) ? options.IamUrl : "https://iam.bluemix.net/identity/token";
            _tokenInfo = new IamTokenData();
            if (!string.IsNullOrEmpty(options.IamApiKey))
                _iamApikey = options.IamApiKey;
            if (!string.IsNullOrEmpty(options.IamAccessToken))
                _userAccessToken = options.IamAccessToken;

            this.Client = new WatsonHttpClient(this._iamUrl);
        }

        /// <summary>
        /// This function sends an access token back through a callback. The source of the token
        /// is determined by the following logic:
        /// 1. If user provides their own managed access token, assume it is valid and send it
        /// 2. If this class is managing tokens and does not yet have one, make a request for one
        /// 3. If this class is managing tokens and the token has expired, refresh it
        /// 4. If this class is managing tokens and has a valid token stored, send it
        /// </summary>
        /// <returns>An IamTokenData object containing the IAM token.</returns>
        public string GetToken()
        {
            if (!string.IsNullOrEmpty(_userAccessToken))
            {
                // 1. use user-managed token
                return _userAccessToken;
            }
            else if (!string.IsNullOrEmpty(_tokenInfo.AccessToken) || IsRefreshTokenExpired())
            {
                // 2. request an initial token
                var tokenInfo = RequestToken();
{'fraction_non_alphanumeric': 0.07929016424391165, 'fraction_numerical': 0.00528601094959411, 'mean_word_length': 2.387468030690537, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6031189', 'n_tokens_mistral': 1381, 'n_tokens_neox': 1331, 'n_words': 523}