private T DeserializeResponse <T>(IRestResponse response)
        {
            var obj = JsonConvert.DeserializeObject <T>(response.Content, JsonNetSerializationSettings.GetDefault());

            if (obj.GetType().GetInterfaces().Contains(typeof(IKillBillObjects)))
            {
                var objects = obj as IKillBillObjects;

                // Get Pagination meta data from the response headers
                if (objects == null)
                {
                    return(obj);
                }

                var paginationCurrentOffset = response.GetValue(_config.HDR_PAGINATION_CURRENT_OFFSET);
                if (paginationCurrentOffset != null)
                {
                    objects.PaginationCurrentOffset = paginationCurrentOffset.ToInt();
                }

                var paginationNextOffset = response.GetValue(_config.HDR_PAGINATION_NEXT_OFFSET);
                if (paginationNextOffset != null)
                {
                    objects.PaginationNextOffset = paginationNextOffset.ToInt();
                }

                var paginationMaxNbRecords = response.GetValue(_config.HDR_PAGINATION_MAX_NB_RECORDS);
                if (paginationMaxNbRecords != null)
                {
                    objects.PaginationMaxNbRecords = paginationMaxNbRecords.ToInt();
                }

                var paginationNextPageUri = response.GetValue(_config.HDR_PAGINATION_NEXT_PAGE_URI);
                if (paginationNextPageUri != null)
                {
                    objects.PaginationNextPageUri = paginationNextPageUri;
                }

                objects.KillBillHttpClient = this;
            }

            return(obj);
        }
        private void CheckResponse <T>(IRestResponse response, out T defaultObject)
            where T : class
        {
            if (response == null)
            {
                throw new KillBillClientException("Error calling KillBill: no response");
            }

            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                throw new KillBillClientException(response.ErrorMessage, new ArgumentException("Unauthorized - did you configure your RBAC and/or tenant credentials?"));
            }

            if (response.StatusCode == HttpStatusCode.NoContent || response.StatusCode == HttpStatusCode.NotFound)
            {
                // Return empty list for KillBillObjects instead of null for convenience
                if (typeof(T).IsAssignableFrom(typeof(KillBillObjects <>)))
                {
                    defaultObject = Activator.CreateInstance <T>();
                    return;
                }

                defaultObject = default(T);
                return;
            }

            if (response.StatusCode >= HttpStatusCode.BadRequest && response.Content != null)
            {
                var billingException = JsonConvert.DeserializeObject <BillingException>(response.Content, JsonNetSerializationSettings.GetDefault());
                var message          = "Error " + response.StatusCode + " from Kill Bill " + billingException.Message;
                throw new KillBillClientException(message, response.StatusCode.ToString(), billingException.Code, billingException.Message);
            }

            if (response.ErrorException != null)
            {
                const string message   = "An unexpected error occurred connecting with KillBill: ";
                var          exception = new KillBillClientException(message, response.ErrorException);
                throw exception;
            }

            defaultObject = default(T);
        }