public void AddRequestGuid(RequestGuid requestGuid)
        {
            ExpirationWrapper <RequestGuid> wrapper = new ExpirationWrapper <RequestGuid>(requestGuid, REQUEST_TIMEOUT_MILLISECONDS);

            wrapper.Expired += RequestExpiredHandler;
            wrapper.RestartTimer();
            if (!_authRequests.TryAdd(requestGuid.ToString(), wrapper))
            {
                throw new InvalidOperationException("Request already exists.");
            }
        }
        public async Task <string> GetTokenAsync(string requestGuidString, string authCode)
        {
            if (_token == null || !_token.IsExpired)
            {
                throw new InvalidOperationException("A valid token has already been retrieved.");
            }

            if (!VerifyAuthRequest(requestGuidString))
            {
                throw new ArgumentException("Invalid request guid.");
            }

            _guid = requestGuidString;

            HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, TOKEN_URI);

            Dictionary <string, string> values = new Dictionary <string, string>
            {
                { "grant_type", "authorization_code" },
                { "code", authCode },
                { "client_id", OAUTH_CLIENT_ID },
                { "redirect_uri", REDIRECT_URI },
            };
            FormUrlEncodedContent content = new FormUrlEncodedContent(values);

            message.Content = content;

            HttpResponseMessage response = await _client.SendAsync(message);

            string responseString = await response.Content.ReadAsStringAsync();

            if (responseString == null)
            {
                throw new InvalidResponseException("Empty response returned by token request.");
            }

            TokenResponse tokenResponse = JsonConvert.DeserializeObject <TokenResponse>(responseString);

            if (tokenResponse == null || tokenResponse.TokenType != TOKEN_TYPE)
            {
                throw new InvalidResponseException("Invalid response returned by token request.");
            }

            _token          = new ExpirationWrapper <string>(tokenResponse.AccessToken, tokenResponse.ExpiresIn * 1000);
            _token.Expired += TokenExpiredHandler;
            _membershipId   = tokenResponse.MembershipId;

            _clientMgr.AddApiClient(this);
            return(requestGuidString);
        }
        private void RequestExpiredHandler(object sender, EventArgs e)
        {
            if (!(sender is ExpirationWrapper <RequestGuid>))
            {
                /* Shouldn't happen */
                return;
            }

            ExpirationWrapper <RequestGuid> wrapper = (ExpirationWrapper <RequestGuid>)sender;

            if (!_authRequests.TryRemove(wrapper.ToString(), out wrapper))
            {
                /* Request was already removed, ignore */
                return;
            }
        }
 public bool Equals(ExpirationWrapper <T> other)
 {
     return(Value.Equals(other.Value));
 }