public ServiceResponse(HttpResponseMessage response, Stream responseContent, Func <Stream, TContent> convert)
        {
            Code        = (int)response.StatusCode;
            Success     = response.IsSuccessStatusCode;
            ContentType = response.Content.Headers.ContentType.ToString();

            switch (response.StatusCode)
            {
            case HttpStatusCode.OK:
                Content = convert(responseContent);
                break;

            case HttpStatusCode.Forbidden:
                Error = AuthenticationError.Parse(responseContent);
                break;

            case HttpStatusCode.BadRequest:
                Error = AuthenticationError.Parse(responseContent) ?? ServiceError.Parse(responseContent);
                break;

            default:
                Error = ServiceError.Parse(responseContent);
                break;
            }
        }
        /// <summary>
        ///     Gets a valid access token.
        /// </summary>
        /// <param name="cancellationToken">The cancellation notification.</param>
        /// <returns>A valid access token.</returns>
        private async Task <OAuthToken> GetToken(CancellationToken cancellationToken)
        {
            if (_cacheToken != null && _cacheToken.Validate())
            {
                return(_cacheToken);
            }

            using (var request = new HttpRequestMessage(HttpMethod.Post, "auth/accesstoken"))
            {
                request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
                request.Headers.Authorization = new AuthenticationHeaderValue("Basic", _consumer);
                request.Content = new FormUrlEncodedContent(new Dictionary <string, string> {
                    { "grant_type", "client_credentials" }
                });

                using (var response = await _client.SendAsync(request, cancellationToken))
                    using (var responseContent = await response.Content.ReadAsStreamAsync())
                    {
                        if (!response.IsSuccessStatusCode)
                        {
                            var error = AuthenticationError.Parse(responseContent);
                            throw new OPSAuthenticationException(error);
                        }

                        try
                        {
                            var serializer = new DataContractJsonSerializer(typeof(OAuthToken));
                            return(_cacheToken = serializer.ReadObject(responseContent) as OAuthToken);
                        }
                        catch (Exception exception)
                        {
                            throw new OPSSerializationException(responseContent, exception);
                        }
                    }
            }
        }