Ejemplo n.º 1
0
        private static LoginToken GetLoginToken(String url, HttpContent authContent)
        {
            try
            {
                HttpClient c = HttpClientSingleton.GetClient();

                var uri = new Uri(MakeUrl(url, LOGIN_URI_SUFFIX));

                _log.Debug($"Login URL: {uri}");

                var response = c.PostAsync(uri, authContent).Result;

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    if (_log.IsDebugEnabled)
                    {
                        _log.Debug($"Response code [{response.StatusCode}]: " +
                                   $"{response.Content.ReadAsStringAsync().Result}");
                    }
                    throw new InvalidOperationException(response.ReasonPhrase);
                }
                else
                {
                    _log.Debug("Successful login.");
                }


                using (JsonReader r = new JsonTextReader(new StreamReader
                                                             (response.Content.ReadAsStreamAsync().Result)))
                {
                    var results = JsonSerializer.Create().Deserialize(r,
                                                                      typeof(Dictionary <String, String>)) as Dictionary <String, String>;

                    return(new LoginToken
                    {
                        TokenType = results["token_type"],
                        ExpireTime = DateTime.Now.AddSeconds(Convert.ToDouble(results["expires_in"])),
                        Token = results["access_token"],
                        ReauthContent = authContent
                    });
                }
            }
            catch (HttpRequestException hex)
            {
                // Next operation will try to log in again due to expired token.
                _log.Warn("Unable to obtain login token due to a communication problem. " +
                          "Login will retry on next operation.", hex);
                return(GetNullToken(authContent));
            }
            catch (Exception ex)
            {
                _log.Warn("Unable to obtain login token due to an unexpected exception.  " +
                          "Login will retry on the next operation.", ex);
                return(GetNullToken(authContent));
            }
        }
Ejemplo n.º 2
0
        public void TestInitializedAccessGivesResult()
        {
            HttpClientSingleton.Initialize(true, TimeSpan.FromSeconds(600));

            var  h      = HttpClientSingleton.GetClient();
            bool result = h != null;

            HttpClientSingleton.Clear();
            Assert.True(result);
        }
Ejemplo n.º 3
0
        public void TestUninitializedAccessThrowsError()
        {
            try
            {
                var h = HttpClientSingleton.GetClient();
            }
            catch (InvalidOperationException)
            {
                HttpClientSingleton.Clear();
                Assert.Pass();
                return;
            }


            HttpClientSingleton.Clear();
            Assert.Fail();
        }