public virtual HttpRequest Create(GetChangesRequest request)
        {
            Ensure.That(request, "request").IsNotNull();

            return new HttpRequest(HttpMethod.Get, GenerateRelativeUrl(request))
                .SetRequestTypeHeader(request.GetType());
        }
        /// <summary>
        /// Returns all configured options of <see cref="GetChangesRequest"/> as key values.
        /// The values are formatted to JSON-compatible strings.
        /// </summary>
        /// <returns></returns>
        protected virtual IDictionary<string, string> ConvertRequestToJsonCompatibleKeyValues(GetChangesRequest request)
        {
            var kvs = new Dictionary<string, string>();

            if (request.Feed.HasValue)
                kvs.Add(KeyNames.Feed, request.Feed.Value.AsString());

            if (!string.IsNullOrWhiteSpace(request.Since))
                kvs.Add(KeyNames.Since, request.Since);

            if (request.IncludeDocs.HasValue)
                kvs.Add(KeyNames.IncludeDocs, request.IncludeDocs.Value.ToString().ToLower());

            if (request.Descending.HasValue)
                kvs.Add(KeyNames.Descending, request.Descending.Value.ToString().ToLower());

            if (request.Limit.HasValue)
                kvs.Add(KeyNames.Limit, request.Limit.Value.ToString(MyCouchRuntime.FormatingCulture.NumberFormat));

            if (request.Heartbeat.HasValue)
                kvs.Add(KeyNames.HeartBeat, request.Heartbeat.Value.ToString(MyCouchRuntime.FormatingCulture.NumberFormat));

            if (request.Timeout.HasValue)
                kvs.Add(KeyNames.Timeout, request.Timeout.Value.ToString(MyCouchRuntime.FormatingCulture.NumberFormat));

            if (request.Filter != null)
                kvs.Add(KeyNames.Filter, request.Filter);

            if (request.Style.HasValue)
                kvs.Add(KeyNames.Style, request.Style.Value.AsString());

            return kvs;
        }
        public override HttpRequest Create(GetChangesRequest request)
        {
            Ensure.That(request, "request").IsNotNull();

            EnsureNonContinuousFeedIsRequested(request);

            return base.Create(request);
        }
 protected virtual string GenerateQueryStringParams(GetChangesRequest request)
 {
     return string.Join("&", ConvertRequestToJsonCompatibleKeyValues(request)
         .Select(kv => string.Format("{0}={1}", kv.Key, Uri.EscapeDataString(kv.Value))));
 }
        protected virtual string GenerateQueryString(GetChangesRequest request)
        {
            var p = GenerateQueryStringParams(request);

            return string.IsNullOrEmpty(p) ? string.Empty : string.Concat("?", p);
        }
 protected virtual string GenerateRelativeUrl(GetChangesRequest request)
 {
     return string.Format("/_changes{0}",
         GenerateQueryString(request));
 }
 protected virtual void EnsureNonContinuousFeedIsRequested(GetChangesRequest request)
 {
     if (request.Feed.HasValue && request.Feed == ChangesFeed.Continuous)
         throw new ArgumentException(ExceptionStrings.GetChangesForNonContinuousFeedOnly, "request");
 }
 protected virtual void EnsureContinuousFeedIsRequested(GetChangesRequest request)
 {
     if (request.Feed.HasValue && request.Feed != ChangesFeed.Continuous)
         throw new ArgumentException(ExceptionStrings.GetContinuousChangesInvalidFeed, "request");
 }
        public async Task<BatchSet> GetChanges(string bucketName, Filter query, int limit, string anchor,string uploadAnchor)
        {
            using (var client = new MyCouchClient(DbServerUrl, bucketName))
            {
                
                GetChangesRequest changesReq = new GetChangesRequest();
                changesReq.Since = anchor;
                changesReq.Limit = limit;
                changesReq.IncludeDocs = true;
                var response = await client.Changes.GetAsync(changesReq);
                if (response.IsSuccess)
                {
                    BatchSet changeSet = new BatchSet();
                    if (response.Results != null)
                    {
                        SyncLogItem logItem = await this.GetSyncLogItem(uploadAnchor);

                        foreach (var row in response.Results)
                        {

                            if (row.Deleted)
                            {
                                this.AddDeletedDoc(changeSet, row, logItem);
                            }
                            else
                            {
                                this.AddChangedDoc(changeSet, row, logItem, query,client.Serializer );
                            }
                        }
                        changeSet.Anchor = response.LastSeq;
                    }

                    return changeSet;
                }
                else CheckBucketNotFound(bucketName, response);
                return null;

            }
        
        }